]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportPage.php
3a876e7aaff22a57ec47d5282b2b978877c40664
[bookstack] / app / Exports / ZipExports / Models / ZipExportPage.php
1 <?php
2
3 namespace BookStack\Exports\ZipExports\Models;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Tools\PageContent;
7 use BookStack\Exports\ZipExports\ZipExportFiles;
8 use BookStack\Exports\ZipExports\ZipValidationHelper;
9
10 class ZipExportPage extends ZipExportModel
11 {
12     public ?int $id = null;
13     public string $name;
14     public ?string $html = null;
15     public ?string $markdown = null;
16     public ?int $priority = null;
17     /** @var ZipExportAttachment[] */
18     public array $attachments = [];
19     /** @var ZipExportImage[] */
20     public array $images = [];
21     /** @var ZipExportTag[] */
22     public array $tags = [];
23
24     public function metadataOnly(): void
25     {
26         $this->html = $this->markdown = $this->priority = null;
27
28         foreach ($this->attachments as $attachment) {
29             $attachment->metadataOnly();
30         }
31         foreach ($this->images as $image) {
32             $image->metadataOnly();
33         }
34         foreach ($this->tags as $tag) {
35             $tag->metadataOnly();
36         }
37     }
38
39     public static function fromModel(Page $model, ZipExportFiles $files): self
40     {
41         $instance = new self();
42         $instance->id = $model->id;
43         $instance->name = $model->name;
44         $instance->html = (new PageContent($model))->render();
45         $instance->priority = $model->priority;
46
47         if (!empty($model->markdown)) {
48             $instance->markdown = $model->markdown;
49         }
50
51         $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
52         $instance->attachments = ZipExportAttachment::fromModelArray($model->attachments()->get()->all(), $files);
53
54         return $instance;
55     }
56
57     /**
58      * @param Page[] $pageArray
59      * @return self[]
60      */
61     public static function fromModelArray(array $pageArray, ZipExportFiles $files): array
62     {
63         return array_values(array_map(function (Page $page) use ($files) {
64             return self::fromModel($page, $files);
65         }, $pageArray));
66     }
67
68     public static function validate(ZipValidationHelper $context, array $data): array
69     {
70         $rules = [
71             'id'    => ['nullable', 'int'],
72             'name'  => ['required', 'string', 'min:1'],
73             'html' => ['nullable', 'string'],
74             'markdown' => ['nullable', 'string'],
75             'priority' => ['nullable', 'int'],
76             'attachments' => ['array'],
77             'images' => ['array'],
78             'tags' => ['array'],
79         ];
80
81         $errors = $context->validateData($data, $rules);
82         $errors['attachments'] = $context->validateRelations($data['attachments'] ?? [], ZipExportAttachment::class);
83         $errors['images'] = $context->validateRelations($data['images'] ?? [], ZipExportImage::class);
84         $errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
85
86         return $errors;
87     }
88
89     public static function fromArray(array $data): self
90     {
91         $model = new self();
92
93         $model->id = $data['id'] ?? null;
94         $model->name = $data['name'];
95         $model->html = $data['html'] ?? null;
96         $model->markdown = $data['markdown'] ?? null;
97         $model->priority = isset($data['priority']) ? intval($data['priority']) : null;
98         $model->attachments = ZipExportAttachment::fromManyArray($data['attachments'] ?? []);
99         $model->images = ZipExportImage::fromManyArray($data['images'] ?? []);
100         $model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
101
102         return $model;
103     }
104 }