1 | <?php |
||
2 | |||
3 | namespace Imanghafoori\Widgets\Utils\Normalizers; |
||
4 | |||
5 | use Imanghafoori\Widgets\Utils\NormalizerContract; |
||
6 | |||
7 | class CacheTagsNormalizer implements NormalizerContract |
||
8 | { |
||
9 | /** |
||
10 | * ّFigures out what the cache tags should be. |
||
11 | * @param object $widget |
||
12 | * @return void |
||
13 | */ |
||
14 | 16 | public function normalize($widget): void |
|
15 | { |
||
16 | 16 | if (! property_exists($widget, 'cacheTags')) { |
|
17 | 15 | $widget->cacheTags = []; |
|
18 | 1 | } elseif (is_array($widget->cacheTags)) { |
|
19 | 1 | $this->checkTagForString($widget); |
|
20 | } else { |
||
21 | $this->errorOut($widget); |
||
22 | } |
||
23 | 16 | } |
|
24 | |||
25 | /** |
||
26 | * @param $widget |
||
27 | * @param null $tag |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
28 | */ |
||
29 | private function errorOut($widget, $tag = null): void |
||
30 | { |
||
31 | $tag = $tag ? ' '.$tag.'is not string' : ''; |
||
0 ignored issues
–
show
|
|||
32 | throw new \InvalidArgumentException('Cache Tags on "'.get_class($widget).'" must be of type Array with String elements.'.$tag); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param $widget |
||
37 | */ |
||
38 | 1 | protected function checkTagForString($widget): void |
|
39 | { |
||
40 | 1 | foreach ($widget->cacheTags as $tag) { |
|
41 | 1 | if (! is_string($tag)) { |
|
42 | 1 | $this->errorOut($widget); |
|
43 | } |
||
44 | } |
||
45 | 1 | } |
|
46 | } |
||
47 |