]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportImage.php
89083b15be116bbe1e97e2833559131265177cc6
[bookstack] / app / Exports / ZipExports / Models / ZipExportImage.php
1 <?php
2
3 namespace BookStack\Exports\ZipExports\Models;
4
5 use BookStack\Exports\ZipExports\ZipExportFiles;
6 use BookStack\Exports\ZipExports\ZipValidationHelper;
7 use BookStack\Uploads\Image;
8 use Illuminate\Validation\Rule;
9
10 class ZipExportImage extends ZipExportModel
11 {
12     public ?int $id = null;
13     public string $name;
14     public string $file;
15     public string $type;
16
17     public static function fromModel(Image $model, ZipExportFiles $files): self
18     {
19         $instance = new self();
20         $instance->id = $model->id;
21         $instance->name = $model->name;
22         $instance->type = $model->type;
23         $instance->file = $files->referenceForImage($model);
24
25         return $instance;
26     }
27
28     public function metadataOnly(): void
29     {
30         //
31     }
32
33     public static function validate(ZipValidationHelper $context, array $data): array
34     {
35         $rules = [
36             'id'    => ['nullable', 'int', $context->uniqueIdRule('image')],
37             'name'  => ['required', 'string', 'min:1'],
38             'file'  => ['required', 'string', $context->fileReferenceRule()],
39             'type'  => ['required', 'string', Rule::in(['gallery', 'drawio'])],
40         ];
41
42         return $context->validateData($data, $rules);
43     }
44
45     public static function fromArray(array $data): self
46     {
47         $model = new self();
48
49         $model->id = $data['id'] ?? null;
50         $model->name = $data['name'];
51         $model->file = $data['file'];
52         $model->type = $data['type'];
53
54         return $model;
55     }
56 }