]> BookStack Code Mirror - bookstack/blob - app/Exports/ZipExports/Models/ZipExportTag.php
b6c9e338aef20e081580148a613f67c833412273
[bookstack] / app / Exports / ZipExports / Models / ZipExportTag.php
1 <?php
2
3 namespace BookStack\Exports\ZipExports\Models;
4
5 use BookStack\Activity\Models\Tag;
6 use BookStack\Exports\ZipExports\ZipValidationHelper;
7
8 class ZipExportTag extends ZipExportModel
9 {
10     public string $name;
11     public ?string $value = null;
12     public ?int $order = null;
13
14     public function metadataOnly(): void
15     {
16         $this->value = $this->order = null;
17     }
18
19     public static function fromModel(Tag $model): self
20     {
21         $instance = new self();
22         $instance->name = $model->name;
23         $instance->value = $model->value;
24         $instance->order = $model->order;
25
26         return $instance;
27     }
28
29     public static function fromModelArray(array $tagArray): array
30     {
31         return array_values(array_map(self::fromModel(...), $tagArray));
32     }
33
34     public static function validate(ZipValidationHelper $context, array $data): array
35     {
36         $rules = [
37             'name'  => ['required', 'string', 'min:1'],
38             'value' => ['nullable', 'string'],
39             'order' => ['nullable', 'integer'],
40         ];
41
42         return $context->validateData($data, $rules);
43     }
44
45     public static function fromArray(array $data): self
46     {
47         $model = new self();
48
49         $model->name = $data['name'];
50         $model->value = $data['value'] ?? null;
51         $model->order = isset($data['order']) ? intval($data['order']) : null;
52
53         return $model;
54     }
55 }