forked from uuur86/strobj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringObjects.php
More file actions
480 lines (403 loc) · 11.1 KB
/
StringObjects.php
File metadata and controls
480 lines (403 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/**
* StrObj: PHP String Object Project
* It enables you to access any objects and arrays readily without
* any problem and in a safe manner.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package strobj
* @license GPLv2
* @author Uğur Biçer <info@ugurbicer.com.tr>
* @version 1.0.3
*/
declare(strict_types=1);
namespace StrObj;
use OverflowException;
use UnexpectedValueException;
use function next;
use function current;
use function key;
use function is_array;
use function is_string;
use function substr;
use function strlen;
use function sprintf;
use function strtolower;
use function preg_match;
use function explode;
use function implode;
use function ini_get;
use function memory_get_usage;
class StringObjects
{
/**
* The object to use
*
* @var object|array
*/
private $obj = null;
/**
* Latest query path info
*
* @var string
*/
private $currentPath = null;
/**
* Stored object values
*
* @var array
*/
private $paths = [];
/**
* Query results
*
* @var array
*/
private $results = [];
/**
* The object paths about validation errors
*
* @var array
*/
private $validationErrors = [];
/**
* User defined regex templates
*
* @var array
*/
private $regexType = [];
/**
* Memory limit in bytes
*
* @var int
*/
private $memoryLimit = 0;
/**
* Constructor
*
* @param object|array $obj The object to use
* @param int $memory The memory limit
*/
public function __construct($obj, int $memory)
{
$this->obj = $obj;
$this->setMemoryLimit($memory);
}
/**
* You can provide an array or any traversable object
*
* @param object $obj The object to use
* @param int $memory The memory limit
*
* @return bool|static
*/
public static function instance($obj, int $memory = 50)
{
if (empty($obj)) {
return false;
}
return new static($obj, $memory);
}
/**
* Converts the string to bytes
*
* @param string|int $amount
*
* @return int
*/
public function convertToByte($amount): int
{
$value = (string)(int)$amount;
$unit = strtolower(substr($amount, strlen($value)));
if ($unit == "g" || $unit == "gb") {
$value *= 1024 * 1024 * 1024;
} elseif ($unit == "m" || $unit == "mb") {
$value *= 1024 * 1024;
} elseif ($unit == "k" || $unit == "kb") {
$value *= 1024;
}
return $value;
}
/**
* Memory leak protection
*
* @param int $mem
*/
private function setMemoryLimit(int $mem): void
{
// Its check only once for performance.
if ($this->memoryLimit > 0) {
return;
}
$mbToByte = 1024 * 1024;
$default = 50 * $mbToByte;
$mem *= $mbToByte;
$iniGetMem = ini_get('memory_limit') ?
$this->convertToByte(ini_get('memory_limit')) : 0;
if (empty($iniGetMem)) {
$mem = $default;
} elseif ($mem > $iniGetMem) {
$mem = $iniGetMem;
}
$this->memoryLimit = $mem;
}
/**
* Registers regex type
*
* @param string $key type key name
* @param string $regex regex pattern
*/
public function addRegexType(string $key, string $regex): void
{
if (!empty($key) && !empty($regex)) {
$this->regexType[$key] = $regex;
}
}
/**
* Checks if the value is valid or not
*
* @param string $path requested path
* @param string $type pre-defined validator type
* @param bool $required field is required?
* @param string $selfRegex self defined regex text
*/
public function validator(string $path, string $type, $required = false, $selfRegex = ""): void
{
if (isset($this->regexType[$type])) {
$regex = $this->regexType[$type];
} elseif (!empty($selfRegex)) {
$regex = $selfRegex;
}
if (!empty($regex)) {
$this->validate($path, $regex, $required);
}
}
/**
* Validates the object path
*
* @param string $path
* @param string $regex
* @param bool $required
*
* @return bool
*
* @throws UnexpectedValueException
*/
private function validate(string $path, string $regex, bool $required): bool
{
$result = true;
$values = $this->getObj($path);
if (!$this->isPathExists($path)) {
$this->setAllPaths($this->validationErrors, $path, false);
}
if (!$required && !$values) {
return true;
}
if (is_string($values) && !empty($values)) {
$result = preg_match($regex, $values);
if ($result === 0 || ($required && empty($values))) {
$this->setAllPaths($this->validationErrors, $path, false);
return false;
} elseif ($result === 1) {
return true;
} elseif ($result === false) {
throw new UnexpectedValueException("StrObj Error: Validation error!");
}
return $result;
}
$values = Collection::instance($values);
while ($values->valid()) {
$result = $result && $this->validate($values->key(), $regex, $required);
$values->next();
}
if (!$result) {
$this->setAllPaths($this->validationErrors, $path, false);
}
return $result;
}
/**
* Checks whether the value which is in the desired path
* and added to the control list is valid or not
*
* @param string $path requested path
*
* @return bool
*/
public function isValid(?string $path): bool
{
return $this->isPathExists($path) && !isset($this->validationErrors[$path]);
}
/**
* Sets the value to the all parent paths.
*
* @param array $data
* @param string $path
* @param mixed $value
*/
public function setAllPaths(&$data, $path, $value): void
{
$pathArray = explode('/', $path);
if (!is_array($pathArray)) {
return;
}
$totalPath = [];
foreach ($pathArray as $pathPart) {
$totalPath[] = $pathPart;
if (is_array($totalPath) && !empty($totalPath)) {
$newPath = implode('/', $totalPath);
$data[$newPath] = $value;
}
}
}
/**
* Saves the value to cache for performance
*
* @param string $path requested path
* @param mixed $obj
*/
private function saveStoredValue(string $path, $obj): void
{
$this->paths[$path] = $obj;
}
/**
* Gets the stored value for performance. This function is used by get method.
*
* @param string $path requested path
*
* @return mixed
*/
private function getStoredValue(string $path)
{
return $this->paths[$path];
}
/**
* Checks whether the object path exists or not
*
* @param string $path
*
* @return bool
*/
public function isPathExists(?string $path): bool
{
return array_key_exists($path, $this->paths);
}
/**
* Performs an extensive search within the object.
*
* @param Collection $obj The object to be searched in
* @param array $pathArray The array of the object path
*
* @return array
*/
private function deepSearch(Collection $obj, array $pathArray): array
{
$results = [];
$objKey = key($pathArray);
while ($obj->valid()) {
// new assignment for each branch
$newPath = $pathArray;
$newPath[$objKey] = $obj->key();
$newPath = implode('/', $newPath);
// get the object belonging to this branch
$getObj = $this->getObj($newPath);
if ($this->isPathExists($newPath)) {
$results[$newPath] = $getObj;
}
$obj->next();
}
return $results;
}
/**
* Returns the requested object with the given path.
*
* @param string $path The path of the object or array to be accessed
*
* @return mixed
*/
private function getObj(string $path)
{
$obj = $this->obj;
if (empty($path)) {
return $obj;
}
if ($this->isPathExists($path)) {
return $this->getStoredValue($path);
}
$pathArray = explode('/', $path);
$currentPath = [];
while (false !== $pathPart = current($pathArray)) {
$obj = Collection::instance($obj);
if ($pathPart === '*') {
$obj = $this->deepSearch($obj, $pathArray);
$this->saveStoredValue($path, $obj);
return $obj;
}
$currentPath[] = $pathPart;
$this->currentPath = implode('/', $currentPath);
if ($obj->valid()) {
if ($obj->offsetExists($pathPart)) {
$obj = $obj->offsetGet($pathPart);
$this->saveStoredValue($this->currentPath, $obj);
}
}
next($pathArray);
}
return $obj;
}
/**
* Searches the requested object with the given path.
*
* @param string $path The path of the object or array to be accessed
*
* @return bool|object Returns $this if query is exists
* otherwise returns false
*
* @throws OverflowException
*/
public function query(string $path)
{
if (memory_get_usage() > $this->memoryLimit) {
throw new OverflowException(
sprintf(
"StrObj Error: Allowed memory size of %s exhausted! (max: %s bytes)",
(int)(memory_get_usage() - $this->memoryLimit),
$this->memoryLimit
)
);
}
$this->results = $this->getObj($path);
if (!empty($path) && !$this->isPathExists($path)) {
return false;
}
return $this;
}
/**
* Gets the value from the inside of the loaded object
* or returns the default value
*
* @param string $path requested object path like
* data/child_data instead of data->child_data
* @param mixed $default default value will return if value not exists
*
* @return mixed
*/
public function get(string $path, $default = false)
{
$results = $this->query($path);
if ($results instanceof self) {
return $results->getResults();
}
return $default;
}
/**
* Returns query results
*
* @return mixed
*/
public function getResults()
{
return $this->results;
}
}