1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Bonfim\Component\View; |
4
|
|
|
|
5
|
|
|
class IfTpl |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
private $pattern = '/{\s?if (.*?)\s?([><!=])(=)?(=)?\s?(.*?)\s?}(.*?){\s?\/if\s?}/is'; |
8
|
|
|
private $block = ''; |
9
|
|
|
private $firstCondition = ''; |
10
|
|
|
private $secondCondition = ''; |
11
|
|
|
private $operator = ''; |
12
|
|
|
private $elseif = array(); |
13
|
|
|
private $ifConten = ''; |
14
|
|
|
private $elseContent = ''; |
15
|
|
|
private $content; |
16
|
|
|
private $match; |
17
|
|
|
|
18
|
|
|
public function __construct(string $content) |
19
|
|
|
{ |
20
|
|
|
$this->content = $content; |
21
|
|
|
$this->if(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function __toString(): string |
25
|
|
|
{ |
26
|
|
|
return $this->content; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function if() : void |
30
|
|
|
{ |
31
|
|
|
if (preg_match_all($this->pattern, $this->content, $matches, PREG_SET_ORDER)) { |
32
|
|
|
for ($i = 0; $i < count($matches); $i++) { |
|
|
|
|
33
|
|
|
$this->match = $matches[$i]; |
34
|
|
|
|
35
|
|
|
$this->setIfBlock(); |
36
|
|
|
$this->setIfOperator(); |
37
|
|
|
$this->setIfFirstCondition(); |
38
|
|
|
$this->setIfSecondCondition(); |
39
|
|
|
$this->setIfContents(); |
40
|
|
|
|
41
|
|
|
$content = "<?php if({$this->firstCondition} {$this->operator} {$this->secondCondition}): ?>"; |
42
|
|
|
$content .= trim($this->ifConten); |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
if (count($this->elseif) != 0) { |
46
|
|
|
for ($k = 0; $k < count($this->elseif); $k++) { |
|
|
|
|
47
|
|
|
$content .= "<?php elseif({$this->elseif[$k]['firstCondition']} {$this->elseif[$k]['operator']} {$this->elseif[$k]['secondCondition']}): ?>"; |
48
|
|
|
$content .= trim($this->elseif[$k]['content']); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!empty($this->elseContent)) { |
53
|
|
|
$content .= "<?php else: ?>"; |
54
|
|
|
$content .= trim($this->elseContent); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$content .= "<?php endif; ?>"; |
58
|
|
|
|
59
|
|
|
$this->content = str_replace($this->block, $content, $this->content); |
60
|
|
|
} |
61
|
|
|
$this->if(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function setIfBlock() : void |
66
|
|
|
{ |
67
|
|
|
$this->block = $this->match[0]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function setIfOperator() : void |
71
|
|
|
{ |
72
|
|
|
$this->operator = implode('', array_slice($this->match, 2, 3)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function setIfFirstCondition() : void |
76
|
|
|
{ |
77
|
|
|
$this->firstCondition = $this->setCondition($this->match[1]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function setIfSecondCondition() : void |
81
|
|
|
{ |
82
|
|
|
$this->secondCondition = $this->setCondition($this->match[5]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function setCondition($condition) : string |
86
|
|
|
{ |
87
|
|
|
if ($this->isVariable($condition)) { |
88
|
|
|
$explode = explode('.', $condition); |
89
|
|
|
|
90
|
|
|
$condition = '$'.$explode[0]; |
91
|
|
|
|
92
|
|
|
for ($i = 1; $i < count($explode); $i++) { |
|
|
|
|
93
|
|
|
$condition .= "['".$explode[$i]."']"; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $condition; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function setIfContents() |
101
|
|
|
{ |
102
|
|
|
$this->setElseContent(); |
103
|
|
|
$this->setElseifContent(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function setElseContent() |
107
|
|
|
{ |
108
|
|
|
$matches = array(); |
109
|
|
|
if (preg_match('/(.*?){\s?else\s?}/is', $this->match[6], $matches)) { |
110
|
|
|
$this->ifConten = $matches[1]; |
111
|
|
|
$this->elseContent = str_replace($matches[0], '', $this->match[6]); |
112
|
|
|
} else { |
113
|
|
|
$this->ifConten = $this->match[6]; |
114
|
|
|
$this->elseContent = ''; |
115
|
|
|
} |
116
|
|
|
$this->ifConten = str_replace('{elseif', '{end}{elseif', $this->ifConten); |
117
|
|
|
$this->ifConten = $this->ifConten . "{end}"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function setElseifContent() |
121
|
|
|
{ |
122
|
|
|
$matches = array(); |
123
|
|
|
if (preg_match_all('/{\s?elseif (.*?)\s?([><!=])(=)?(=)?\s?(.*?)\s?}(.*?){end}/is', $this->ifConten, $matches, PREG_SET_ORDER)) { |
124
|
|
|
for ($i = 0; $i < count($matches); $i++) { |
|
|
|
|
125
|
|
|
$this->ifConten = str_replace($matches[$i][0], '', $this->ifConten); |
126
|
|
|
$this->elseif[$i]['firstCondition'] = $this->setCondition($matches[$i][1]); |
127
|
|
|
$this->elseif[$i]['secondCondition'] = $this->setCondition($matches[$i][5]); |
128
|
|
|
$this->elseif[$i]['operator'] = implode('', array_slice($matches[$i], 2, 3)); |
129
|
|
|
$this->elseif[$i]['content'] = trim($matches[$i][6]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
$this->ifConten = str_replace('{end}', '', $this->ifConten); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function isVariable(mixed $term) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
return (is_numeric($term) || $this->isStringValue((string) $term) || $this->isReservedKey($term)) ? false : true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function isReservedKey(mixed $key) |
141
|
|
|
{ |
142
|
|
|
return ($key == "null" || $key == "true" || $key == "false") ? true : false; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function isStringValue(string $string) |
146
|
|
|
{ |
147
|
|
|
return (substr($string, 0, 1) == '"' || substr($string, 0, 1) == "'") ? true : false; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.