-
Notifications
You must be signed in to change notification settings - Fork 0
/
Orm.php
165 lines (128 loc) · 4.64 KB
/
Orm.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
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
<?php namespace Phprest\Service\RequestFilter\Processor;
use Doctrine\Common\Collections\Criteria;
use Phprest\Service\RequestFilter\Contract\Query\ProcessStrategy as QueryProcessStrategy;
use Phprest\Service\RequestFilter\Contract\Sort\ProcessStrategy as SortProcessStrategy;
use Phprest\Service\RequestFilter\Contract\Offset\ProcessStrategy as OffsetProcessStrategy;
use Phprest\Service\RequestFilter\Contract\Limit\ProcessStrategy as LimitProcessStrategy;
use Phprest\Service\RequestFilter\DataStructure;
use Phprest\Service\RequestFilter\DataStructure\Query\Expression;
class Orm implements QueryProcessStrategy, SortProcessStrategy, OffsetProcessStrategy, LimitProcessStrategy
{
/**
* @var Criteria
*/
protected $criteria;
/**
* @var array
*/
protected $specialProperties;
/**
* @var array
*/
protected $excludedProperties;
/**
* @param Criteria $criteria
* @param array $specialProperties array of property => callback
* @param array $excludedProperties array of property names
*/
public function __construct(Criteria $criteria,
array $specialProperties = [],
array $excludedProperties = [])
{
$this->criteria = $criteria;
$this->specialProperties = $specialProperties;
$this->excludedProperties = $excludedProperties;
}
/**
* @param DataStructure\Query\Query $queries
*
* @return mixed
*/
public function query(DataStructure\Query\Query $queries)
{
$logicFunc = 'andWhere';
if ($queries instanceof DataStructure\OrQuery) {
$logicFunc = 'orWhere';
}
foreach ($queries as $query) {
if ($this->isExcludedProperty($query->getProperty())) {
continue;
}
$value = $this->unlockSpecialProperty($query->getProperty(), $query->getValue());
if ($query instanceof Expression\Equal) {
$this->criteria->{$logicFunc}(Criteria::expr()->eq($query->getProperty(), $value));
} elseif ($query instanceof Expression\NotEqual) {
$this->criteria->{$logicFunc}(Criteria::expr()->neq($query->getProperty(), $value));
} elseif ($query instanceof Expression\GreaterThan) {
$this->criteria->{$logicFunc}(Criteria::expr()->gt($query->getProperty(), $value));
} elseif ($query instanceof Expression\GreaterThanEqual) {
$this->criteria->{$logicFunc}(Criteria::expr()->gte($query->getProperty(), $value));
} elseif ($query instanceof Expression\LesserThan) {
$this->criteria->{$logicFunc}(Criteria::expr()->lt($query->getProperty(), $value));
} elseif ($query instanceof Expression\LesserThanEqual) {
$this->criteria->{$logicFunc}(Criteria::expr()->lte($query->getProperty(), $value));
} elseif ($query instanceof Expression\In) {
$this->criteria->{$logicFunc}(Criteria::expr()->in($query->getProperty(), $value));
} elseif ($query instanceof Expression\NotIn) {
$this->criteria->{$logicFunc}(Criteria::expr()->notIn($query->getProperty(), $query->getValue()));
}
}
}
/**
* @param DataStructure\Sort $sorting
*
* @return void
*/
public function sort(DataStructure\Sort $sorting)
{
$sortings = [];
foreach ($sorting as $sortingEntity) {
if ($this->isExcludedProperty($sortingEntity->getProperty())) {
continue;
}
$sortings[$sortingEntity->getProperty()] =
$sortingEntity instanceof DataStructure\Sort\Asc ? 'asc' : 'desc';
}
$this->criteria->orderBy($sortings);
}
/**
* @param integer $offset
*
* @return mixed
*/
public function offset($offset)
{
$this->criteria->setFirstResult($offset);
}
/**
* @param integer $limit
*
* @return void
*/
public function limit($limit)
{
$this->criteria->setMaxResults($limit);
}
/**
* @param string $property
* @param mixed $value
*
* @return mixed
*/
protected function unlockSpecialProperty($property, $value)
{
if (array_key_exists($property, $this->specialProperties)) {
return $this->specialProperties[$property]($value);
}
return $value;
}
/**
* @param string $property
*
* @return boolean
*/
protected function isExcludedProperty($property)
{
return in_array($property, $this->excludedProperties);
}
}