1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Http\Query; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Neomerx\JsonApi |
25
|
|
|
*/ |
26
|
|
|
class BaseQueryParser implements BaseQueryParserInterface |
27
|
|
|
{ |
28
|
|
|
use BaseQueryParserTrait { |
29
|
|
|
BaseQueryParserTrait::getFields as getFieldsImpl; |
30
|
|
|
BaseQueryParserTrait::getIncludes as getIncludesImpl; |
31
|
|
|
BaseQueryParserTrait::getIncludePaths as getIncludePathsImpl; |
32
|
|
|
BaseQueryParserTrait::getSorts as getSortsImpl; |
33
|
|
|
BaseQueryParserTrait::getProfileUrls as getProfileUrlsImpl; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** Message */ |
37
|
|
|
public const MSG_ERR_INVALID_PARAMETER = 'Invalid Parameter.'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $parameters; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string[]|null |
46
|
|
|
*/ |
47
|
|
|
private $messages; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $parameters |
51
|
|
|
* @param string[]|null $messages |
52
|
|
|
*/ |
53
|
14 |
|
public function __construct(array $parameters = [], array $messages = null) |
54
|
|
|
{ |
55
|
14 |
|
$this->setParameters($parameters)->setMessages($messages); |
56
|
14 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
4 |
|
public function getFields(): iterable |
62
|
|
|
{ |
63
|
4 |
|
return $this->getFieldsImpl($this->getParameters(), $this->getMessage(static::MSG_ERR_INVALID_PARAMETER)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
11 |
|
public function getIncludes(): iterable |
70
|
|
|
{ |
71
|
11 |
|
return $this->getIncludesImpl($this->getParameters(), $this->getMessage(static::MSG_ERR_INVALID_PARAMETER)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
1 |
|
public function getIncludePaths(): iterable |
78
|
|
|
{ |
79
|
1 |
|
return $this->getIncludePathsImpl($this->getParameters(), $this->getMessage(static::MSG_ERR_INVALID_PARAMETER)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
2 |
|
public function getSorts(): iterable |
86
|
|
|
{ |
87
|
2 |
|
return $this->getSortsImpl($this->getParameters(), $this->getMessage(static::MSG_ERR_INVALID_PARAMETER)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
1 |
|
public function getProfileUrls(): iterable |
94
|
|
|
{ |
95
|
1 |
|
return $this->getProfileUrlsImpl($this->getParameters(), $this->getMessage(static::MSG_ERR_INVALID_PARAMETER)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $parameters |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
14 |
|
protected function setParameters(array $parameters): self |
104
|
|
|
{ |
105
|
14 |
|
$this->parameters = $parameters; |
106
|
|
|
|
107
|
14 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
14 |
|
protected function getParameters(): array |
114
|
|
|
{ |
115
|
14 |
|
return $this->parameters; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $messages |
120
|
|
|
* |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
14 |
|
protected function setMessages(?array $messages): self |
124
|
|
|
{ |
125
|
14 |
|
$this->messages = $messages; |
126
|
|
|
|
127
|
14 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $message |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
14 |
|
protected function getMessage(string $message): string |
136
|
|
|
{ |
137
|
14 |
|
return $this->messages[$message] ?? $message; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|