]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportPage.php
2c8b9a88abdf13bf990aafef630e276eac026565
[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 static function fromModel(Page $model, ZipExportFiles $files): self
25     {
26         $instance = new self();
27         $instance->id = $model->id;
28         $instance->name = $model->name;
29         $instance->html = (new PageContent($model))->render();
30         $instance->priority = $model->priority;
31
32         if (!empty($model->markdown)) {
33             $instance->markdown = $model->markdown;
34         }
35
36         $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
37         $instance->attachments = ZipExportAttachment::fromModelArray($model->attachments()->get()->all(), $files);
38
39         return $instance;
40     }
41
42     /**
43      * @param Page[] $pageArray
44      * @return self[]
45      */
46     public static function fromModelArray(array $pageArray, ZipExportFiles $files): array
47     {
48         return array_values(array_map(function (Page $page) use ($files) {
49             return self::fromModel($page, $files);
50         }, $pageArray));
51     }
52
53     public static function validate(ZipValidationHelper $context, array $data): array
54     {
55         $rules = [
56             'id'    => ['nullable', 'int'],
57             'name'  => ['required', 'string', 'min:1'],
58             'html' => ['nullable', 'string'],
59             'markdown' => ['nullable', 'string'],
60             'priority' => ['nullable', 'int'],
61             'attachments' => ['array'],
62             'images' => ['array'],
63             'tags' => ['array'],
64         ];
65
66         $errors = $context->validateData($data, $rules);
67         $errors['attachments'] = $context->validateRelations($data['attachments'] ?? [], ZipExportAttachment::class);
68         $errors['images'] = $context->validateRelations($data['images'] ?? [], ZipExportImage::class);
69         $errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
70
71         return $errors;
72     }
73 }