1 | <?php |
||
8 | class ChimpDrill |
||
9 | { |
||
10 | /** |
||
11 | * @var array callback => syntax pattern |
||
12 | */ |
||
13 | protected $pattern = array( |
||
14 | 'placeholder' => '/\*\|([A-Za-z0-9_]+)\|\*/', |
||
15 | 'if' => '/\*\|(IF|IFNOT|ELSEIF):([A-Za-z0-9_]+)(?:[\s]*(=|!=|>=|<=|>|<)[\s]*(.+?))?\|\*/', |
||
16 | 'else' => '/\*\|ELSE:\|\*/', |
||
17 | 'endif' => '/\*\|END:IF\|\*/', |
||
18 | 'filter' => '/\*\|(HTML|TITLE|LOWER|UPPER):([A-Za-z0-9_]+)\|\*/', |
||
19 | 'date' => '/\*\|DATE:(.+?)\|\*/' |
||
20 | ); |
||
21 | |||
22 | /** |
||
23 | * @var string parsed message |
||
24 | */ |
||
25 | protected $parsed = null; |
||
26 | |||
27 | /** |
||
28 | * @var string message |
||
29 | */ |
||
30 | protected $message; |
||
31 | |||
32 | /** |
||
33 | * @var array placeholder |
||
34 | */ |
||
35 | protected $placeholder; |
||
36 | |||
37 | /** |
||
38 | * @param string $message Message to parse |
||
39 | * @param array $placeholder Placeholder |
||
40 | */ |
||
41 | public function __construct($message, array $placeholder) |
||
46 | |||
47 | /** |
||
48 | * @return string |
||
49 | */ |
||
50 | public function __toString() |
||
54 | |||
55 | /** |
||
56 | * Returns parsed message. |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getParsed() |
||
66 | |||
67 | /** |
||
68 | * Parse the message (If this haven't be done yet). |
||
69 | * |
||
70 | * @return ChimpDrill |
||
71 | */ |
||
72 | protected function parseMessage() |
||
96 | |||
97 | /** |
||
98 | * Searches for a placeholder and returns the found or default value. |
||
99 | * |
||
100 | * @param string $name |
||
101 | * @param mixed $default |
||
102 | * |
||
103 | * @return mixed |
||
104 | */ |
||
105 | protected function getPlaceholder($name, $default = null) |
||
109 | |||
110 | /** |
||
111 | * @param mixed $value |
||
112 | * |
||
113 | * @return mixed |
||
114 | */ |
||
115 | protected function exportValue($value) |
||
119 | |||
120 | /** |
||
121 | * Escape an string. |
||
122 | * |
||
123 | * @param string $value |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function escapeValue($value) |
||
131 | |||
132 | /** |
||
133 | * Rolls back escaping. |
||
134 | * |
||
135 | * @param string $value |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | protected function unescapeValue($value) |
||
143 | |||
144 | /** |
||
145 | * Compares two values with an operator. |
||
146 | * |
||
147 | * @param mixed $val1 |
||
148 | * @param string $operator |
||
149 | * @param mixed $val2 |
||
150 | * |
||
151 | * @return boolean |
||
152 | */ |
||
153 | protected function compare($val1, $operator, $val2) |
||
175 | |||
176 | /** |
||
177 | * Parses placeholder merge tags. |
||
178 | * |
||
179 | * @param array $match |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | protected function parsePlaceholder(array $match) |
||
192 | |||
193 | /** |
||
194 | * Parses `IF|ELSEIF|IFNOT` conditional merge tags. |
||
195 | * |
||
196 | * @param array $match |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | protected function parseIf(array $match) |
||
221 | |||
222 | /** |
||
223 | * Parses `ELSE` conditional merge tags. |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | protected function parseElse() |
||
231 | |||
232 | /** |
||
233 | * Parses `ENDIF` conditional merge tags. |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | protected function parseEndif() |
||
241 | |||
242 | /** |
||
243 | * Parses `HTML|TITLE|LOWER|UPPER` filter merge tags. |
||
244 | * |
||
245 | * @param array $match |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | protected function parseFilter(array $match) |
||
267 | |||
268 | /** |
||
269 | * Parses date merge tags. |
||
270 | * |
||
271 | * @param array $match |
||
272 | * |
||
273 | * @return bool|string |
||
274 | */ |
||
275 | protected function parseDate(array $match) |
||
279 | } |
||
280 |