]> BookStack Code Mirror - bookstack/blob - app/Sorting/SortRule.php
Merge pull request #5626 from BookStackApp/rubentalstra-development
[bookstack] / app / Sorting / SortRule.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\Factories\HasFactory;
10 use Illuminate\Database\Eloquent\Model;
11 use Illuminate\Database\Eloquent\Relations\HasMany;
12
13 /**
14  * @property int $id
15  * @property string $name
16  * @property string $sequence
17  * @property Carbon $created_at
18  * @property Carbon $updated_at
19  */
20 class SortRule extends Model implements Loggable
21 {
22     use HasFactory;
23
24     /**
25      * @return SortRuleOperation[]
26      */
27     public function getOperations(): array
28     {
29         return SortRuleOperation::fromSequence($this->sequence);
30     }
31
32     /**
33      * @param SortRuleOperation[] $options
34      */
35     public function setOperations(array $options): void
36     {
37         $values = array_map(fn (SortRuleOperation $opt) => $opt->value, $options);
38         $this->sequence = implode(',', $values);
39     }
40
41     public function logDescriptor(): string
42     {
43         return "({$this->id}) {$this->name}";
44     }
45
46     public function getUrl(): string
47     {
48         return url("/settings/sorting/rules/{$this->id}");
49     }
50
51     public function books(): HasMany
52     {
53         return $this->hasMany(Book::class);
54     }
55
56     public static function allByName(): Collection
57     {
58         return static::query()
59             ->withCount('books')
60             ->orderBy('name', 'asc')
61             ->get();
62     }
63 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.