-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSearchQueryParser.php
More file actions
167 lines (146 loc) · 4.98 KB
/
SearchQueryParser.php
File metadata and controls
167 lines (146 loc) · 4.98 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
<?php
namespace Bilendi\DevExpressBundle\DataGrid\Parser;
use Bilendi\DevExpressBundle\DataGrid\Expression\ComparisonExpression;
use Bilendi\DevExpressBundle\DataGrid\Expression\CompositeExpression;
use Bilendi\DevExpressBundle\DataGrid\Expression\Visitable;
use Bilendi\DevExpressBundle\DataGrid\Search\SearchQuery;
use Bilendi\DevExpressBundle\DataGrid\Search\SearchQueryBuilder;
use Bilendi\DevExpressBundle\Exception\NotArrayException;
use Bilendi\DevExpressBundle\Exception\NotNumericException;
/**
* Class SearchQueryParser.
*/
class SearchQueryParser
{
/**
* @var SearchQueryBuilder
*/
protected $builder;
/**
* SearchQueryParser constructor.
*/
public function __construct()
{
$this->builder = new SearchQueryBuilder();
}
public function getBuilder(): SearchQueryBuilder
{
return $this->builder;
}
public function parse(\stdClass $data): SearchQuery
{
if (isset($data->take)) {
$this->parseMaxResults($data);
}
if (isset($data->skip)) {
$this->parseStartIndex($data);
}
if (isset($data->sort)) {
$this->parseSort($data);
}
if (isset($data->filter)) {
$this->parseFilter($data);
}
return $this->builder->build();
}
/**
* @throws NotNumericException
*/
public function parseStartIndex(\stdClass $data)
{
if (is_numeric($data->skip)) {
$this->builder->setStartIndex(intval($data->skip));
} else {
throw new NotNumericException('skip');
}
}
/**
* @throws NotNumericException
*/
public function parseMaxResults(\stdClass $data)
{
if (is_numeric($data->take)) {
$this->builder->setMaxResults(intval($data->take));
} else {
throw new NotNumericException('take');
}
}
/**
* @throws NotArrayException
*/
public function parseSort(\stdClass $data)
{
if (is_array($data->sort)) {
\Functional\map(
$data->sort,
function (\stdClass $sort) {
$this->builder->sort($sort->selector, $sort->desc);
}
);
} else {
throw new NotArrayException('sort');
}
}
public function parseComparison(array $filter): ComparisonExpression
{
return new ComparisonExpression($filter[0], $filter[1], $filter[2]);
}
/**
* @throws \Exception
*/
public function parseDisjunction(array $filter, CompositeExpression $parent = null): Visitable
{
if (1 == count($filter)) {
if (null === $parent) {
return $this->parseComparison(array_shift($filter));
} else {
$parent->addExpression($this->parseDisjunction(array_shift($filter)));
return $parent;
}
} elseif (count($filter) >= 3) {
$first = array_shift($filter);
$second = array_shift($filter);
if ('and' === $second || 'or' === $second) {
if ('and' === $second) {
$newComposite = new CompositeExpression(CompositeExpression::TYPE_AND, []);
} else {
$newComposite = new CompositeExpression(CompositeExpression::TYPE_OR, []);
}
if (null !== $parent) {
if (count($filter[0]) > 1 && 0 === count($parent->getExpressions())) {
$newComposite->addExpression($this->parseDisjunction($first, $newComposite));
return $this->parseDisjunction($filter, $newComposite);
} else {
$parent->addExpression($this->parseDisjunction($first, $newComposite));
return $this->parseDisjunction($filter, $parent);
}
} else {
$newComposite->addExpression($this->parseDisjunction($first, $newComposite));
return $this->parseDisjunction($filter, $newComposite);
}
} else {
return $this->parseComparison([$first, $second, $filter[0]]);
}
} elseif (2 == count($filter)) {
$first = array_shift($filter);
$second = array_shift($filter);
if ('!' === $first) {
$newComposite = new CompositeExpression(CompositeExpression::TYPE_NOT, []);
$newComposite->addExpression($this->parseDisjunction($second, $newComposite));
return $newComposite;
}
}
throw new \Exception('Error for format filter:'.print_r($filter, true));
}
/**
* @throws NotArrayException
*/
public function parseFilter(\stdClass $data)
{
if (is_array($data->filter)) {
$this->builder->filter($this->parseDisjunction($data->filter));
} else {
throw new NotArrayException('filter');
}
}
}