1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Sketch\Tpl; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class IfTag |
7
|
|
|
* @package Sketch\Tpl |
8
|
|
|
*/ |
9
|
|
|
class IfTag extends Tag |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
use ElseView; |
12
|
|
|
use ElseifView; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $block = ''; |
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $firstCondition = ''; |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $secondCondition = ''; |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $operator = ''; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $ifContent = ''; |
34
|
|
|
/** |
35
|
|
|
* @var |
36
|
|
|
*/ |
37
|
|
|
private $replace; |
38
|
|
|
|
39
|
10 |
|
public function __construct() |
40
|
|
|
{ |
41
|
10 |
|
parent::__construct('/{\s?if (.*?)\s?([><!=])(=)?(=)?\s?(.*?)\s?}(.*?){\s?\/if\s?}/is'); |
42
|
10 |
|
} |
43
|
|
|
|
44
|
8 |
|
public function handle(array $match): string |
45
|
|
|
{ |
46
|
8 |
|
$this->setIfOperator(); |
47
|
8 |
|
$this->setIfFirstCondition(); |
48
|
8 |
|
$this->setIfSecondCondition(); |
49
|
8 |
|
$this->setIfContents(); |
50
|
|
|
|
51
|
8 |
|
$this->if(); |
52
|
8 |
|
$this->elseif(); |
53
|
8 |
|
$this->else(); |
54
|
8 |
|
$this->endif(); |
55
|
|
|
|
56
|
8 |
|
$this->elseif = []; |
57
|
|
|
|
58
|
8 |
|
return $this->replace; |
59
|
|
|
} |
60
|
|
|
|
61
|
8 |
|
private function setIfOperator() : void |
62
|
|
|
{ |
63
|
8 |
|
$this->operator = implode('', array_slice($this->match, 2, 3)); |
64
|
8 |
|
} |
65
|
|
|
|
66
|
8 |
|
private function setIfFirstCondition() : void |
67
|
|
|
{ |
68
|
8 |
|
$this->firstCondition = $this->setCondition($this->match[1]); |
69
|
8 |
|
} |
70
|
|
|
|
71
|
8 |
|
private function setIfSecondCondition() : void |
72
|
|
|
{ |
73
|
8 |
|
$this->secondCondition = $this->setCondition($this->match[5]); |
74
|
8 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $condition |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
8 |
|
private function setCondition($condition) : string |
81
|
|
|
{ |
82
|
8 |
|
if ($this->isVariable($condition)) { |
83
|
8 |
|
$explode = explode('.', $condition); |
84
|
|
|
|
85
|
8 |
|
$condition = '$'.$explode[0]; |
86
|
|
|
|
87
|
8 |
|
for ($i = 1; $i < count($explode); $i++) { |
|
|
|
|
88
|
2 |
|
$condition .= "->".$explode[$i]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
8 |
|
return $condition; |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
private function setIfContents() |
96
|
|
|
{ |
97
|
8 |
|
$this->setElseContent(); |
98
|
8 |
|
$this->setElseifContent(); |
99
|
8 |
|
} |
100
|
|
|
|
101
|
8 |
|
private function if(): void |
102
|
|
|
{ |
103
|
8 |
|
$this->replace = "<?php if({$this->firstCondition} {$this->operator} {$this->secondCondition}): ?>"; |
104
|
8 |
|
$this->replace .= trim($this->ifContent); |
105
|
8 |
|
} |
106
|
|
|
|
107
|
8 |
|
private function endif(): void |
108
|
|
|
{ |
109
|
8 |
|
$this->replace .= "<?php endif; ?>"; |
110
|
8 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $term |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
8 |
|
private function isVariable($term) |
117
|
|
|
{ |
118
|
8 |
|
return (is_numeric($term) |
119
|
8 |
|
|| $this->isStringValue((string) $term) |
120
|
8 |
|
|| $this->isReservedKey($term)) ? false : true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $key |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
8 |
|
private function isReservedKey($key) |
128
|
|
|
{ |
129
|
8 |
|
return ($key == "null" || $key == "true" || $key == "false") ? true : false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $string |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
8 |
|
private function isStringValue(string $string) |
137
|
|
|
{ |
138
|
8 |
|
return (substr($string, 0, 1) == '"' || substr($string, 0, 1) == "'") ? true : false; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.