]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportChapter.php
ZIP Imports: Added image type validation/handling
[bookstack] / app / Exports / ZipExports / Models / ZipExportChapter.php
1 <?php
2
3 namespace BookStack\Exports\ZipExports\Models;
4
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Exports\ZipExports\ZipExportFiles;
8 use BookStack\Exports\ZipExports\ZipValidationHelper;
9
10 class ZipExportChapter extends ZipExportModel
11 {
12     public ?int $id = null;
13     public string $name;
14     public ?string $description_html = null;
15     public ?int $priority = null;
16     /** @var ZipExportPage[] */
17     public array $pages = [];
18     /** @var ZipExportTag[] */
19     public array $tags = [];
20
21     public function metadataOnly(): void
22     {
23         $this->description_html = $this->priority = null;
24
25         foreach ($this->pages as $page) {
26             $page->metadataOnly();
27         }
28         foreach ($this->tags as $tag) {
29             $tag->metadataOnly();
30         }
31     }
32
33     public static function fromModel(Chapter $model, ZipExportFiles $files): self
34     {
35         $instance = new self();
36         $instance->id = $model->id;
37         $instance->name = $model->name;
38         $instance->description_html = $model->descriptionHtml();
39         $instance->priority = $model->priority;
40         $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
41
42         $pages = $model->getVisiblePages()->filter(fn (Page $page) => !$page->draft)->all();
43         $instance->pages = ZipExportPage::fromModelArray($pages, $files);
44
45         return $instance;
46     }
47
48     /**
49      * @param Chapter[] $chapterArray
50      * @return self[]
51      */
52     public static function fromModelArray(array $chapterArray, ZipExportFiles $files): array
53     {
54         return array_values(array_map(function (Chapter $chapter) use ($files) {
55             return self::fromModel($chapter, $files);
56         }, $chapterArray));
57     }
58
59     public static function validate(ZipValidationHelper $context, array $data): array
60     {
61         $rules = [
62             'id'    => ['nullable', 'int', $context->uniqueIdRule('chapter')],
63             'name'  => ['required', 'string', 'min:1'],
64             'description_html' => ['nullable', 'string'],
65             'priority' => ['nullable', 'int'],
66             'tags' => ['array'],
67             'pages' => ['array'],
68         ];
69
70         $errors = $context->validateData($data, $rules);
71         $errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
72         $errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
73
74         return $errors;
75     }
76
77     public static function fromArray(array $data): self
78     {
79         $model = new self();
80
81         $model->id = $data['id'] ?? null;
82         $model->name = $data['name'];
83         $model->description_html = $data['description_html'] ?? null;
84         $model->priority = isset($data['priority']) ? intval($data['priority']) : null;
85         $model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
86         $model->pages = ZipExportPage::fromManyArray($data['pages'] ?? []);
87
88         return $model;
89     }
90 }