3 namespace BookStack\Exports\ZipExports\Models;
5 use BookStack\Exports\ZipExports\ZipExportFiles;
6 use BookStack\Exports\ZipExports\ZipValidationHelper;
7 use BookStack\Uploads\Attachment;
9 class ZipExportAttachment extends ZipExportModel
11 public ?int $id = null;
13 public ?string $link = null;
14 public ?string $file = null;
16 public function metadataOnly(): void
18 $this->link = $this->file = null;
21 public static function fromModel(Attachment $model, ZipExportFiles $files): self
23 $instance = new self();
24 $instance->id = $model->id;
25 $instance->name = $model->name;
27 if ($model->external) {
28 $instance->link = $model->path;
30 $instance->file = $files->referenceForAttachment($model);
36 public static function fromModelArray(array $attachmentArray, ZipExportFiles $files): array
38 return array_values(array_map(function (Attachment $attachment) use ($files) {
39 return self::fromModel($attachment, $files);
40 }, $attachmentArray));
43 public static function validate(ZipValidationHelper $context, array $data): array
46 'id' => ['nullable', 'int', $context->uniqueIdRule('attachment')],
47 'name' => ['required', 'string', 'min:1'],
48 'link' => ['required_without:file', 'nullable', 'string'],
49 'file' => ['required_without:link', 'nullable', 'string', $context->fileReferenceRule()],
52 return $context->validateData($data, $rules);
55 public static function fromArray(array $data): self
59 $model->id = $data['id'] ?? null;
60 $model->name = $data['name'];
61 $model->link = $data['link'] ?? null;
62 $model->file = $data['file'] ?? null;