]> BookStack Code Mirror - bookstack/blob - app/Sorting/SortSet.php
8cdee1df49c4e24b276616328a94a1dfae7fda05
[bookstack] / app / Sorting / SortSet.php
1 <?php
2
3 namespace BookStack\Sorting;
4
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Entities\Models\Book;
7 use Carbon\Carbon;
8 use Illuminate\Database\Eloquent\Collection;
9 use Illuminate\Database\Eloquent\Model;
10 use Illuminate\Database\Eloquent\Relations\HasMany;
11
12 /**
13  * @property int $id
14  * @property string $name
15  * @property string $sequence
16  * @property Carbon $created_at
17  * @property Carbon $updated_at
18  */
19 class SortSet extends Model implements Loggable
20 {
21     /**
22      * @return SortSetOperation[]
23      */
24     public function getOperations(): array
25     {
26         return SortSetOperation::fromSequence($this->sequence);
27     }
28
29     /**
30      * @param SortSetOperation[] $options
31      */
32     public function setOperations(array $options): void
33     {
34         $values = array_map(fn (SortSetOperation $opt) => $opt->value, $options);
35         $this->sequence = implode(',', $values);
36     }
37
38     public function logDescriptor(): string
39     {
40         return "({$this->id}) {$this->name}";
41     }
42
43     public function getUrl(): string
44     {
45         return url("/settings/sorting/sets/{$this->id}");
46     }
47
48     public function books(): HasMany
49     {
50         return $this->hasMany(Book::class);
51     }
52
53     public static function allByName(): Collection
54     {
55         return static::query()
56             ->withCount('books')
57             ->orderBy('name', 'asc')
58             ->get();
59     }
60 }