2024-10-23 18:30:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exports\ZipExports\Models;
|
|
|
|
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use BookStack\Exports\ZipExports\ZipExportFiles;
|
2024-10-30 23:26:23 +08:00
|
|
|
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
2024-10-23 18:30:32 +08:00
|
|
|
|
|
|
|
class ZipExportChapter extends ZipExportModel
|
|
|
|
{
|
|
|
|
public ?int $id = null;
|
|
|
|
public string $name;
|
|
|
|
public ?string $description_html = null;
|
|
|
|
public ?int $priority = null;
|
|
|
|
/** @var ZipExportPage[] */
|
|
|
|
public array $pages = [];
|
|
|
|
/** @var ZipExportTag[] */
|
|
|
|
public array $tags = [];
|
|
|
|
|
2024-11-05 21:17:31 +08:00
|
|
|
public function metadataOnly(): void
|
|
|
|
{
|
|
|
|
$this->description_html = $this->priority = null;
|
|
|
|
|
|
|
|
foreach ($this->pages as $page) {
|
|
|
|
$page->metadataOnly();
|
|
|
|
}
|
|
|
|
foreach ($this->tags as $tag) {
|
|
|
|
$tag->metadataOnly();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 18:30:32 +08:00
|
|
|
public static function fromModel(Chapter $model, ZipExportFiles $files): self
|
|
|
|
{
|
|
|
|
$instance = new self();
|
|
|
|
$instance->id = $model->id;
|
|
|
|
$instance->name = $model->name;
|
|
|
|
$instance->description_html = $model->descriptionHtml();
|
|
|
|
$instance->priority = $model->priority;
|
|
|
|
$instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
|
|
|
|
|
|
|
|
$pages = $model->getVisiblePages()->filter(fn (Page $page) => !$page->draft)->all();
|
|
|
|
$instance->pages = ZipExportPage::fromModelArray($pages, $files);
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Chapter[] $chapterArray
|
|
|
|
* @return self[]
|
|
|
|
*/
|
|
|
|
public static function fromModelArray(array $chapterArray, ZipExportFiles $files): array
|
|
|
|
{
|
|
|
|
return array_values(array_map(function (Chapter $chapter) use ($files) {
|
|
|
|
return self::fromModel($chapter, $files);
|
|
|
|
}, $chapterArray));
|
|
|
|
}
|
2024-10-30 23:26:23 +08:00
|
|
|
|
|
|
|
public static function validate(ZipValidationHelper $context, array $data): array
|
|
|
|
{
|
|
|
|
$rules = [
|
2024-11-18 23:53:21 +08:00
|
|
|
'id' => ['nullable', 'int', $context->uniqueIdRule('chapter')],
|
2024-10-30 23:26:23 +08:00
|
|
|
'name' => ['required', 'string', 'min:1'],
|
|
|
|
'description_html' => ['nullable', 'string'],
|
|
|
|
'priority' => ['nullable', 'int'],
|
|
|
|
'tags' => ['array'],
|
|
|
|
'pages' => ['array'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$errors = $context->validateData($data, $rules);
|
|
|
|
$errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
|
|
|
|
$errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
2024-11-05 21:17:31 +08:00
|
|
|
|
|
|
|
public static function fromArray(array $data): self
|
|
|
|
{
|
|
|
|
$model = new self();
|
|
|
|
|
|
|
|
$model->id = $data['id'] ?? null;
|
|
|
|
$model->name = $data['name'];
|
|
|
|
$model->description_html = $data['description_html'] ?? null;
|
|
|
|
$model->priority = isset($data['priority']) ? intval($data['priority']) : null;
|
|
|
|
$model->tags = ZipExportTag::fromManyArray($data['tags'] ?? []);
|
|
|
|
$model->pages = ZipExportPage::fromManyArray($data['pages'] ?? []);
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
2024-10-23 18:30:32 +08:00
|
|
|
}
|