|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\Widgets\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Imanghafoori\Widgets\BaseWidget; |
|
6
|
|
|
use Imanghafoori\Widgets\Utils\Normalizers\CacheNormalizer; |
|
7
|
|
|
use Imanghafoori\Widgets\Utils\Normalizers\ControllerNormalizer; |
|
8
|
|
|
use Imanghafoori\Widgets\Utils\Normalizers\PresenterNormalizer; |
|
9
|
|
|
use Imanghafoori\Widgets\Utils\Normalizers\TemplateNormalizer; |
|
10
|
|
|
|
|
11
|
|
|
class Normalizer |
|
12
|
|
|
{ |
|
13
|
|
|
private $widget; |
|
14
|
|
|
private $presenterNormalizer; |
|
15
|
|
|
private $templateNormalizer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Normalizer constructor. |
|
19
|
|
|
* @param TemplateNormalizer $templateNormalizer |
|
20
|
|
|
* @param CacheNormalizer $cacheNormalizer |
|
21
|
|
|
* @param PresenterNormalizer $presenterNormalizer |
|
22
|
|
|
* @param ControllerNormalizer $controllerNormalizer |
|
23
|
|
|
* @internal param $widget |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(TemplateNormalizer $templateNormalizer, CacheNormalizer $cacheNormalizer, PresenterNormalizer $presenterNormalizer, ControllerNormalizer $controllerNormalizer) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->presenterNormalizer = $presenterNormalizer; |
|
28
|
|
|
$this->controllerNormalizer = $controllerNormalizer; |
|
|
|
|
|
|
29
|
|
|
$this->templateNormalizer = $templateNormalizer; |
|
30
|
|
|
$this->cacheNormalizer = $cacheNormalizer; |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function normalizeWidgetConfig(BaseWidget $widget) |
|
35
|
|
|
{ |
|
36
|
|
|
// to avoid normalizing a widget multiple times unnecessarily : |
|
37
|
|
|
if (isset($widget->isNormalized)) { |
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->widget = $widget; |
|
42
|
|
|
$this->controllerNormalizer->normalizeControllerMethod($widget); |
|
43
|
|
|
$this->presenterNormalizer->normalizePresenterName($widget); |
|
44
|
|
|
$this->templateNormalizer->normalizeTemplateName($widget); |
|
45
|
|
|
$this->cacheNormalizer->normalizeCacheLifeTime($widget); |
|
46
|
|
|
$this->cacheNormalizer->normalizeCacheTags($widget); |
|
47
|
|
|
$this->normalizeContextAs(); |
|
48
|
|
|
$widget->isNormalized = true; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Figures out what the variable name should be in view file. |
|
53
|
|
|
* @return null |
|
54
|
|
|
*/ |
|
55
|
|
|
private function normalizeContextAs() |
|
56
|
|
|
{ |
|
57
|
|
|
// removes the $ sign. |
|
58
|
|
|
$this->widget->contextAs = str_replace('$', '', (string)$this->widget->contextAs); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: