-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReQuery.php
79 lines (65 loc) · 2.14 KB
/
ReQuery.php
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
<?php
namespace Bfg\Entity;
/**
* Class ReQuery.
* @package Bfg\Entity
*/
class ReQuery
{
/**
* @var array
*/
protected array $filter = [];
/**
* ReQuery constructor.
* @param $source
*/
public function __construct(
protected $source
) {
}
/**
* @param string $query Example: upper_case|test>>int|float:id,test|hay:name,int:role_id,roles(id,name,users(id,name)),test
* @param array|null $global
* @return $this
*/
public function setFilter(string $query, array $global = null)
{
$matches = [];
$current_global = [];
if (preg_match_all('/((([0-9A-Za-z\_\-\s\|]+)([\>]{1,2}))*(([0-9A-Za-z\_\-\s\|]+)\:)*([0-9A-Za-z\_\-\s]+)(\((.*)\))?)/', $query, $matches, PREG_SET_ORDER, 0)) {
foreach ($matches as $match) {
if ($match[3]) {
$current_global = explode('|', $match[3]);
if ($match[4] == '>>') {
if ($global) {
$global = array_unique(array_merge($global, $current_global));
} else {
$global = $current_global;
}
}
}
if ($match[7]) {
$add_data = [
'field' => trim($match[7]),
];
$g = array_merge($global ?? $current_global);
if ($match[6]) {
$add_data['filter'] = array_unique(array_merge($g, explode('|', $match[6])));
} elseif ($g) {
$add_data['filter'] = $g;
}
if (isset($match[9]) && $match[9]) {
$add_data['child'] = $this->parse($match[9], $global);
}
// if (isset($match[8]) && $match[8]) {
//
// $add_data['child'] = $this->parse(substr($match[8], 1, -1), $global);
// }
$this->filter[] = $add_data;
}
}
}
return $this;
}
}