]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportAttachment.php
Merge branch 'docker-simplify' into development
[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 ?string $link = null;
14     public ?string $file = null;
15
16     public function metadataOnly(): void
17     {
18         $this->link = $this->file = null;
19     }
20
21     public static function fromModel(Attachment $model, ZipExportFiles $files): self
22     {
23         $instance = new self();
24         $instance->id = $model->id;
25         $instance->name = $model->name;
26
27         if ($model->external) {
28             $instance->link = $model->path;
29         } else {
30             $instance->file = $files->referenceForAttachment($model);
31         }
32
33         return $instance;
34     }
35
36     public static function fromModelArray(array $attachmentArray, ZipExportFiles $files): array
37     {
38         return array_values(array_map(function (Attachment $attachment) use ($files) {
39             return self::fromModel($attachment, $files);
40         }, $attachmentArray));
41     }
42
43     public static function validate(ZipValidationHelper $context, array $data): array
44     {
45         $rules = [
46             'id'    => ['nullable', 'int', $context->uniqueIdRule('attachment')],
47             'name'  => ['required', 'string', 'min:1'],
48             'link'  => ['required_without:file', 'nullable', 'string'],
49             'file'  => ['required_without:link', 'nullable', 'string', $context->fileReferenceRule()],
50         ];
51
52         return $context->validateData($data, $rules);
53     }
54
55     public static function fromArray(array $data): self
56     {
57         $model = new self();
58
59         $model->id = $data['id'] ?? null;
60         $model->name = $data['name'];
61         $model->link = $data['link'] ?? null;
62         $model->file = $data['file'] ?? null;
63
64         return $model;
65     }
66 }