Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
93.75% |
15 / 16 |
CRAP | |
89.36% |
42 / 47 |
Parser | |
0.00% |
0 / 1 |
|
93.75% |
15 / 16 |
21.53 | |
89.36% |
42 / 47 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
fromFile | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
fromString | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
getCommand | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getCollection | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getParsedJs | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
convertMongoObjects | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getDatabase | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
setDatabase | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
withData | |
100.00% |
1 / 1 |
3 | |
100.00% |
3 / 3 |
|||
clearData | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 5 |
|||
isParsed | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
parse | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
setTopLevelArray | |
100.00% |
1 / 1 |
1 | |
100.00% |
7 / 7 |
|||
setCommandInformations | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
pregify | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\MongoJsExecutor; |
4 | |
5 | use Illuminate\Support\Facades\Storage; |
6 | use Qmp\Laravel\MongoJsExecutor\Replacers; |
7 | |
8 | |
9 | class Parser |
10 | { |
11 | /** |
12 | * Undocumented variable |
13 | * |
14 | * @var string |
15 | */ |
16 | protected $rawJs; |
17 | |
18 | /** |
19 | * Undocumented variable |
20 | * |
21 | * @var string |
22 | */ |
23 | protected $jsonJs; |
24 | |
25 | /** |
26 | * Undocumented variable |
27 | * |
28 | * @var string |
29 | */ |
30 | protected $jsonJsClean; |
31 | |
32 | /** |
33 | * Undocumented variable |
34 | * |
35 | * @var [type] |
36 | */ |
37 | protected $command; |
38 | |
39 | /** |
40 | * Undocumented variable |
41 | * |
42 | * @var [type] |
43 | */ |
44 | protected $collection; |
45 | |
46 | /** |
47 | * Undocumented variable |
48 | * |
49 | * @var array|string |
50 | */ |
51 | protected $search = []; |
52 | |
53 | /** |
54 | * Undocumented variable |
55 | * |
56 | * @var array|string |
57 | */ |
58 | protected $replace = []; |
59 | |
60 | /** |
61 | * Undocumented variable |
62 | * |
63 | * @var string |
64 | */ |
65 | protected $database; |
66 | |
67 | /** |
68 | * |
69 | */ |
70 | protected $collectionSuffix; |
71 | |
72 | /** |
73 | * Undocumented variable |
74 | * |
75 | * @var array |
76 | */ |
77 | protected $replacers = [ |
78 | Replacers\Comma::class, |
79 | Replacers\Helpers::class, |
80 | Replacers\Range::class, |
81 | Replacers\Date::class, |
82 | Replacers\Json::class, |
83 | Replacers\NullString::class, |
84 | Replacers\Boolean::class |
85 | ]; |
86 | |
87 | /** |
88 | * Undocumented variable |
89 | * |
90 | * @var boolean |
91 | */ |
92 | protected $isParsed = false; |
93 | |
94 | /** |
95 | * Undocumented function |
96 | * |
97 | * @param [type] $collectionSuffix |
98 | */ |
99 | public function __construct($collectionSuffix = null) |
100 | { |
101 | $this->collectionSuffix = $collectionSuffix; |
102 | } |
103 | |
104 | /** |
105 | * Undocumented function |
106 | * |
107 | * @param string $filepath |
108 | * @param [type] $closure |
109 | * @return self |
110 | */ |
111 | public function fromFile(string $filepath, string $disk = null) |
112 | { |
113 | $path = $disk ? Storage::disk($disk)->url($filepath) : $filepath; |
114 | |
115 | $this->fromString(file_get_contents($path)); |
116 | return $this; |
117 | } |
118 | |
119 | /** |
120 | * Undocumented function |
121 | * |
122 | * @param string $file |
123 | * @return self |
124 | */ |
125 | public function fromString(string $file) |
126 | { |
127 | $this->rawJs = $file; |
128 | $this->setDatabase(); |
129 | $this->setCommandInformations(); |
130 | $this->setTopLevelArray(); |
131 | |
132 | return $this; |
133 | } |
134 | |
135 | /** |
136 | * Undocumented function |
137 | * |
138 | * @return void |
139 | */ |
140 | public function getCommand() |
141 | { |
142 | return $this->command; |
143 | } |
144 | |
145 | /** |
146 | * Undocumented function |
147 | * |
148 | * @return string |
149 | */ |
150 | public function getCollection() |
151 | { |
152 | return str_replace('SUFFIX', $this->collectionSuffix, $this->collection); |
153 | } |
154 | |
155 | /** |
156 | * Undocumented function |
157 | * |
158 | * @return array |
159 | */ |
160 | public function getParsedJs() |
161 | { |
162 | return $this->convertMongoObjects($this->jsonJs); |
163 | } |
164 | |
165 | /** |
166 | * Undocumented function |
167 | * |
168 | * @param [type] $json |
169 | * @return void |
170 | */ |
171 | protected function convertMongoObjects($json) |
172 | { |
173 | return (array)\MongoDB\BSON\toPHP(\MongoDB\BSON\fromJSON($json)); |
174 | } |
175 | |
176 | /** |
177 | * Undocumented function |
178 | * |
179 | * @return void |
180 | */ |
181 | public function getDatabase() |
182 | { |
183 | return $this->database; |
184 | } |
185 | |
186 | /** |
187 | * Undocumented function |
188 | * |
189 | * @return void |
190 | */ |
191 | protected function setDatabase() |
192 | { |
193 | $temp = explode("\n", $this->rawJs); |
194 | if (substr($temp[0], 0, 2) === '//') { |
195 | $database = explode(' ', $temp[0]); |
196 | $this->database = array_pop($database); |
197 | array_shift($temp); |
198 | } |
199 | } |
200 | |
201 | /** |
202 | * Undocumented function |
203 | * |
204 | * @param string $search |
205 | * @param string|array $replace |
206 | * @return self |
207 | */ |
208 | public function withData(string $search, $replace): self |
209 | { |
210 | $this->search[] = self::pregify($search); |
211 | $this->replace[] = is_array($replace) ? json_encode($replace) : (is_string($replace) ? '"' . $replace . '"' : $replace); |
212 | |
213 | return $this; |
214 | } |
215 | |
216 | /** |
217 | * Undocumented function |
218 | * |
219 | * @return self |
220 | */ |
221 | public function clearData(): self |
222 | { |
223 | $this->search = []; |
224 | $this->replace = []; |
225 | $this->isParsed = false; |
226 | $this->jsonJs = null; |
227 | |
228 | return $this; |
229 | } |
230 | |
231 | /** |
232 | * Undocumented function |
233 | * |
234 | * @return boolean |
235 | */ |
236 | public function isParsed() |
237 | { |
238 | return $this->isParsed; |
239 | } |
240 | |
241 | /** |
242 | * Undocumented function |
243 | * |
244 | * @return void |
245 | */ |
246 | public function parse() |
247 | { |
248 | |
249 | $this->jsonJs = preg_replace($this->search, $this->replace, $this->jsonJsClean); |
250 | |
251 | foreach ($this->replacers as $replacer) { |
252 | $this->jsonJs = (new $replacer)->handle($this->jsonJs); |
253 | } |
254 | |
255 | $this->isParsed = true; |
256 | |
257 | return $this; |
258 | } |
259 | /** |
260 | * Undocumented function |
261 | * |
262 | * @return void |
263 | */ |
264 | protected function setTopLevelArray() |
265 | { |
266 | $temp = explode('(', $this->rawJs); |
267 | array_shift($temp); |
268 | |
269 | $temp = explode(')', implode('(', $temp)); |
270 | array_pop($temp); |
271 | |
272 | $temp = implode(')', $temp); |
273 | |
274 | $this->jsonJsClean = $temp; |
275 | } |
276 | |
277 | /** |
278 | * Undocumented function |
279 | * |
280 | * @return void |
281 | */ |
282 | protected function setCommandInformations() |
283 | { |
284 | $infos = explode('.', explode('(', $this->rawJs)[0]); |
285 | $this->collection = $infos[1]; |
286 | $this->command = $infos[2]; |
287 | } |
288 | |
289 | |
290 | /** |
291 | * Undocumented function |
292 | * |
293 | * @param string $key |
294 | * @return string |
295 | */ |
296 | protected static function pregify($key) |
297 | { |
298 | return (sprintf("/[\"']?@%s[\"']?/", strtoupper($key))); |
299 | } |
300 | } |