3 namespace BookStack\Exports\ZipExports\Models;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Exports\ZipExports\ZipExportFiles;
9 use BookStack\Exports\ZipExports\ZipValidationHelper;
11 class ZipExportBook extends ZipExportModel
13 public ?int $id = null;
15 public ?string $description_html = null;
16 public ?string $cover = null;
17 /** @var ZipExportChapter[] */
18 public array $chapters = [];
19 /** @var ZipExportPage[] */
20 public array $pages = [];
21 /** @var ZipExportTag[] */
22 public array $tags = [];
24 public function metadataOnly(): void
26 $this->description_html = $this->cover = null;
28 foreach ($this->chapters as $chapter) {
29 $chapter->metadataOnly();
31 foreach ($this->pages as $page) {
32 $page->metadataOnly();
34 foreach ($this->tags as $tag) {
39 public function children(): array
46 usort($children, function ($a, $b) {
47 return ($a->priority ?? 0) - ($b->priority ?? 0);
53 public static function fromModel(Book $model, ZipExportFiles $files): self
55 $instance = new self();
56 $instance->id = $model->id;
57 $instance->name = $model->name;
58 $instance->description_html = $model->descriptionHtml();
61 $instance->cover = $files->referenceForImage($model->cover);
64 $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
69 $children = $model->getDirectVisibleChildren()->all();
70 foreach ($children as $child) {
71 if ($child instanceof Chapter) {
73 } else if ($child instanceof Page && !$child->draft) {
78 $instance->pages = ZipExportPage::fromModelArray($pages, $files);
79 $instance->chapters = ZipExportChapter::fromModelArray($chapters, $files);
84 public static function validate(ZipValidationHelper $context, array $data): array
87 'id' => ['nullable', 'int', $context->uniqueIdRule('book')],
88 'name' => ['required', 'string', 'min:1'],
89 'description_html' => ['nullable', 'string'],
90 'cover' => ['nullable', 'string', $context->fileReferenceRule()],
93 'chapters' => ['array'],
96 $errors = $context->validateData($data, $rules);
97 $errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
98 $errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
99 $errors['chapters'] = $context->validateRelations($data['chapters'] ?? [], ZipExportChapter::class);
104 public static function fromArray(array $data): self
108 $model->id = $data['id'] ?? null;
109 $model->name = $data['name'];
110 $model->description_html = $data['description_html'] ?? null;
111 $model->cover = $data['cover'] ?? null;
112 $model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
113 $model->pages = ZipExportPage::fromManyArray($data['pages'] ?? []);
114 $model->chapters = ZipExportChapter::fromManyArray($data['chapters'] ?? []);