1 | <?php |
||
30 | trait ReferenceTrait |
||
31 | { |
||
32 | /** |
||
33 | * refernece start chars |
||
34 | * |
||
35 | * @var string |
||
36 | * @access protected |
||
37 | */ |
||
38 | protected $ref_start = '${'; |
||
39 | |||
40 | /** |
||
41 | * reference ending chars |
||
42 | * |
||
43 | * @var string |
||
44 | * @access protected |
||
45 | */ |
||
46 | protected $ref_end = '}'; |
||
47 | |||
48 | /** |
||
49 | * cached pattern to match |
||
50 | * |
||
51 | * @var string |
||
52 | * @access protected |
||
53 | */ |
||
54 | protected $ref_pattern = '~(\$\{((?:(?!\$\{|\}).)+?)\})~'; |
||
55 | |||
56 | /** |
||
57 | * unresolved reference |
||
58 | * |
||
59 | * @var array |
||
60 | * @access protected |
||
61 | */ |
||
62 | protected $unresolved = []; |
||
63 | |||
64 | /** |
||
65 | * {@inheritDoc} |
||
66 | */ |
||
67 | public function setReference( |
||
68 | /*# string */ $start, |
||
69 | /*# string */ $end |
||
70 | ) { |
||
71 | $this->ref_start = $start; |
||
72 | $this->ref_end = $end; |
||
73 | |||
74 | // build pattern |
||
75 | $s = preg_quote($start); |
||
76 | $e = preg_quote($end); |
||
77 | $this->ref_pattern = sprintf( |
||
78 | "~(%s((?:(?!%s|%s).)+?)%s)~", $s, $s, $e, $e |
||
79 | ); |
||
80 | |||
81 | return $this; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | public function hasReference( |
||
88 | /*# string */ $subject, |
||
89 | array &$matched |
||
90 | )/*# : bool */ { |
||
91 | if (is_string($subject) && |
||
92 | false !== strpos($subject, $this->ref_start) && |
||
93 | preg_match($this->ref_pattern, $subject, $matched) |
||
94 | ) { |
||
95 | return true; |
||
96 | } |
||
97 | return false; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritDoc} |
||
102 | */ |
||
103 | public function deReference(/*# string */ $subject) |
||
104 | { |
||
105 | $matched = []; |
||
106 | $loop = 0; |
||
107 | while ($this->hasReference($subject, $matched)) { |
||
108 | $val = $this->resolveReference($matched[2], $loop++); |
||
109 | if (is_string($val)) { |
||
110 | $subject = str_replace($matched[1], $val, $subject); |
||
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 | * @param int $loop |
||
140 | * @return mixed |
||
141 | * @throws RuntimeException if loop found |
||
142 | * @access protected |
||
143 | */ |
||
144 | protected function resolveReference(/*# string */ $name, /*# int */ $loop) |
||
172 | |||
173 | /** |
||
174 | * For unknown reference $name |
||
175 | * |
||
176 | * @param string $name |
||
177 | * @return mixed |
||
178 | * @access protected |
||
179 | */ |
||
180 | abstract protected function resolveUnknown(/*# string */ $name); |
||
181 | |||
182 | /** |
||
183 | * The real resolving method. return NULL for unknown reference |
||
184 | * |
||
185 | * @param string $name |
||
186 | * @return mixed |
||
187 | * @access protected |
||
188 | */ |
||
189 | abstract protected function getReference(/*# string */ $name); |
||
190 | } |
||
191 |