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\Http\Query\BaseQueryParserInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @package Neomerx\JsonApi |
23
|
|
|
*/ |
24
|
|
|
class BaseQueryParser implements BaseQueryParserInterface |
25
|
|
|
{ |
26
|
|
|
use BaseQueryParserTrait { |
27
|
|
|
BaseQueryParserTrait::getFields as getFieldsImpl; |
28
|
|
|
BaseQueryParserTrait::getIncludes as getIncludesImpl; |
29
|
|
|
BaseQueryParserTrait::getSorts as getSortsImpl; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** Message */ |
33
|
|
|
public const MSG_ERR_INVALID_PARAMETER = 'Invalid Parameter.'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $parameters; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string[]|null |
42
|
|
|
*/ |
43
|
|
|
private $messages; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $parameters |
47
|
|
|
* @param string[]|null $messages |
48
|
|
|
*/ |
49
|
13 |
|
public function __construct(array $parameters = [], array $messages = null) |
50
|
|
|
{ |
51
|
13 |
|
$this->setParameters($parameters)->setMessages($messages); |
52
|
13 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
4 |
|
public function getFields(): iterable |
58
|
|
|
{ |
59
|
4 |
|
return $this->getFieldsImpl($this->getParameters(), $this->getMessage(static::MSG_ERR_INVALID_PARAMETER)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
10 |
|
public function getIncludes(): iterable |
66
|
|
|
{ |
67
|
10 |
|
return $this->getIncludesImpl($this->getParameters(), $this->getMessage(static::MSG_ERR_INVALID_PARAMETER)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
2 |
|
public function getSorts(): iterable |
74
|
|
|
{ |
75
|
2 |
|
return $this->getSortsImpl($this->getParameters(), $this->getMessage(static::MSG_ERR_INVALID_PARAMETER)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $parameters |
80
|
|
|
* |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
13 |
|
protected function setParameters(array $parameters): self |
84
|
|
|
{ |
85
|
13 |
|
$this->parameters = $parameters; |
86
|
|
|
|
87
|
13 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
13 |
|
protected function getParameters(): array |
94
|
|
|
{ |
95
|
13 |
|
return $this->parameters; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $messages |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
13 |
|
protected function setMessages(?array $messages): self |
104
|
|
|
{ |
105
|
13 |
|
$this->messages = $messages; |
106
|
|
|
|
107
|
13 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $message |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
13 |
|
protected function getMessage(string $message): string |
116
|
|
|
{ |
117
|
13 |
|
$hasTranslation = $this->messages !== null && array_key_exists($message, $this->messages) === false; |
118
|
|
|
|
119
|
13 |
|
return $hasTranslation === true ? $this->messages[$message] : $message; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|