1 | <?php |
||
20 | class Active implements ActiveContract |
||
21 | { |
||
22 | /* ----------------------------------------------------------------- |
||
23 | | Properties |
||
24 | | ----------------------------------------------------------------- |
||
25 | */ |
||
26 | |||
27 | /** @var string */ |
||
28 | protected $activeClass; |
||
29 | |||
30 | /** @var string */ |
||
31 | protected $fallbackClass; |
||
32 | |||
33 | /** @var \Illuminate\Http\Request */ |
||
34 | protected $request; |
||
35 | |||
36 | /* ----------------------------------------------------------------- |
||
37 | | Constructor |
||
38 | | ----------------------------------------------------------------- |
||
39 | */ |
||
40 | |||
41 | /** |
||
42 | * Active constructor. |
||
43 | * |
||
44 | * @param array $options |
||
45 | */ |
||
46 | 138 | public function __construct(array $options) |
|
51 | |||
52 | /* ----------------------------------------------------------------- |
||
53 | | Getters & Setters |
||
54 | | ----------------------------------------------------------------- |
||
55 | */ |
||
56 | |||
57 | /** |
||
58 | * Get the `active` CSS class. |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | 24 | public function getActiveClass(): string |
|
66 | |||
67 | /** |
||
68 | * Set the `active` CSS class. |
||
69 | * |
||
70 | * @param string $class |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | 138 | public function setActiveClass(string $class) |
|
80 | |||
81 | /** |
||
82 | * Get the fallback (inactive) class. |
||
83 | * |
||
84 | * @return string|null |
||
85 | */ |
||
86 | 36 | public function getFallbackClass(): ?string |
|
90 | |||
91 | /** |
||
92 | * Set the fallback (inactive) class. |
||
93 | * |
||
94 | * @param string|null $class |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | 138 | public function setFallbackClass(?string $class) |
|
104 | |||
105 | /** |
||
106 | * Get the request. |
||
107 | * |
||
108 | * @return \Illuminate\Http\Request |
||
109 | */ |
||
110 | 126 | public function getRequest(): Request |
|
114 | |||
115 | /** |
||
116 | * Set the request. |
||
117 | * |
||
118 | * @param \Illuminate\Http\Request $request |
||
119 | * |
||
120 | * @return $this |
||
121 | */ |
||
122 | 6 | public function setRequest(Request $request) |
|
128 | |||
129 | /* ----------------------------------------------------------------- |
||
130 | | Main Methods |
||
131 | | ----------------------------------------------------------------- |
||
132 | */ |
||
133 | |||
134 | /** |
||
135 | * Get the active class if the current path/route is active. |
||
136 | * |
||
137 | * @param string|array $routes |
||
138 | * @param string|null $class |
||
139 | * @param string|null $fallback |
||
140 | * |
||
141 | * @return string|null |
||
142 | */ |
||
143 | 48 | public function active($routes, $class = null, $fallback = null) |
|
149 | |||
150 | /** |
||
151 | * Get the active class if the current route is in haystack routes. |
||
152 | * |
||
153 | * @param string|array $routes |
||
154 | * @param string|null $class |
||
155 | * @param string|null $fallback |
||
156 | * |
||
157 | * @return string|null |
||
158 | */ |
||
159 | 36 | public function route($routes, $class = null, $fallback = null) |
|
165 | |||
166 | /** |
||
167 | * Get the active class if the current path is in haystack paths. |
||
168 | * |
||
169 | * @param string|array $routes |
||
170 | * @param string|null $class |
||
171 | * @param string|null $fallback |
||
172 | * |
||
173 | * @return string|null |
||
174 | */ |
||
175 | 36 | public function path($routes, $class = null, $fallback = null) |
|
181 | |||
182 | /** |
||
183 | * Check if any given routes/paths are active. |
||
184 | * |
||
185 | * @param string|array $routes |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | 126 | public function is($routes): bool |
|
194 | |||
195 | /** |
||
196 | * Check if the current route is one of the given routes. |
||
197 | * |
||
198 | * @param string|array $routes |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | 120 | public function isRoute($routes): bool |
|
212 | |||
213 | /** |
||
214 | * Check if the current path is active. |
||
215 | * |
||
216 | * @param string|array $routes |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | 126 | public function isPath($routes): bool |
|
230 | |||
231 | /* ----------------------------------------------------------------- |
||
232 | | Other Methods |
||
233 | | ----------------------------------------------------------------- |
||
234 | */ |
||
235 | |||
236 | /** |
||
237 | * Get the css class based on the given active state. |
||
238 | * |
||
239 | * @param bool $isActive |
||
240 | * @param string|null $class |
||
241 | * @param string|null $fallback |
||
242 | * |
||
243 | * @return string|null |
||
244 | */ |
||
245 | 48 | protected function getCssClass(bool $isActive, ?string $class = null, ?string $fallback = null): ?string |
|
251 | |||
252 | /** |
||
253 | * Check if the given routes/paths are ignored. |
||
254 | * |
||
255 | * @param array $ignored |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | 126 | protected function isIgnored(array $ignored): bool |
|
264 | |||
265 | /** |
||
266 | * Separate ignored routes from the whitelist routes. |
||
267 | * |
||
268 | * @param array $allRoutes |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | 126 | protected function parseRoutes(array $allRoutes): array |
|
289 | } |
||
290 |