1 | <?php |
||
28 | trait ReferenceTrait |
||
29 | { |
||
30 | /** |
||
31 | * refernece start chars |
||
32 | * |
||
33 | * @var string |
||
34 | * @access protected |
||
35 | */ |
||
36 | protected $ref_start = '${'; |
||
37 | |||
38 | /** |
||
39 | * reference ending chars |
||
40 | * |
||
41 | * @var string |
||
42 | * @access protected |
||
43 | */ |
||
44 | protected $ref_end = '}'; |
||
45 | |||
46 | /** |
||
47 | * cached pattern to match |
||
48 | * |
||
49 | * @var string |
||
50 | * @access protected |
||
51 | */ |
||
52 | protected $ref_pattern = '~(\$\{((?:(?!\$\{|\}).)+?)\})~'; |
||
53 | |||
54 | /** |
||
55 | * unresolved reference |
||
56 | * |
||
57 | * @var array |
||
58 | * @access protected |
||
59 | */ |
||
60 | protected $unresolved = []; |
||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | public function setReference( |
||
81 | |||
82 | /** |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | public function hasReference( |
||
86 | /*# string */ $subject, |
||
87 | array &$matched |
||
88 | )/*# : bool */ { |
||
89 | if (is_string($subject) && |
||
90 | false !== strpos($subject, $this->ref_start) && |
||
91 | preg_match($this->ref_pattern, $subject, $matched) |
||
92 | ) { |
||
93 | return true; |
||
94 | } |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | public function deReference(/*# string */ $subject) |
||
102 | { |
||
103 | $matched = []; |
||
104 | $count = 0; |
||
105 | while ($this->hasReference($subject, $matched)) { |
||
106 | $val = $this->resolveReference($matched[2]); |
||
107 | if (is_string($val) && ++$count < 15) { |
||
108 | $subject = str_replace($matched[1], $val, $subject); |
||
109 | } elseif ($count == 15) { |
||
110 | return $val; |
||
111 | } else { |
||
112 | return $val; |
||
113 | } |
||
114 | } |
||
115 | return $subject; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | */ |
||
121 | public function deReferenceArray(&$dataArray) |
||
134 | |||
135 | /** |
||
136 | * Resolve the reference $name |
||
137 | * |
||
138 | * @param string $name |
||
139 | * @return mixed |
||
140 | * @access protected |
||
141 | */ |
||
142 | protected function resolveReference(/*# string */ $name) |
||
162 | |||
163 | /** |
||
164 | * For unknown reference $name |
||
165 | * |
||
166 | * @param string $name |
||
167 | * @return mixed |
||
168 | * @access protected |
||
169 | */ |
||
170 | abstract protected function resolveUnknown(/*# string */ $name); |
||
171 | |||
172 | /** |
||
173 | * The real resolving method. return NULL for unknown reference |
||
174 | * |
||
175 | * @param string $name |
||
176 | * @return mixed |
||
177 | * @access protected |
||
178 | */ |
||
179 | abstract protected function getReference(/*# string */ $name); |
||
180 | } |
||
181 |