]> BookStack Code Mirror - bookstack/blob - app/Sorting/SortSet.php
Sorting: Connected up default sort setting for books
[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\Model;
9 use Illuminate\Database\Eloquent\Relations\HasMany;
10
11 /**
12  * @property int $id
13  * @property string $name
14  * @property string $sequence
15  * @property Carbon $created_at
16  * @property Carbon $updated_at
17  */
18 class SortSet extends Model implements Loggable
19 {
20     /**
21      * @return SortSetOperation[]
22      */
23     public function getOperations(): array
24     {
25         return SortSetOperation::fromSequence($this->sequence);
26     }
27
28     /**
29      * @param SortSetOperation[] $options
30      */
31     public function setOperations(array $options): void
32     {
33         $values = array_map(fn (SortSetOperation $opt) => $opt->value, $options);
34         $this->sequence = implode(',', $values);
35     }
36
37     public function logDescriptor(): string
38     {
39         return "({$this->id}) {$this->name}";
40     }
41
42     public function getUrl(): string
43     {
44         return url("/settings/sorting/sets/{$this->id}");
45     }
46
47     public function books(): HasMany
48     {
49         return $this->hasMany(Book::class);
50     }
51 }