|
1
|
|
|
<?php namespace Neomerx\JsonApi\Http\Query; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2018 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Neomerx\JsonApi\Contracts\Document\ErrorInterface; |
|
20
|
|
|
use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface as P; |
|
21
|
|
|
use Neomerx\JsonApi\Document\Error; |
|
22
|
|
|
use Neomerx\JsonApi\Exceptions\JsonApiException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @package Neomerx\JsonApi |
|
26
|
|
|
*/ |
|
27
|
|
|
trait BaseQueryParserTrait |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $parameters |
|
31
|
|
|
* @param string $errorTitle |
|
32
|
|
|
* |
|
33
|
|
|
* @return iterable |
|
34
|
|
|
*/ |
|
35
|
10 |
|
protected function getIncludes(array $parameters, string $errorTitle): iterable |
|
36
|
|
|
{ |
|
37
|
10 |
|
if (array_key_exists(P::PARAM_INCLUDE, $parameters) === true) { |
|
38
|
9 |
|
$splitByDot = function (string $path) use ($errorTitle): iterable { |
|
39
|
5 |
|
foreach ($this->splitStringAndCheckNoEmpties(P::PARAM_INCLUDE, $path, '.', $errorTitle) as $link) { |
|
40
|
5 |
|
yield $link; |
|
41
|
|
|
} |
|
42
|
9 |
|
}; |
|
43
|
|
|
|
|
44
|
9 |
|
$paramName = P::PARAM_INCLUDE; |
|
45
|
9 |
|
$includes = $parameters[P::PARAM_INCLUDE]; |
|
46
|
9 |
|
foreach ($this->splitCommaSeparatedStringAndCheckNoEmpties($paramName, $includes, $errorTitle) as $path) { |
|
47
|
5 |
|
yield $path => $splitByDot($path); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
5 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $parameters |
|
54
|
|
|
* @param string $errorTitle |
|
55
|
|
|
* |
|
56
|
|
|
* @return iterable |
|
57
|
|
|
*/ |
|
58
|
4 |
|
protected function getFields(array $parameters, string $errorTitle): iterable |
|
59
|
|
|
{ |
|
60
|
4 |
|
if (array_key_exists(P::PARAM_FIELDS, $parameters) === true) { |
|
61
|
3 |
|
$fields = $parameters[P::PARAM_FIELDS]; |
|
62
|
3 |
|
if (is_array($fields) === false || empty($fields) === true) { |
|
63
|
1 |
|
throw new JsonApiException($this->createParameterError(P::PARAM_FIELDS, $errorTitle)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
foreach ($fields as $type => $fieldList) { |
|
67
|
2 |
|
yield $type => $this->splitCommaSeparatedStringAndCheckNoEmpties($type, $fieldList, $errorTitle); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $parameters |
|
74
|
|
|
* @param string $errorTitle |
|
75
|
|
|
* |
|
76
|
|
|
* @return iterable |
|
77
|
|
|
*/ |
|
78
|
2 |
|
protected function getSorts(array $parameters, string $errorTitle): iterable |
|
79
|
|
|
{ |
|
80
|
2 |
|
if (array_key_exists(P::PARAM_SORT, $parameters) === true) { |
|
81
|
2 |
|
$sorts = $parameters[P::PARAM_SORT]; |
|
82
|
2 |
|
$values = $this->splitCommaSeparatedStringAndCheckNoEmpties(P::PARAM_SORT, $sorts, $errorTitle); |
|
83
|
2 |
|
foreach ($values as $orderAndField) { |
|
84
|
2 |
|
switch ($orderAndField[0]) { |
|
85
|
2 |
|
case '-': |
|
86
|
2 |
|
$isAsc = false; |
|
87
|
2 |
|
$field = substr($orderAndField, 1); |
|
88
|
2 |
|
break; |
|
89
|
2 |
|
case '+': |
|
90
|
2 |
|
$isAsc = true; |
|
91
|
2 |
|
$field = substr($orderAndField, 1); |
|
92
|
2 |
|
break; |
|
93
|
|
|
default: |
|
94
|
2 |
|
$isAsc = true; |
|
95
|
2 |
|
$field = $orderAndField; |
|
96
|
2 |
|
break; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
yield $field => $isAsc; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $paramName |
|
106
|
|
|
* @param string|mixed $shouldBeString |
|
107
|
|
|
* @param string $errorTitle |
|
108
|
|
|
* |
|
109
|
|
|
* @return iterable |
|
110
|
|
|
*/ |
|
111
|
11 |
|
private function splitCommaSeparatedStringAndCheckNoEmpties( |
|
112
|
|
|
string $paramName, |
|
113
|
|
|
$shouldBeString, |
|
114
|
|
|
string $errorTitle |
|
115
|
|
|
): iterable { |
|
116
|
11 |
|
return $this->splitStringAndCheckNoEmpties($paramName, $shouldBeString, ',', $errorTitle); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $paramName |
|
121
|
|
|
* @param string|mixed $shouldBeString |
|
122
|
|
|
* @param string $separator |
|
123
|
|
|
* @param string $errorTitle |
|
124
|
|
|
* |
|
125
|
|
|
* @return iterable |
|
126
|
|
|
*/ |
|
127
|
11 |
|
private function splitStringAndCheckNoEmpties( |
|
128
|
|
|
string $paramName, |
|
129
|
|
|
$shouldBeString, |
|
130
|
|
|
string $separator, |
|
131
|
|
|
string $errorTitle |
|
132
|
|
|
): iterable { |
|
133
|
11 |
|
if (is_string($shouldBeString) === false || ($trimmed = trim($shouldBeString)) === '') { |
|
134
|
4 |
|
throw new JsonApiException($this->createParameterError($paramName, $errorTitle)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
7 |
|
foreach (explode($separator, $trimmed) as $value) { |
|
138
|
7 |
|
$trimmedValue = trim($value); |
|
139
|
7 |
|
if (($trimmedValue) === '') { |
|
140
|
1 |
|
throw new JsonApiException($this->createParameterError($paramName, $errorTitle)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
7 |
|
yield $trimmedValue; |
|
144
|
|
|
} |
|
145
|
7 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $paramName |
|
149
|
|
|
* @param string $errorTitle |
|
150
|
|
|
* |
|
151
|
|
|
* @return ErrorInterface |
|
152
|
|
|
*/ |
|
153
|
6 |
|
private function createParameterError(string $paramName, string $errorTitle): ErrorInterface |
|
154
|
|
|
{ |
|
155
|
6 |
|
$source = [Error::SOURCE_PARAMETER => $paramName]; |
|
156
|
6 |
|
$error = new Error(null, null, null, null, $errorTitle, null, $source); |
|
157
|
|
|
|
|
158
|
6 |
|
return $error; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|