1 | <?php |
||
27 | class SettingsManager extends Collection |
||
28 | { |
||
29 | 63 | public function __construct($items = []) |
|
30 | { |
||
31 | 63 | parent::__construct($items); |
|
32 | 63 | $this->load(); |
|
33 | 63 | } |
|
34 | |||
35 | /** |
||
36 | * Load all settings. |
||
37 | * |
||
38 | * @return \Illuminate\Database\Eloquent\Collection|bool |
||
39 | */ |
||
40 | 73 | protected function load() |
|
41 | { |
||
42 | 73 | if ($this->count() === 0) { |
|
43 | // Skip exception if table does not exists |
||
44 | // This method called from RouteServiceProvider, which is called within command line 'artisan migrate' |
||
45 | // before the table exists. |
||
46 | try { |
||
47 | 64 | $items = Setting::all(); |
|
48 | 64 | $this->items = is_array($items) ? $items : $this->getArrayableItems($items); |
|
49 | } catch (\Exception $e) { |
||
50 | return false; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | 73 | return $this; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Returns a setting value. |
||
59 | * |
||
60 | * @param string $name |
||
61 | * @param mixed|null $default |
||
62 | * |
||
63 | * @return mixed |
||
64 | */ |
||
65 | 73 | public function get($name, $default = null) |
|
66 | { |
||
67 | 73 | if ($this->load()) { |
|
68 | 73 | foreach ($this->all() as $setting) { |
|
69 | 73 | if ($setting->key === $name) { |
|
70 | 73 | return $setting->value; |
|
71 | } |
||
72 | } |
||
73 | } |
||
74 | |||
75 | 1 | return $default; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Whether or not the public projects enabled. |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | 63 | public function isPublicProjectsEnabled() |
|
84 | { |
||
85 | 63 | return (boolean) $this->get('enable_public_projects') === true; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Returns date format. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 33 | public function getDateFormat() |
|
94 | { |
||
95 | 33 | return (string) $this->get('date_format'); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns first status tag id. |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | 37 | public function getFirstStatusTagId() |
|
107 | |||
108 | /** |
||
109 | * Returns users default language. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 5 | public function getLanguage() |
|
117 | |||
118 | /** |
||
119 | * Save a collection of settings. |
||
120 | * |
||
121 | * @param $values |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | 7 | public function save($values) |
|
143 | } |
||
144 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: