2024-10-21 02:56:56 +08:00
|
|
|
<?php
|
|
|
|
|
2024-10-23 17:48:26 +08:00
|
|
|
namespace BookStack\Exports\ZipExports\Models;
|
2024-10-21 02:56:56 +08:00
|
|
|
|
2024-10-23 17:48:26 +08:00
|
|
|
use BookStack\Exports\ZipExports\ZipExportFiles;
|
2024-10-30 21:13:41 +08:00
|
|
|
use BookStack\Exports\ZipExports\ZipValidationHelper;
|
2024-10-21 02:56:56 +08:00
|
|
|
use BookStack\Uploads\Attachment;
|
|
|
|
|
2024-10-21 19:13:41 +08:00
|
|
|
class ZipExportAttachment extends ZipExportModel
|
2024-10-21 02:56:56 +08:00
|
|
|
{
|
|
|
|
public ?int $id = null;
|
|
|
|
public string $name;
|
|
|
|
public ?int $order = null;
|
|
|
|
public ?string $link = null;
|
|
|
|
public ?string $file = null;
|
|
|
|
|
|
|
|
public static function fromModel(Attachment $model, ZipExportFiles $files): self
|
|
|
|
{
|
|
|
|
$instance = new self();
|
|
|
|
$instance->id = $model->id;
|
|
|
|
$instance->name = $model->name;
|
2024-10-27 22:33:43 +08:00
|
|
|
$instance->order = $model->order;
|
2024-10-21 02:56:56 +08:00
|
|
|
|
|
|
|
if ($model->external) {
|
|
|
|
$instance->link = $model->path;
|
|
|
|
} else {
|
|
|
|
$instance->file = $files->referenceForAttachment($model);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromModelArray(array $attachmentArray, ZipExportFiles $files): array
|
|
|
|
{
|
|
|
|
return array_values(array_map(function (Attachment $attachment) use ($files) {
|
|
|
|
return self::fromModel($attachment, $files);
|
|
|
|
}, $attachmentArray));
|
|
|
|
}
|
2024-10-30 21:13:41 +08:00
|
|
|
|
|
|
|
public static function validate(ZipValidationHelper $context, array $data): array
|
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'id' => ['nullable', 'int'],
|
|
|
|
'name' => ['required', 'string', 'min:1'],
|
|
|
|
'order' => ['nullable', 'integer'],
|
|
|
|
'link' => ['required_without:file', 'nullable', 'string'],
|
|
|
|
'file' => ['required_without:link', 'nullable', 'string', $context->fileReferenceRule()],
|
|
|
|
];
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
return $context->validateData($data, $rules);
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
2024-10-21 02:56:56 +08:00
|
|
|
}
|