]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportChapter.php
ZIP Exports: Added core logic for books/chapters
[bookstack] / app / Exports / ZipExports / Models / ZipExportChapter.php
1 <?php
2
3 namespace BookStack\Exports\ZipExports\Models;
4
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Exports\ZipExports\ZipExportFiles;
8
9 class ZipExportChapter extends ZipExportModel
10 {
11     public ?int $id = null;
12     public string $name;
13     public ?string $description_html = null;
14     public ?int $priority = null;
15     /** @var ZipExportPage[] */
16     public array $pages = [];
17     /** @var ZipExportTag[] */
18     public array $tags = [];
19
20     public static function fromModel(Chapter $model, ZipExportFiles $files): self
21     {
22         $instance = new self();
23         $instance->id = $model->id;
24         $instance->name = $model->name;
25         $instance->description_html = $model->descriptionHtml();
26         $instance->priority = $model->priority;
27         $instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
28
29         $pages = $model->getVisiblePages()->filter(fn (Page $page) => !$page->draft)->all();
30         $instance->pages = ZipExportPage::fromModelArray($pages, $files);
31
32         return $instance;
33     }
34
35     /**
36      * @param Chapter[] $chapterArray
37      * @return self[]
38      */
39     public static function fromModelArray(array $chapterArray, ZipExportFiles $files): array
40     {
41         return array_values(array_map(function (Chapter $chapter) use ($files) {
42             return self::fromModel($chapter, $files);
43         }, $chapterArray));
44     }
45 }