1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Marcin. |
4
|
|
|
* Date: 16.06.2018 |
5
|
|
|
* Time: 13:58 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Mrcnpdlk\Lib\UrlSearchParser\Criteria; |
9
|
|
|
|
10
|
|
|
use ArrayIterator; |
11
|
|
|
use IteratorAggregate; |
12
|
|
|
use Mrcnpdlk\Lib\UrlSearchParser\Exception\DuplicateParamException; |
13
|
|
|
use Mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException; |
14
|
|
|
use Traversable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Sort |
18
|
|
|
*/ |
19
|
|
|
class Sort implements IteratorAggregate |
20
|
|
|
{ |
21
|
|
|
public const DIRECTION_ASC = 'ASC'; |
22
|
|
|
public const DIRECTION_DESC = 'DESC'; |
23
|
|
|
public const DELIMITER = ','; |
24
|
|
|
public const DESC_IDENTIFIER = '-'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Mrcnpdlk\Lib\UrlSearchParser\Criteria\SortParam[] |
28
|
|
|
*/ |
29
|
|
|
private $sortParams = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sort constructor. |
33
|
|
|
* |
34
|
|
|
* @param string|null $sortString |
35
|
|
|
* |
36
|
|
|
* @throws \Mrcnpdlk\Lib\UrlSearchParser\Exception\DuplicateParamException |
37
|
|
|
* @throws \Mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException |
38
|
|
|
* @throws \Mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException |
39
|
|
|
*/ |
40
|
27 |
|
public function __construct(string $sortString = null) |
41
|
|
|
{ |
42
|
|
|
/** @var string[] $tParams */ |
43
|
27 |
|
$tParams = $sortString ? explode(self::DELIMITER, $sortString) : []; |
44
|
27 |
|
foreach ($tParams as $param) { |
45
|
6 |
|
if (empty($param)) { |
46
|
1 |
|
throw new EmptyParamException(sprintf('Empty SORT param')); |
47
|
|
|
} |
48
|
5 |
|
if (self::DESC_IDENTIFIER === $param[0]) { |
49
|
4 |
|
$param = substr($param, 1); |
50
|
4 |
|
if (empty($param)) { |
51
|
1 |
|
throw new EmptyParamException(sprintf('Empty SORT param')); |
52
|
|
|
} |
53
|
3 |
|
$this->appendParam(new SortParam($param, self::DIRECTION_DESC)); |
54
|
|
|
} else { |
55
|
4 |
|
$this->appendParam(new SortParam($param, self::DIRECTION_ASC)); |
56
|
|
|
} |
57
|
|
|
} |
58
|
24 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \Mrcnpdlk\Lib\UrlSearchParser\Criteria\SortParam $sortParam |
62
|
|
|
* |
63
|
|
|
* @throws \Mrcnpdlk\Lib\UrlSearchParser\Exception\DuplicateParamException |
64
|
|
|
* |
65
|
|
|
* @return \Mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort |
66
|
|
|
*/ |
67
|
4 |
|
public function appendParam(SortParam $sortParam): Sort |
68
|
|
|
{ |
69
|
4 |
|
if ($this->isExists($sortParam->param)) { |
70
|
1 |
|
throw new DuplicateParamException(sprintf('Duplicate Sort param `%s`', $sortParam->param)); |
71
|
|
|
} |
72
|
4 |
|
$this->sortParams[] = $sortParam; |
73
|
|
|
|
74
|
4 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Removing parametr by name |
79
|
|
|
* |
80
|
|
|
* @param string $paramName |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
1 |
|
public function removeByParamName(string $paramName): Sort |
85
|
|
|
{ |
86
|
1 |
|
foreach ($this->sortParams as $key => $param) { |
87
|
1 |
|
if ($param->param === $paramName) { |
88
|
1 |
|
unset($this->sortParams[$key]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $paramName |
97
|
|
|
* |
98
|
|
|
* @return \Mrcnpdlk\Lib\UrlSearchParser\Criteria\SortParam[] |
99
|
|
|
*/ |
100
|
2 |
|
public function getByParamName(string $paramName): array |
101
|
|
|
{ |
102
|
2 |
|
$params = []; |
103
|
2 |
|
foreach ($this->sortParams as $param) { |
104
|
2 |
|
if ($param->param === $paramName) { |
105
|
2 |
|
$params[] = $param; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
return $params; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieve an external iterator |
114
|
|
|
* |
115
|
|
|
* @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
116
|
|
|
* |
117
|
|
|
* @return Traversable<\Mrcnpdlk\Lib\UrlSearchParser\Criteria\SortParam> An instance of an object implementing <b>Iterator</b> or |
118
|
|
|
* <b>Traversable</b> |
119
|
|
|
* |
120
|
|
|
* @since 5.0.0 |
121
|
|
|
*/ |
122
|
1 |
|
public function getIterator(): Traversable |
123
|
|
|
{ |
124
|
1 |
|
return new ArrayIterator($this->sortParams); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $paramName |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
4 |
|
public function isExists(string $paramName): bool |
133
|
|
|
{ |
134
|
4 |
|
foreach ($this->sortParams as $param) { |
135
|
3 |
|
if ($param->param === $paramName) { |
136
|
3 |
|
return true; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param \Mrcnpdlk\Lib\UrlSearchParser\Criteria\SortParam $sortParam |
145
|
|
|
* |
146
|
|
|
* @throws \Mrcnpdlk\Lib\UrlSearchParser\Exception\DuplicateParamException |
147
|
|
|
* |
148
|
|
|
* @return \Mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort |
149
|
|
|
*/ |
150
|
1 |
|
public function replaceParam(SortParam $sortParam): Sort |
151
|
|
|
{ |
152
|
1 |
|
$isChanged = false; |
153
|
1 |
|
foreach ($this->sortParams as &$param) { |
154
|
1 |
|
if ($param->param === $sortParam->param) { |
155
|
1 |
|
$param->direction = $sortParam->direction; |
156
|
1 |
|
$isChanged = true; |
157
|
|
|
} |
158
|
|
|
} |
159
|
1 |
|
unset($param); |
160
|
1 |
|
if (!$isChanged) { |
161
|
|
|
$this->appendParam($sortParam); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return \Mrcnpdlk\Lib\UrlSearchParser\Criteria\SortParam[] |
169
|
|
|
*/ |
170
|
24 |
|
public function toArray(): array |
171
|
|
|
{ |
172
|
24 |
|
return $this->sortParams; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|