|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MarcusCampos\Dealer; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
class Parser |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $regex = "/[,]+(?![^\(]*\))/"; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Parse |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $queryText |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
public function parse($queryText) |
|
21
|
|
|
{ |
|
22
|
|
|
$splitedValues = explode('->', $queryText); |
|
23
|
|
|
$response = []; |
|
24
|
|
|
$hasModel = false; |
|
25
|
|
|
|
|
26
|
|
|
foreach ($splitedValues as $value) { |
|
27
|
|
|
$key = substr($value, 0, strpos($value, '(')); |
|
28
|
|
|
$stringBetweenParentheses = $this->getStringBetween($value, '(', ')'); |
|
29
|
|
|
|
|
30
|
|
|
switch($key) { |
|
31
|
|
|
case 'filters': |
|
32
|
|
|
$response['filters'] = preg_split($this->regex, $stringBetweenParentheses); |
|
33
|
|
|
$response = $this->parseArgs('filters', $response); |
|
34
|
|
|
break; |
|
35
|
|
|
|
|
36
|
|
|
case 'orderBy': |
|
37
|
|
|
$response['orderBy'] = explode(',', $this->getStringBetween($value, '(', ')')); |
|
38
|
|
|
break; |
|
39
|
|
|
|
|
40
|
|
|
case 'limit': |
|
41
|
|
|
$response['limit'] = $this->getStringBetween($value, '(', ')'); |
|
42
|
|
|
break; |
|
43
|
|
|
|
|
44
|
|
|
case 'paginate': |
|
45
|
|
|
$response['paginate'] = $this->getStringBetween($value, '(', ')'); |
|
46
|
|
|
break; |
|
47
|
|
|
|
|
48
|
|
|
case 'groupBy': |
|
49
|
|
|
$response['groupBy'] = $this->getStringBetween($value, '(', ')'); |
|
50
|
|
|
break; |
|
51
|
|
|
|
|
52
|
|
|
default: |
|
53
|
|
|
if (!$hasModel) { |
|
54
|
|
|
$response['model'] = ucfirst($key); |
|
55
|
|
|
$response['fields'] = preg_split($this->regex, $stringBetweenParentheses); |
|
56
|
|
|
$response = $this->parseArgs('fields',$response); |
|
57
|
|
|
$hasModel = true; |
|
58
|
|
|
} else { |
|
59
|
|
|
throw new Exception('You have a syntax problem near "'.$key.'"'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
break; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $response; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get string between chars |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $str |
|
73
|
|
|
* @param string $from |
|
74
|
|
|
* @param string $to |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
private function getStringBetween($str, $from, $to) |
|
78
|
|
|
{ |
|
79
|
|
|
$sub = substr($str, strpos($str, $from)+strlen($from),strlen($str)); |
|
80
|
|
|
return substr($sub,0, strrpos($sub, $to)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Parse arguments |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $element |
|
87
|
|
|
* @param array $data |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
private function parseArgs(string $element, array $data) |
|
91
|
|
|
{ |
|
92
|
|
|
$response = $data; |
|
93
|
|
|
|
|
94
|
|
|
foreach ($response[$element] as $index => $value) { |
|
95
|
|
|
if (strpos($value, '(') + strpos($value, ')') > 1) { |
|
96
|
|
|
$fKey = substr($value, 0, strpos($value, '(')); |
|
97
|
|
|
$fStringBetween = $this->getStringBetween($value, '(', ')'); |
|
98
|
|
|
$response[$element]['extends'][] = [ |
|
99
|
|
|
"name" => $fKey, 'args' => $fStringBetween ? preg_split($this->regex, str_replace('\'', '',$fStringBetween)) : null |
|
100
|
|
|
]; |
|
101
|
|
|
unset($response[$element][$index]); |
|
102
|
|
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$response[$element]['only'][] = $value; |
|
106
|
|
|
unset($response[$element][$index]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $response; |
|
110
|
|
|
} |
|
111
|
|
|
} |