1
|
|
|
<?php namespace Pz\Doctrine\Rest; |
2
|
|
|
|
3
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
4
|
|
|
use Pz\Doctrine\Rest\Exceptions\RestException; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
|
7
|
|
|
class RestRequest implements RestRequestContract |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Request |
11
|
|
|
*/ |
12
|
|
|
protected $http; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
protected $isRelationships = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* RestRequest constructor. |
21
|
|
|
* |
22
|
|
|
* @param Request $request |
23
|
|
|
*/ |
24
|
35 |
|
public function __construct(Request $request) |
25
|
|
|
{ |
26
|
35 |
|
$this->http = $request; |
27
|
35 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return Request |
31
|
|
|
*/ |
32
|
35 |
|
public function http() |
33
|
|
|
{ |
34
|
35 |
|
return $this->http; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
2 |
|
public function getBasePath() |
41
|
|
|
{ |
42
|
2 |
|
return $this->http()->getPathInfo(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
19 |
|
public function getBaseUrl() |
49
|
|
|
{ |
50
|
19 |
|
return $this->http()->getBaseUrl(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return int|array |
55
|
|
|
*/ |
56
|
19 |
|
public function getId() |
57
|
|
|
{ |
58
|
19 |
|
return $this->http()->get('id'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
* @throws RestException |
64
|
|
|
*/ |
65
|
14 |
|
public function getData() |
66
|
|
|
{ |
67
|
14 |
|
$request = $this->http()->request; |
68
|
|
|
|
69
|
14 |
|
if ($request->has('data')) { |
70
|
13 |
|
return $request->get('data'); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
throw RestException::missingRootData(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get jsonapi fieldsets. |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
19 |
|
public function getFields() |
82
|
|
|
{ |
83
|
19 |
|
return $this->http()->get('fields'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return mixed|null |
88
|
|
|
*/ |
89
|
18 |
|
public function getFilter() |
90
|
|
|
{ |
91
|
18 |
|
if ($query = $this->http()->get('filter')) { |
92
|
7 |
|
if (is_string($query) && (null !== ($json = json_decode($query, true)))) { |
93
|
1 |
|
return $json; |
94
|
|
|
} |
95
|
|
|
|
96
|
7 |
|
return $query; |
97
|
|
|
} |
98
|
|
|
|
99
|
11 |
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array|null |
104
|
|
|
*/ |
105
|
12 |
|
public function getOrderBy() |
106
|
|
|
{ |
107
|
12 |
|
if ($sort = $this->http()->get('sort')) { |
108
|
1 |
|
$fields = explode(',', $sort); |
109
|
1 |
|
$orderBy = []; |
110
|
|
|
|
111
|
1 |
|
foreach ($fields as $field) { |
112
|
1 |
|
$direction = 'ASC'; |
113
|
1 |
|
if ($field[0] === '-') { |
114
|
1 |
|
$field = substr($field, 1); |
115
|
1 |
|
$direction = 'DESC'; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$orderBy[$field] = $direction; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $orderBy; |
122
|
|
|
} |
123
|
|
|
|
124
|
11 |
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return int|null |
129
|
|
|
*/ |
130
|
12 |
|
public function getStart() |
131
|
|
|
{ |
132
|
12 |
|
if (($page = $this->http()->get('page')) && $this->getLimit() !== null) { |
133
|
2 |
|
if (isset($page['number']) && is_numeric($page['number'])) { |
134
|
2 |
|
return ($page['number'] - 1) * $this->getLimit(); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
return isset($page['offset']) && is_numeric($page['offset']) ? (int) $page['offset'] : 0; |
138
|
|
|
} |
139
|
|
|
|
140
|
11 |
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return int|null |
145
|
|
|
*/ |
146
|
12 |
|
public function getLimit() |
147
|
|
|
{ |
148
|
12 |
|
if ($page = $this->http()->get('page')) { |
149
|
2 |
|
if (isset($page['number']) && is_numeric($page['number'])) { |
150
|
2 |
|
return isset($page['size']) && is_numeric($page['size']) ? |
151
|
2 |
|
(int) $page['size'] : static::DEFAULT_LIMIT; |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return isset($page['limit']) && is_numeric($page['limit']) ? |
155
|
1 |
|
(int) $page['limit'] : static::DEFAULT_LIMIT; |
156
|
|
|
} |
157
|
|
|
|
158
|
11 |
|
return null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array|string|null |
163
|
|
|
*/ |
164
|
19 |
|
public function getInclude() |
165
|
|
|
{ |
166
|
19 |
|
$include = @explode(',', $this->http()->get('include')); |
167
|
|
|
|
168
|
19 |
|
if (!is_array($include)) { |
|
|
|
|
169
|
|
|
RestException::invalidInclude(); |
170
|
|
|
} |
171
|
|
|
|
172
|
19 |
|
return $include; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return array|string|null |
177
|
|
|
*/ |
178
|
19 |
|
public function getExclude() |
179
|
|
|
{ |
180
|
19 |
|
return $this->http()->get('exclude'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param null|bool $value |
185
|
|
|
* |
186
|
|
|
* @return bool|null |
187
|
|
|
*/ |
188
|
19 |
|
public function isRelationships($value = null) |
189
|
|
|
{ |
190
|
19 |
|
if ($value !== null) { |
191
|
4 |
|
$this->isRelationships = $value; |
192
|
|
|
} |
193
|
|
|
|
194
|
19 |
|
return $this->isRelationships; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|