|
1
|
|
|
<?php namespace Pz\Doctrine\Rest; |
|
2
|
|
|
|
|
3
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
5
|
|
|
|
|
6
|
|
|
class RestRequest implements RestRequestContract |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var Request |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $http; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* RestRequest constructor. |
|
15
|
|
|
* |
|
16
|
|
|
* @param Request $request |
|
17
|
|
|
*/ |
|
18
|
15 |
|
public function __construct(Request $request) |
|
19
|
|
|
{ |
|
20
|
15 |
|
$this->http = $request; |
|
21
|
15 |
|
} |
|
22
|
|
|
|
|
23
|
15 |
|
public function http() |
|
24
|
|
|
{ |
|
25
|
15 |
|
return $this->http; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
13 |
|
public function isAcceptJsonApi() |
|
32
|
|
|
{ |
|
33
|
13 |
|
return in_array(static::JSON_API_CONTENT_TYPE, $this->http()->getAcceptableContentTypes()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function isContentJsonApi() |
|
40
|
|
|
{ |
|
41
|
3 |
|
return $this->http()->headers->get('CONTENT_TYPE') === static::JSON_API_CONTENT_TYPE; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return int|array |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function getId() |
|
48
|
|
|
{ |
|
49
|
6 |
|
return $this->http()->get('id'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get jsonapi fieldsets. |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
13 |
|
public function getFields() |
|
58
|
|
|
{ |
|
59
|
13 |
|
return $this->http()->get('fields'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return mixed|null |
|
64
|
|
|
*/ |
|
65
|
7 |
|
public function getFilter() |
|
66
|
|
|
{ |
|
67
|
7 |
|
if ($query = $this->http()->get('filter')) { |
|
68
|
1 |
|
if (is_string($query) && (null !== ($json = json_decode($query, true)))) { |
|
69
|
1 |
|
return $json; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return $query; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
6 |
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array|null |
|
80
|
|
|
*/ |
|
81
|
7 |
|
public function getOrderBy() |
|
82
|
|
|
{ |
|
83
|
7 |
|
if ($sort = $this->http()->get('sort')) { |
|
84
|
1 |
|
$fields = explode(',', $sort); |
|
85
|
1 |
|
$orderBy = []; |
|
86
|
|
|
|
|
87
|
1 |
|
foreach ($fields as $field) { |
|
88
|
1 |
|
if (empty($field)) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$direction = 'ASC'; |
|
93
|
1 |
|
if ($field[0] === '-') { |
|
94
|
1 |
|
$field = substr($field, 1); |
|
95
|
1 |
|
$direction = 'DESC'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
$orderBy[$field] = $direction; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $orderBy; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
6 |
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return int|null |
|
109
|
|
|
*/ |
|
110
|
7 |
|
public function getStart() |
|
111
|
|
|
{ |
|
112
|
7 |
|
if (($page = $this->http()->get('page')) && $this->getLimit() !== null) { |
|
113
|
2 |
|
if (isset($page['number']) && is_numeric($page['number'])) { |
|
114
|
1 |
|
return ($page['number'] - 1) * $this->getLimit(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
return isset($page['offset']) && is_numeric($page['offset']) ? (int) $page['offset'] : 0; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
6 |
|
return null; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return int|null |
|
125
|
|
|
*/ |
|
126
|
7 |
|
public function getLimit() |
|
127
|
|
|
{ |
|
128
|
7 |
|
if ($page = $this->http()->get('page')) { |
|
129
|
2 |
|
if (isset($page['number']) && is_numeric($page['number'])) { |
|
130
|
1 |
|
return isset($page['size']) && is_numeric($page['size']) ? |
|
131
|
1 |
|
(int) $page['size'] : static::DEFAULT_LIMIT; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
2 |
|
return isset($page['limit']) && is_numeric($page['limit']) ? |
|
135
|
2 |
|
(int) $page['limit'] : static::DEFAULT_LIMIT; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
6 |
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return array|string|null |
|
143
|
|
|
*/ |
|
144
|
13 |
|
public function getInclude() |
|
145
|
|
|
{ |
|
146
|
13 |
|
return $this->http()->get('include'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return array|string|null |
|
151
|
|
|
*/ |
|
152
|
13 |
|
public function getExclude() |
|
153
|
|
|
{ |
|
154
|
13 |
|
return $this->http()->get('exclude'); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|