]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportBook.php
ZIP Exports: Added ID checks and testing to validator
[bookstack] / app / Exports / ZipExports / Models / ZipExportBook.php
1 <?php
2
3 namespace BookStack\Exports\ZipExports\Models;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Exports\ZipExports\ZipExportFiles;
9 use BookStack\Exports\ZipExports\ZipValidationHelper;
10
11 class ZipExportBook extends ZipExportModel
12 {
13     public ?int $id = null;
14     public string $name;
15     public ?string $description_html = null;
16     public ?string $cover = null;
17     /** @var ZipExportChapter[] */
18     public array $chapters = [];
19     /** @var ZipExportPage[] */
20     public array $pages = [];
21     /** @var ZipExportTag[] */
22     public array $tags = [];
23
24     public function metadataOnly(): void
25     {
26         $this->description_html = $this->cover = null;
27
28         foreach ($this->chapters as $chapter) {
29             $chapter->metadataOnly();
30         }
31         foreach ($this->pages as $page) {
32             $page->metadataOnly();
33         }
34         foreach ($this->tags as $tag) {
35             $tag->metadataOnly();
36         }
37     }
38
39     public static function fromModel(Book $model, ZipExportFiles $files): self
40     {
41         $instance = new self();
42         $instance->id = $model->id;
43         $instance->name = $model->name;
44         $instance->description_html = $model->descriptionHtml();
45
46         if ($model->cover) {
47             $instance->cover = $files->referenceForImage($model->cover);
48         }
49
50         $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
51
52         $chapters = [];
53         $pages = [];
54
55         $children = $model->getDirectVisibleChildren()->all();
56         foreach ($children as $child) {
57             if ($child instanceof Chapter) {
58                 $chapters[] = $child;
59             } else if ($child instanceof Page) {
60                 $pages[] = $child;
61             }
62         }
63
64         $instance->pages = ZipExportPage::fromModelArray($pages, $files);
65         $instance->chapters = ZipExportChapter::fromModelArray($chapters, $files);
66
67         return $instance;
68     }
69
70     public static function validate(ZipValidationHelper $context, array $data): array
71     {
72         $rules = [
73             'id'    => ['nullable', 'int', $context->uniqueIdRule('book')],
74             'name'  => ['required', 'string', 'min:1'],
75             'description_html' => ['nullable', 'string'],
76             'cover' => ['nullable', 'string', $context->fileReferenceRule()],
77             'tags' => ['array'],
78             'pages' => ['array'],
79             'chapters' => ['array'],
80         ];
81
82         $errors = $context->validateData($data, $rules);
83         $errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
84         $errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
85         $errors['chapters'] = $context->validateRelations($data['chapters'] ?? [], ZipExportChapter::class);
86
87         return $errors;
88     }
89
90     public static function fromArray(array $data): self
91     {
92         $model = new self();
93
94         $model->id = $data['id'] ?? null;
95         $model->name = $data['name'];
96         $model->description_html = $data['description_html'] ?? null;
97         $model->cover = $data['cover'] ?? null;
98         $model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
99         $model->pages = ZipExportPage::fromManyArray($data['pages'] ?? []);
100         $model->chapters = ZipExportChapter::fromManyArray($data['chapters'] ?? []);
101
102         return $model;
103     }
104 }