]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportChapter.php
Merge branch 'portazips' into development
[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 = 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 function children(): array
34     {
35         return $this->pages;
36     }
37
38     public static function fromModel(Chapter $model, ZipExportFiles $files): self
39     {
40         $instance = new self();
41         $instance->id = $model->id;
42         $instance->name = $model->name;
43         $instance->description_html = $model->descriptionHtml();
44         $instance->priority = $model->priority;
45         $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
46
47         $pages = $model->getVisiblePages()->filter(fn (Page $page) => !$page->draft)->all();
48         $instance->pages = ZipExportPage::fromModelArray($pages, $files);
49
50         return $instance;
51     }
52
53     /**
54      * @param Chapter[] $chapterArray
55      * @return self[]
56      */
57     public static function fromModelArray(array $chapterArray, ZipExportFiles $files): array
58     {
59         return array_values(array_map(function (Chapter $chapter) use ($files) {
60             return self::fromModel($chapter, $files);
61         }, $chapterArray));
62     }
63
64     public static function validate(ZipValidationHelper $context, array $data): array
65     {
66         $rules = [
67             'id'    => ['nullable', 'int', $context->uniqueIdRule('chapter')],
68             'name'  => ['required', 'string', 'min:1'],
69             'description_html' => ['nullable', 'string'],
70             'priority' => ['nullable', 'int'],
71             'tags' => ['array'],
72             'pages' => ['array'],
73         ];
74
75         $errors = $context->validateData($data, $rules);
76         $errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
77         $errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
78
79         return $errors;
80     }
81
82     public static function fromArray(array $data): self
83     {
84         $model = new self();
85
86         $model->id = $data['id'] ?? null;
87         $model->name = $data['name'];
88         $model->description_html = $data['description_html'] ?? null;
89         $model->priority = isset($data['priority']) ? intval($data['priority']) : null;
90         $model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
91         $model->pages = ZipExportPage::fromManyArray($data['pages'] ?? []);
92
93         return $model;
94     }
95 }