10000 Close #204 · rzenkov/json-api@5fd7456 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5fd7456

Browse files
committed
Close neomerx#204
1 parent e6140f1 commit 5fd7456

File tree

2 files changed

+182
-120
lines changed

2 files changed

+182
-120
lines changed

src/Http/Query/BaseQueryParser.php

Lines changed: 22 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717
*/
1818

1919
use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface;
20-
use Neomerx\JsonApi\Document\Error;
21-
use Neomerx\JsonApi\Exceptions\JsonApiException;
2220

2321
/**
2422
* @package Neomerx\JsonApi
2523
*/
2624
class BaseQueryParser implements BaseQueryParserInterface
2725
{
26+
use BaseQueryParserTrait {
27+
BaseQueryParserTrait::getFields as getFieldsImpl;
28+
BaseQueryParserTrait::getIncludes as getIncludesImpl;
29+
BaseQueryParserTrait::getSorts as getSortsImpl;
30+
}
31+
2832
/** Message */
2933
public const MSG_ERR_INVALID_PARAMETER = 'Invalid Parameter.';
3034

@@ -44,132 +48,43 @@ class BaseQueryParser implements BaseQueryParserInterface
4448
*/
4549
public function __construct(array $parameters = [], array $messages = null)
4650
{
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);
6152
}
6253

6354
/**
6455
* @inheritdoc
6556
*/
66-
public function getIncludes(): iterable
57+
public function getFields(): iterable
6758
{
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));
8060
}
8161

8262
/**
8363
* @inheritdoc
8464
*/
85-
public function getFields(): iterable
65+
public function getIncludes(): iterable
8666
{
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));
9768
}
9869

9970
/**
10071
* @inheritdoc
10172
*/
10273
public function getSorts(): iterable
10374
{
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));
12576
}
12677

12778
/**
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
14980
*
150-
* @return iterable
81+
* @return self
15182
*/
152-
protected function splitStringAndCheckNoEmpties(string $paramName, $shouldBeString, string $separator): iterable
83+
protected function setParameters(array $parameters): self
15384
{
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;
16386

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;
17388
}
17489

17590
/**
@@ -181,28 +96,15 @@ protected function getParameters(): array
18196
}
18297

18398
/**
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
196100
*
197-
* @return Error
101+
* @return self
198102
*/
199-
protected function createQueryError(string $name, string $title): Error
103+
protected function setMessages(?array $messages): self
200104
{
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;
204106

205-
return $error;
107+
return $this;
206108
}
207109

208110
/**
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php namespace Neomerx\JsonApi\Http\Query;
2+
3+
/**
4+
* Copyright 2015-2018 info@neomerx.com
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+
protected function getIncludes(array $parameters, string $errorTitle): iterable
36+
{
37+
if (array_key_exists(P::PARAM_INCLUDE, $parameters) === true) {
38+
$splitByDot = function (string $path) use ($errorTitle): iterable {
39+
foreach ($this->splitStringAndCheckNoEmpties(P::PARAM_INCLUDE, $path, '.', $errorTitle) as $link) {
40+
yield $link;
41+
}
42+
};
43+
44+
$paramName = P::PARAM_INCLUDE;
45+
$includes = $parameters[P::PARAM_INCLUDE];
46+
foreach ($this->splitCommaSeparatedStringAndCheckNoEmpties($paramName, $includes, $errorTitle) as $path) {
47+
yield $path => $splitByDot($path);
48+
}
49+
}
50+
}
51+
52+
/**
53+
* @param array $parameters
54+
* @param string $errorTitle
55+
*
56+
* @return iterable
57+
*/
58+
protected function getFields(array $parameters, string $errorTitle): iterable
59+
{
60+
if (array_key_exists(P::PARAM_FIELDS, $parameters) === true) {
61+
$fields = $parameters[P::PARAM_FIELDS];
62+
if (is_array($fields) === false || empty($fields) === true) {
63+
throw new JsonApiException($this->createParameterError(P::PARAM_FIELDS, $errorTitle));
64+
}
65+
66+
foreach ($fields as $type => $fieldList) {
67+
yield $type => $this->splitCommaSeparatedStringAndCheckNoEmpties($type, $fieldList, $errorTitle);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* @param array $parameters
74+
* @param string $errorTitle
75+
*
76+
* @return iterable
77+
*/
78+
protected function getSorts(array $parameters, string $errorTitle): iterable
79+
{
80+
if (array_key_exists(P::PARAM_SORT, $parameters) === true) {
81+
$sorts = $parameters[P::PARAM_SORT];
82+
$values = $this->splitCommaSeparatedStringAndCheckNoEmpties(P::PARAM_SORT, $sorts, $errorTitle);
83+
foreach ($values as $orderAndField) {
84+
switch ($orderAndField[0]) {
85+
case '-':
86+
$isAsc = false;
87+
$field = substr($orderAndField, 1);
88+
break;
89+
case '+':
90+
$isAsc = true;
91+
$field = substr($orderAndField, 1);
92+
break;
93+
default:
94+
$isAsc = true;
95+
$field = $orderAndField;
96+
break;
97+
}
98+
99+
yield $field => $isAsc;
100+
}
101+
}
102+
}
103+
104+
/**
105+
* @param string $paramName
106+
* @param string|mixed $shouldBeString
107+
* @param string $errorTitle
108+
*
109+
* @return iterable
110+
*/
111+
private function splitCommaSeparatedStringAndCheckNoEmpties(
112+
string $paramName,
113+
$shouldBeString,
114+
string $errorTitle
115+
): iterable {
116+
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+
private function splitStringAndCheckNoEmpties(
128+
string $paramName,
129+
$shouldBeString,
130+
string $separator,
131+
string $errorTitle
132+
): iterable {
133+
if (is_string($shouldBeString) === false || ($trimmed = trim($shouldBeString)) === '') {
134+
throw new JsonApiException($this->createParameterError($paramName, $errorTitle));
135+
}
136+
137+
foreach (explode($separator, $trimmed) as $value) {
138+
$trimmedValue = trim($value);
139+
if (($trimmedValue) === '') {
140+
throw new JsonApiException($this->createParameterError($paramName, $errorTitle));
141+
}
142+
143+
yield $trimmedValue;
144+
}
145+
}
146+
147+
/**
148+
* @param string $paramName
149+
* @param string $errorTitle
150+
*
151+
* @return ErrorInterface
152+
*/
153+
private function createParameterError(string $paramName, string $errorTitle): ErrorInterface
154+
{
155+
$source = [Error::SOURCE_PARAMETER => $paramName];
156+
$error = new Error(null, null, null, null, $errorTitle, null, $source);
157+
158+
return $error;
159+
}
160+
}

0 commit comments

Comments
 (0)
0