1 | <?php |
||
26 | class VariableProvider |
||
27 | { |
||
28 | /** |
||
29 | * The replacement regex to get content between %^content% |
||
30 | * |
||
31 | * @var string REGEX_ENV_SYNTAX |
||
32 | */ |
||
33 | const REGEX_ENV_SYNTAX = '\\%\\^\\s*?([A-Za-z0-9 _ .]+)\\s*?\\%'; |
||
34 | |||
35 | /** |
||
36 | * The variable regex to get content between %content% |
||
37 | * |
||
38 | * @var string REGEX_REPLACEMENT_SYNTAX |
||
39 | */ |
||
40 | const REGEX_REPLACEMENT_SYNTAX = '\\%\\s*?([A-Za-z0-9 _ .]+)\\s*?\\%'; |
||
41 | |||
42 | /** |
||
43 | * The variable regex to get content between %$content% |
||
44 | * |
||
45 | * @var string REGEX_VARIABLE_SYNTAX |
||
46 | */ |
||
47 | const REGEX_VARIABLE_SYNTAX = '\\%\\$\\s*?([A-Za-z0-9 _ .]+)\\s*?\\%'; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * The replacement store |
||
52 | * |
||
53 | * @var \M1\Vars\Variables\ReplacementStore $rstore |
||
54 | */ |
||
55 | private static $variable_types = array('replacement', 'variable', 'env'); |
||
56 | |||
57 | /** |
||
58 | * The replacement store |
||
59 | * |
||
60 | * @var \M1\Vars\Variables\ReplacementStore $rstore |
||
61 | */ |
||
62 | public $rstore; |
||
63 | |||
64 | /** |
||
65 | * The variable store |
||
66 | * |
||
67 | * @var \M1\Vars\Variables\ReplacementStore $vstore |
||
68 | */ |
||
69 | public $vstore; |
||
70 | |||
71 | /** |
||
72 | * Creates new instance of VariableProvider |
||
73 | * |
||
74 | * @param \M1\Vars\Vars $vars Instance of the calling Vars |
||
75 | */ |
||
76 | 79 | public function __construct($vars) |
|
82 | |||
83 | /** |
||
84 | * Parses the string for the types of variables |
||
85 | * |
||
86 | * @param string $value The string to parse |
||
87 | * |
||
88 | * @return string The parsed variable |
||
89 | */ |
||
90 | 60 | public function parse($value) |
|
99 | |||
100 | /** |
||
101 | * Parses the string based on the type of variable |
||
102 | * |
||
103 | * @param string $value The string to parse |
||
104 | * @param string $type The variable type |
||
105 | * |
||
106 | * @return string The parsed variable |
||
107 | */ |
||
108 | 60 | private function typeParse($value, $type) |
|
124 | |||
125 | /** |
||
126 | * Fetches the variable matches in the string |
||
127 | * |
||
128 | * @param string $value The string to fetch matches for |
||
129 | * @param string $regex The variable type regex |
||
130 | * |
||
131 | * @return array The matches |
||
132 | */ |
||
133 | 60 | private function fetchVariableMatches($value, $regex) |
|
143 | |||
144 | /** |
||
145 | * Fetches the variable from the stores |
||
146 | * |
||
147 | * @param string $variable_name The variable to fetch |
||
148 | * @param string $type The variable type |
||
149 | * |
||
150 | * @return string The fetches value for the variable |
||
151 | */ |
||
152 | 5 | private function fetchVariable($variable_name, $type) |
|
164 | |||
165 | /** |
||
166 | * Checks to see if the variable exists |
||
167 | * |
||
168 | * @param string $variable The variable to check |
||
169 | * @param string $type The variable type |
||
170 | * |
||
171 | * @throws \InvalidArgumentException If the variable does not exist |
||
172 | * |
||
173 | * @return bool Does the variable exist |
||
174 | */ |
||
175 | 5 | private function checkVariableExists($variable, $type) |
|
188 | |||
189 | /** |
||
190 | * Does the replacements in the string for the variable |
||
191 | * |
||
192 | * @param string $value The string to parse |
||
193 | * @param array $matches The matches |
||
194 | * @param string $type The variable type |
||
195 | * |
||
196 | * @return string The parsed variable |
||
197 | */ |
||
198 | 2 | public function doReplacements($value, $matches, $type) |
|
212 | } |
||
213 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: