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 static function fromModel(Book $model, ZipExportFiles $files): self
41 $instance = new self();
42 $instance->id = $model->id;
43 $instance->name = $model->name;
44 $instance->description_html = $model->descriptionHtml();
47 $instance->cover = $files->referenceForImage($model->cover);
50 $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
55 $children = $model->getDirectVisibleChildren()->all();
56 foreach ($children as $child) {
57 if ($child instanceof Chapter) {
59 } else if ($child instanceof Page) {
64 $instance->pages = ZipExportPage::fromModelArray($pages, $files);
65 $instance->chapters = ZipExportChapter::fromModelArray($chapters, $files);
70 public static function validate(ZipValidationHelper $context, array $data): array
73 'id' => ['nullable', 'int', $context->uniqueIdRule('book')],
74 'name' => ['required', 'string', 'min:1'],
75 'description_html' => ['nullable', 'string'],
76 'cover' => ['nullable', 'string', $context->fileReferenceRule()],
79 'chapters' => ['array'],
82 $errors = $context->validateData($data, $rules);
83 $errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
84 $errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
85 $errors['chapters'] = $context->validateRelations($data['chapters'] ?? [], ZipExportChapter::class);
90 public static function fromArray(array $data): self
94 $model->id = $data['id'] ?? null;
95 $model->name = $data['name'];
96 $model->description_html = $data['description_html'] ?? null;
97 $model->cover = $data['cover'] ?? null;
98 $model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
99 $model->pages = ZipExportPage::fromManyArray($data['pages'] ?? []);
100 $model->chapters = ZipExportChapter::fromManyArray($data['chapters'] ?? []);