mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-14 15:13:37 +08:00
c4ec50d437
Some checks failed
analyse-php / build (push) Has been cancelled
lint-php / build (push) Has been cancelled
test-migrations / build (8.1) (push) Has been cancelled
test-migrations / build (8.2) (push) Has been cancelled
test-migrations / build (8.3) (push) Has been cancelled
test-php / build (8.1) (push) Has been cancelled
test-php / build (8.2) (push) Has been cancelled
test-php / build (8.3) (push) Has been cancelled
75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Exports\ZipExports\Models;
|
|
|
|
use BookStack\Entities\Models\Book;
|
|
use BookStack\Entities\Models\Chapter;
|
|
use BookStack\Entities\Models\Page;
|
|
use BookStack\Exports\ZipExports\ZipExportFiles;
|
|
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
|
|
|
class ZipExportBook extends ZipExportModel
|
|
{
|
|
public ?int $id = null;
|
|
public string $name;
|
|
public ?string $description_html = null;
|
|
public ?string $cover = null;
|
|
/** @var ZipExportChapter[] */
|
|
public array $chapters = [];
|
|
/** @var ZipExportPage[] */
|
|
public array $pages = [];
|
|
/** @var ZipExportTag[] */
|
|
public array $tags = [];
|
|
|
|
public static function fromModel(Book $model, ZipExportFiles $files): self
|
|
{
|
|
$instance = new self();
|
|
$instance->id = $model->id;
|
|
$instance->name = $model->name;
|
|
$instance->description_html = $model->descriptionHtml();
|
|
|
|
if ($model->cover) {
|
|
$instance->cover = $files->referenceForImage($model->cover);
|
|
}
|
|
|
|
$instance->tags = ZipExportTag::fromModelArray($model->tags()->get()->all());
|
|
|
|
$chapters = [];
|
|
$pages = [];
|
|
|
|
$children = $model->getDirectVisibleChildren()->all();
|
|
foreach ($children as $child) {
|
|
if ($child instanceof Chapter) {
|
|
$chapters[] = $child;
|
|
} else if ($child instanceof Page) {
|
|
$pages[] = $child;
|
|
}
|
|
}
|
|
|
|
$instance->pages = ZipExportPage::fromModelArray($pages, $files);
|
|
$instance->chapters = ZipExportChapter::fromModelArray($chapters, $files);
|
|
|
|
return $instance;
|
|
}
|
|
|
|
public static function validate(ZipValidationHelper $context, array $data): array
|
|
{
|
|
$rules = [
|
|
'id' => ['nullable', 'int'],
|
|
'name' => ['required', 'string', 'min:1'],
|
|
'description_html' => ['nullable', 'string'],
|
|
'cover' => ['nullable', 'string', $context->fileReferenceRule()],
|
|
'tags' => ['array'],
|
|
'pages' => ['array'],
|
|
'chapters' => ['array'],
|
|
];
|
|
|
|
$errors = $context->validateData($data, $rules);
|
|
$errors['tags'] = $context->validateRelations($data['tags'] ?? [], ZipExportTag::class);
|
|
$errors['pages'] = $context->validateRelations($data['pages'] ?? [], ZipExportPage::class);
|
|
$errors['chapters'] = $context->validateRelations($data['chapters'] ?? [], ZipExportChapter::class);
|
|
|
|
return $errors;
|
|
}
|
|
}
|