3 namespace BookStack\Exports\ZipExports\Models;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Tools\PageContent;
7 use BookStack\Exports\ZipExports\ZipExportFiles;
8 use BookStack\Exports\ZipExports\ZipValidationHelper;
10 class ZipExportPage extends ZipExportModel
12 public ?int $id = null;
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 = [];
24 public function metadataOnly(): void
26 $this->html = $this->markdown = $this->priority = null;
28 foreach ($this->attachments as $attachment) {
29 $attachment->metadataOnly();
31 foreach ($this->images as $image) {
32 $image->metadataOnly();
34 foreach ($this->tags as $tag) {
39 public static function fromModel(Page $model, ZipExportFiles $files): self
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;
47 if (!empty($model->markdown)) {
48 $instance->markdown = $model->markdown;
51 $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
52 $instance->attachments = ZipExportAttachment::fromModelArray($model->attachments()->get()->all(), $files);
58 * @param Page[] $pageArray
61 public static function fromModelArray(array $pageArray, ZipExportFiles $files): array
63 return array_values(array_map(function (Page $page) use ($files) {
64 return self::fromModel($page, $files);
68 public static function validate(ZipValidationHelper $context, array $data): array
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'],
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);
89 public static function fromArray(array $data): self
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'] ?? []);