]> BookStack Code Mirror - bookstack/blob - app/Activity/WatchLevels.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Activity / WatchLevels.php
1 <?php
2
3 namespace BookStack\Activity;
4
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
8
9 class WatchLevels
10 {
11     /**
12      * Default level, No specific option set
13      * Typically not a stored status
14      */
15     const DEFAULT = -1;
16
17     /**
18      * Ignore all notifications.
19      */
20     const IGNORE = 0;
21
22     /**
23      * Watch for new content.
24      */
25     const NEW = 1;
26
27     /**
28      * Watch for updates and new content
29      */
30     const UPDATES = 2;
31
32     /**
33      * Watch for comments, updates and new content.
34      */
35     const COMMENTS = 3;
36
37     /**
38      * Get all the possible values as an option_name => value array.
39      * @returns array<string, int>
40      */
41     public static function all(): array
42     {
43         $options = [];
44         foreach ((new \ReflectionClass(static::class))->getConstants() as $name => $value) {
45             $options[strtolower($name)] = $value;
46         }
47
48         return $options;
49     }
50
51     /**
52      * Get the watch options suited for the given entity.
53      * @returns array<string, int>
54      */
55     public static function allSuitedFor(Entity $entity): array
56     {
57         $options = static::all();
58
59         if ($entity instanceof Page) {
60             unset($options['new']);
61         } elseif ($entity instanceof Bookshelf) {
62             return [];
63         }
64
65         return $options;
66     }
67
68     /**
69      * Convert the given name to a level value.
70      * Defaults to default value if the level does not exist.
71      */
72     public static function levelNameToValue(string $level): int
73     {
74         return static::all()[$level] ?? static::DEFAULT;
75     }
76
77     /**
78      * Convert the given int level value to a level name.
79      * Defaults to 'default' level name if not existing.
80      */
81     public static function levelValueToName(int $level): string
82     {
83         foreach (static::all() as $name => $value) {
84             if ($level === $value) {
85                 return $name;
86             }
87         }
88
89         return 'default';
90     }
91 }