17
17
*/
18
18
19
19
use Neomerx \JsonApi \Contracts \Http \Query \BaseQueryParserInterface ;
20
- use Neomerx \JsonApi \Document \Error ;
21
- use Neomerx \JsonApi \Exceptions \JsonApiException ;
22
20
23
21
/**
24
22
* @package Neomerx\JsonApi
25
23
*/
26
24
class BaseQueryParser implements BaseQueryParserInterface
27
25
{
26
+ use BaseQueryParserTrait {
27
+ BaseQueryParserTrait::getFields as getFieldsImpl;
28
+ BaseQueryParserTrait::getIncludes as getIncludesImpl;
29
+ BaseQueryParserTrait::getSorts as getSortsImpl;
30
+ }
31
+
28
32
/** Message */
29
33
public const MSG_ERR_INVALID_PARAMETER = 'Invalid Parameter. ' ;
30
34
@@ -44,132 +48,43 @@ class BaseQueryParser implements BaseQueryParserInterface
44
48
*/
45
49
public function __construct (array $ parameters = [], array $ messages = null )
46
50
{
47
- $ this ->setParameters ($ parameters );
48
- $ this ->messages = $ messages ;
49
- }
50
-
51
- /**
52
- * @param array $parameters
53
- *
54
- * @return self
55
- */
56
- public function setParameters (array $ parameters ): self
57
- {
58
- $ this ->parameters = $ parameters ;
59
-
60
- return $ this ;
51
+ $ this ->setParameters ($ parameters )->setMessages ($ messages );
61
52
}
62
53
63
54
/**
64
55
* @inheritdoc
65
56
*/
66
- public function getIncludes (): iterable
57
+ public function getFields (): iterable
67
58
{
68
- if (array_key_exists (static ::PARAM_INCLUDE , $ this ->getParameters ()) === true ) {
69
- $ splitByDot = function (string $ path ): iterable {
70
- foreach ($ this ->splitStringAndCheckNoEmpties (static ::PARAM_INCLUDE , $ path , '. ' ) as $ link ) {
71
- yield $ link ;
72
- }
73
- };
74
-
75
- $ includes = $ this ->getParameters ()[static ::PARAM_INCLUDE ];
76
- foreach ($ this ->splitCommaSeparatedStringAndCheckNoEmpties (static ::PARAM_INCLUDE , $ includes ) as $ path ) {
77
- yield $ path => $ splitByDot ($ path );
78
- }
79
- }
59
+ return $ this ->getFieldsImpl ($ this ->getParameters (), $ this ->getMessage (static ::MSG_ERR_INVALID_PARAMETER ));
80
60
}
81
61
82
62
/**
83
63
* @inheritdoc
84
64
*/
85
- public function getFields (): iterable
65
+ public function getIncludes (): iterable
86
66
{
87
- if (array_key_exists (static ::PARAM_FIELDS , $ this ->getParameters ()) === true ) {
88
- $ fields = $ this ->getParameters ()[st
341A
atic ::PARAM_FIELDS ];
89
- if (is_array ($ fields ) === false || empty ($ fields ) === true ) {
90
- throw new JsonApiException ($ this ->createParameterError (static ::PARAM_FIELDS ));
91
- }
92
-
93
- foreach ($ fields as $ type => $ fieldList ) {
94
- yield $ type => $ this ->splitCommaSeparatedStringAndCheckNoEmpties ($ type , $ fieldList );
95
- }
96
- }
67
+ return $ this ->getIncludesImpl ($ this ->getParameters (), $ this ->getMessage (static ::MSG_ERR_INVALID_PARAMETER ));
97
68
}
98
69
99
70
/**
100
71
* @inheritdoc
101
72
*/
102
73
public function getSorts (): iterable
103
74
{
104
- if (array_key_exists (static ::PARAM_SORT , $ this ->getParameters ()) === true ) {
105
- $ sorts = $ this ->getParameters ()[static ::PARAM_SORT ];
106
- foreach ($ this ->splitCommaSeparatedStringAndCheckNoEmpties (static ::PARAM_SORT , $ sorts ) as $ orderAndField ) {
107
- switch ($ orderAndField [0 ]) {
108
- case '- ' :
109
- $ isAsc = false ;
110
- $ field = substr ($ orderAndField , 1 );
111
- break ;
112
- case '+ ' :
113
- $ isAsc = true ;
114
- $ field = substr ($ orderAndField , 1 );
115
- break ;
116
- default :
117
- $ isAsc = true ;
118
- $ field = $ orderAndField ;
119
- break ;
120
- }
121
-
122
- yield $ field => $ isAsc ;
123
- }
124
- }
75
+ return $ this ->getSortsImpl ($ this ->getParameters (), $ this ->getMessage (static ::MSG_ERR_INVALID_PARAMETER ));
125
76
}
126
77
127
78
/**
128
- * @param string $paramName
129
- * @param string|mixed $shouldBeString
130
- * @param string $separator
131
- *
132
- * @return iterable
133
- */
134
- protected function splitString (string $ paramName , $ shouldBeString , string $ separator ): iterable
135
- {
136
- if (is_string ($ shouldBeString ) === false || ($ trimmed = trim ($ shouldBeString )) === '' ) {
137
- throw new JsonApiException ($ this ->createParameterError ($ paramName ));
138
- }
139
-
140
- foreach (explode ($ separator , $ trimmed ) as $ value ) {
141
- yield $ value ;
142
- }
143
- }
144
-
145
- /**
146
- * @param string $paramName
147
- * @param string|mixed $shouldBeString
148
- * @param string $separator
79
+ * @param array $parameters
149
80
*
150
- * @return iterable
81
+ * @return self
151
82
*/
152
- protected function splitStringAndCheckNoEmpties ( string $ paramName , $ shouldBeString , string $ separator ): iterable
83
+ protected function setParameters ( array $ parameters ): self
153
84
{
154
- foreach ($ this ->splitString ($ paramName , $ shouldBeString , $ separator ) as $ value ) {
155
- $ trimmedValue = trim ($ value );
156
- if (($ trimmedValue ) === '' ) {
157
- throw new JsonApiException ($ this ->createParameterError ($ paramName ));
158
- }
159
-
160
- yield $ trimmedValue ;
161
- }
162
- }
85
+ $ this ->parameters = $ parameters ;
163
86
164
- /**
165
- * @param string $paramName
166
- * @param string|mixed $shouldBeString
167
- *
168
- * @return iterable
169
- */
170
- protected function splitCommaSeparatedStringAndCheckNoEmpties (string $ paramName , $ shouldBeString ): iterable
171
- {
172
- return $ this ->splitStringAndCheckNoEmpties ($ paramName , $ shouldBeString , ', ' );
87
+ return $ this ;
173
88
}
174
89
175
90
/**
@@ -181,28 +96,15 @@ protected function getParameters(): array
181
96
}
182
97
183
98
/**
184
- * @param string $parameterName
185
- *
186
- * @return Error
187
- */
188
- protected function createParameterError (string $ parameterName ): Error
189
- {
190
- return $ this ->createQueryError ($ parameterName , static ::MSG_ERR_INVALID_PARAMETER );
191
- }
192
-
193
- /**
194
- * @param string $name
195
- * @param string $title
99
+ * @param array $messages
196
100
*
197
- * @return Error
101
+ * @return self
198
102
*/
199
- protected function createQueryError ( string $ name , string $ title ): Error
103
+ protected function setMessages (? array $ messages ): self
200
104
{
201
- $ title = $ this ->getMessage ($ title );
202
- $ source = [Error::SOURCE_PARAMETER => $ name ];
203
- $ error = new Error (null , null , null , null , $ title , null , $ source );
105
+ $ this ->messages = $ messages ;
204
106
205
- return $ error ;
107
+ return $ this ;
206
108
}
207
109
208
110
/**
0 commit comments