1 | <?php |
||
34 | trait ReferenceTrait |
||
35 | { |
||
36 | use LocalCacheTrait; |
||
37 | |||
38 | /** |
||
39 | * refernece start chars |
||
40 | * |
||
41 | * @var string |
||
42 | * @access private |
||
43 | */ |
||
44 | private $ref_start = '${'; |
||
45 | |||
46 | /** |
||
47 | * reference ending chars |
||
48 | * |
||
49 | * @var string |
||
50 | * @access private |
||
51 | */ |
||
52 | private $ref_end = '}'; |
||
53 | |||
54 | /** |
||
55 | * cached pattern to match |
||
56 | * |
||
57 | * @var string |
||
58 | * @access private |
||
59 | */ |
||
60 | private $ref_pattern = '~(\$\{((?:(?!\$\{|\}).)+?)\})~'; |
||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | public function setReferencePattern( |
||
66 | /*# string */ $start, |
||
67 | /*# string */ $end |
||
68 | ) { |
||
69 | $this->ref_start = $start; |
||
70 | $this->ref_end = $end; |
||
71 | |||
72 | // build pattern on the fly |
||
73 | $s = preg_quote($start); |
||
74 | $e = preg_quote($end); |
||
75 | $this->ref_pattern = sprintf( |
||
76 | "~(%s((?:(?!%s|%s).)+?)%s)~", $s, $s, $e, $e |
||
77 | ); |
||
78 | |||
79 | return $this->clearLocalCache(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | public function hasReference( |
||
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | public function deReference(/*# string */ $subject) |
||
125 | |||
126 | /** |
||
127 | * {@inheritDoc} |
||
128 | */ |
||
129 | public function deReferenceArray(&$dataArray) |
||
143 | |||
144 | /** |
||
145 | * Check dereferenced value |
||
146 | * |
||
147 | * @param mixed $value |
||
148 | * @param string $subject the subject to dereference |
||
149 | * @param string $reference the matched whole reference |
||
150 | * @return mixed |
||
151 | * @throws RuntimeException if $subject malformed, like mix string & array |
||
152 | * @access private |
||
153 | */ |
||
154 | private function checkValue( |
||
176 | |||
177 | /** |
||
178 | * Resolve the reference $name |
||
179 | * |
||
180 | * @param string $name |
||
181 | * @return mixed |
||
182 | * @throws RuntimeException if reference unknown |
||
183 | * @access private |
||
184 | * @since 2.0.6 added cache support |
||
185 | * @since 2.0.8 added delegatorAware support |
||
186 | */ |
||
187 | private function resolveReference(/*# string */ $name) |
||
207 | |||
208 | /** |
||
209 | * Throw exception if looped |
||
210 | * |
||
211 | * @param int $loop loop counter |
||
212 | * @param string $name reference name |
||
213 | * @throws RuntimeException if loop found |
||
214 | * @access private |
||
215 | * @since 2.0.6 |
||
216 | */ |
||
217 | private function checkReferenceLoop( |
||
228 | |||
229 | /** |
||
230 | * Lookup reference with delegator or self |
||
231 | * |
||
232 | * @param string $name |
||
233 | * @return mixed |
||
234 | * @access private |
||
235 | */ |
||
236 | private function referenceLookup(/*# string */ $name) |
||
258 | |||
259 | /** |
||
260 | * For unknown reference $name, normally returns NULL |
||
261 | * |
||
262 | * @param string $name |
||
263 | * @return mixed |
||
264 | * @throws \Exception if implementor WANTS TO !! |
||
265 | * @access protected |
||
266 | */ |
||
267 | abstract protected function resolveUnknown(/*# string */ $name); |
||
268 | |||
269 | /** |
||
270 | * The REAL resolving method. return NULL for unknown reference |
||
271 | * |
||
272 | * @param string $name |
||
273 | * @return mixed |
||
274 | * @access protected |
||
275 | */ |
||
276 | abstract protected function getReference(/*# string */ $name); |
||
277 | } |
||
278 |