1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | defined('BASEPATH') or exit('No direct script access allowed'); |
||
5 | |||
6 | class JSONP |
||
7 | { |
||
8 | |||
9 | private $data = null; |
||
10 | private $buffer = null; |
||
11 | private $pool = null; |
||
12 | private $poolKey = null; |
||
13 | |||
14 | const FIELD_REGEX = "/\w+/"; |
||
15 | const ARRAY_REGEX = "/^\w+\[/"; |
||
16 | const ALL_ARRAY_REGEX = "/^\w+\[\*\]/"; |
||
17 | const SPECIFIC_ARRAY_REGEX = "/^\w+\[\d+\]/"; |
||
18 | const RANGE_ARRAY_REGEX = "/^\w+\[(\d|):(\d|)\]/"; |
||
19 | |||
20 | /** |
||
21 | * [parse description] |
||
22 | * @param [type] $data [description] |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
23 | * @return bool [description] |
||
24 | */ |
||
25 | public function parse(&$data): bool |
||
26 | { |
||
27 | if (is_scalar($data)) { |
||
28 | $this->data = json_decode($data); |
||
29 | if ($this->data == null || !is_object($this->data)) { |
||
30 | $this->data = null; |
||
31 | return false; |
||
32 | } |
||
33 | return true; |
||
34 | } |
||
35 | if (is_array($data)) { |
||
36 | $this->data = &$data; |
||
37 | //var_dump($this->data); |
||
38 | return true; |
||
39 | } |
||
40 | return false; |
||
41 | } |
||
42 | /** |
||
43 | * [set description] |
||
44 | * @param string $path [description] |
||
45 | * @param [type] $value [description] |
||
0 ignored issues
–
show
|
|||
46 | */ |
||
47 | public function set(string $path, $value): void |
||
48 | { |
||
49 | $this->run_json_path($path); |
||
50 | if ($this->pool == null) $this->buffer = $value; |
||
51 | |||
52 | if ($this->poolKey == null) { |
||
53 | for ($x = 0; count($this->pool); $x++) { |
||
54 | $this->pool[array_keys($this->pool)[$x]] = $value; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | if (is_scalar($this->poolKey)) { |
||
59 | $values = []; |
||
60 | $this->recursive_scalar_key_build($values, $this->pool); |
||
61 | //return $values; |
||
62 | } |
||
63 | } |
||
64 | /** |
||
65 | * [get description] |
||
66 | * @param string $path [description] |
||
67 | * @return [type] [description] |
||
0 ignored issues
–
show
|
|||
68 | */ |
||
69 | public function get(string $path) |
||
70 | { |
||
71 | $this->run_json_path($path); |
||
72 | |||
73 | if ($this->pool == null) return $this->buffer; |
||
74 | |||
75 | if ($this->poolKey == null) return $this->pool; |
||
76 | |||
77 | if (is_scalar($this->poolKey)) { |
||
78 | $values = []; |
||
79 | var_dump($this->poolKey); |
||
0 ignored issues
–
show
|
|||
80 | $this->recursive_scalar_key_build($values, $this->pool); |
||
81 | return $values; |
||
82 | } |
||
83 | |||
84 | // TODO: Recursive Array Key Build. |
||
85 | } |
||
86 | private function recursive_scalar_key_build(array &$values, &$pool): void |
||
87 | { |
||
88 | if (isset($pool[$this->poolKey])) { |
||
89 | $values[] = &$pool[$this->poolKey]; |
||
90 | } |
||
91 | for ($x = 0; $x < count($pool); $x++) { |
||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
|
|||
92 | if (is_array($pool[array_keys($pool)[$x]])) { |
||
93 | $this->recursive_scalar_key_build($values, $pool[array_keys($pool)[$x]]); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | /** |
||
98 | * [get_reference description] |
||
99 | * @param string $path [description] |
||
100 | * @return [type] [description] |
||
0 ignored issues
–
show
|
|||
101 | */ |
||
102 | public function &get_reference(string $path) |
||
103 | { |
||
104 | $this->run_json_path($path); |
||
105 | |||
106 | if ($this->pool == null) return $this->buffer; |
||
107 | |||
108 | if ($this->poolKey == null) return $this->pool; |
||
109 | |||
110 | if (is_scalar($this->poolKey)) { |
||
111 | $values = []; |
||
112 | $this->recursive_scalar_key_build($values, $this->pool); |
||
113 | return $values; |
||
114 | } |
||
115 | } |
||
116 | /** |
||
117 | * [run_path description] |
||
118 | * @param string $path [description] |
||
119 | */ |
||
120 | private function run_json_path(string $path): void |
||
121 | { |
||
122 | $this->buffer = &$this->data; |
||
123 | unset($this->pool); |
||
124 | $this->pool = null; |
||
125 | $this->poolKey = null; |
||
126 | $steps = explode('.', $path); |
||
127 | foreach ($steps as $loopIndex => $step) { |
||
128 | switch ($step) { |
||
129 | case '$': |
||
130 | case '': |
||
131 | continue 2; |
||
132 | default: |
||
133 | |||
134 | if ($this->exact_match(self::FIELD_REGEX, $step)) { |
||
135 | if ($this->pool == null) { |
||
136 | $this->buffer = &$this->buffer[$step]; |
||
137 | } else { |
||
138 | if ($this->poolKey == null) { |
||
139 | $this->poolKey = $step; |
||
140 | } else { |
||
141 | $this->poolKey .= ".$step"; |
||
142 | } |
||
143 | } |
||
144 | if ($loopIndex == count($steps) - 1) { |
||
145 | break; |
||
146 | } else { |
||
147 | continue 2; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | if ($step == '[*]' || $step == '*') { |
||
152 | $this->pool = &$this->buffer; |
||
153 | if ($loopIndex == count($steps) - 1) { |
||
154 | break; |
||
155 | } else { |
||
156 | continue 2; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | if (preg_match(self::ARRAY_REGEX, $step)) { |
||
161 | if (preg_match(self::SPECIFIC_ARRAY_REGEX, $step)) { |
||
162 | list($key, $index) = $this->parse_specific_array_notation($step); |
||
163 | if ($this->pool == null) { |
||
164 | $this->buffer = &$this->buffer[$key][$index]; |
||
165 | } else { |
||
166 | $this->poolKey = [$key, $index]; |
||
167 | } |
||
168 | if ($loopIndex == count($steps) - 1) { |
||
169 | break; |
||
170 | } else { |
||
171 | continue 2; |
||
172 | } |
||
173 | } |
||
174 | if (preg_match(self::ALL_ARRAY_REGEX, $step)) { |
||
175 | $this->pool = &$this->buffer[$this->get_key_from_notation($step)]; |
||
176 | if ($loopIndex == count($steps) - 1) { |
||
177 | break; |
||
178 | } else { |
||
179 | continue 2; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | /** |
||
187 | * [exact_match description] |
||
188 | * @param string $regex [description] |
||
189 | * @param [type] $haystack [description] |
||
0 ignored issues
–
show
|
|||
190 | * @return bool [description] |
||
191 | */ |
||
192 | private function exact_match(string $regex, $haystack): bool |
||
193 | { |
||
194 | preg_match($regex, $haystack, $matches); |
||
195 | return count($matches) > 0 && strlen($matches[0]) == strlen($haystack); |
||
196 | } |
||
197 | /** |
||
198 | * [get_key_from_notation description] |
||
199 | * @param string $notation [description] |
||
200 | * @return string [description] |
||
201 | */ |
||
202 | private function get_key_from_notation(string $notation): string |
||
203 | { |
||
204 | preg_match("/\w+/", $notation, $matches); |
||
205 | return $matches[0]; |
||
206 | } |
||
207 | /** |
||
208 | * [parse_specific_array_notation description] |
||
209 | * @param string $notation [description] |
||
210 | * @return array [description] |
||
211 | */ |
||
212 | private function parse_specific_array_notation(string $notation): array |
||
213 | { |
||
214 | preg_match("/\w+/", $notation, $matches); |
||
215 | $key = $matches[0]; |
||
216 | $notation = preg_replace(self::ARRAY_REGEX, '', $notation); |
||
217 | $notation = str_replace(']', '', $notation); |
||
218 | return [$key, $notation]; |
||
219 | } |
||
220 | } |
||
221 |