]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportAttachment.php
e586b91b0ee69556ba367a348db18ca265e46342
[bookstack] / app / Exports / ZipExports / Models / ZipExportAttachment.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\Attachment;
8
9 class ZipExportAttachment extends ZipExportModel
10 {
11     public ?int $id = null;
12     public string $name;
13     public ?int $order = null;
14     public ?string $link = null;
15     public ?string $file = null;
16
17     public static function fromModel(Attachment $model, ZipExportFiles $files): self
18     {
19         $instance = new self();
20         $instance->id = $model->id;
21         $instance->name = $model->name;
22         $instance->order = $model->order;
23
24         if ($model->external) {
25             $instance->link = $model->path;
26         } else {
27             $instance->file = $files->referenceForAttachment($model);
28         }
29
30         return $instance;
31     }
32
33     public static function fromModelArray(array $attachmentArray, ZipExportFiles $files): array
34     {
35         return array_values(array_map(function (Attachment $attachment) use ($files) {
36             return self::fromModel($attachment, $files);
37         }, $attachmentArray));
38     }
39
40     public static function validate(ZipValidationHelper $context, array $data): array
41     {
42         $rules = [
43             'id'    => ['nullable', 'int'],
44             'name'  => ['required', 'string', 'min:1'],
45             'order' => ['nullable', 'integer'],
46             'link'  => ['required_without:file', 'nullable', 'string'],
47             'file'  => ['required_without:link', 'nullable', 'string', $context->fileReferenceRule()],
48         ];
49
50         return $context->validateData($data, $rules);
51     }
52 }