1 | <?php |
||
19 | trait ReferenceTrait |
||
20 | { |
||
21 | /** |
||
22 | * default is '${' and '}' |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $ref_pattern = '~(\$\{(\w((?!\$\{|\}).)*)\})~'; |
||
27 | |||
28 | /** |
||
29 | * @inheritDoc |
||
30 | */ |
||
31 | public function setReferencePattern(string $start, string $end): object |
||
38 | |||
39 | /** |
||
40 | * {@inheritDoc} |
||
41 | */ |
||
42 | public function deReference(&$subject): void |
||
56 | |||
57 | /** |
||
58 | * Replace all references in the target string (recursively) |
||
59 | * |
||
60 | * @param string $subject |
||
61 | * @return mixed |
||
62 | * @throws \RuntimeException if bad thing happens |
||
63 | */ |
||
64 | protected function deReferenceString(string $subject) |
||
82 | |||
83 | /** |
||
84 | * Check dereferenced value |
||
85 | * |
||
86 | * @param mixed $value |
||
87 | * @param string $subject the subject to dereference |
||
88 | * @param string $matched the matched reference |
||
89 | * @return mixed |
||
90 | * @throws \RuntimeException if $subject malformed |
||
91 | */ |
||
92 | protected function checkValue($value, string $subject, string $matched) |
||
107 | |||
108 | /** |
||
109 | * resolving the references |
||
110 | * |
||
111 | * @param mixed $name |
||
112 | * @return mixed |
||
113 | * @throws \RuntimeException if bad thing happens |
||
114 | */ |
||
115 | protected function resolveReference(string $name) |
||
122 | |||
123 | /** |
||
124 | * Dealing with unknown reference, may leave it untouched |
||
125 | * |
||
126 | * @param string $subject the subject to dereference |
||
127 | * @param string $matched the matched reference |
||
128 | * @return mixed |
||
129 | * @throws \RuntimeException if $subject malformed |
||
130 | */ |
||
131 | protected function resolveUnknown(string $subject, string $matched) |
||
135 | |||
136 | /** |
||
137 | * resolve the references |
||
138 | * |
||
139 | * @param string $name |
||
140 | * @return mixed |
||
141 | * @throws \RuntimeException if bad thing happens |
||
142 | */ |
||
143 | abstract protected function getReference(string $name); |
||
144 | } |
||
145 |