|
1
|
|
|
<?php namespace Pz\LaravelDoctrine\Rest; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
4
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
|
5
|
|
|
use Pz\Doctrine\Rest\Exceptions\RestException; |
|
6
|
|
|
|
|
7
|
|
|
class RestRequest extends FormRequest implements RestRequestContract |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @return array |
|
11
|
|
|
*/ |
|
12
|
5 |
|
public function rules() |
|
13
|
|
|
{ |
|
14
|
|
|
return [ |
|
15
|
5 |
|
'filter' => 'sometimes|required', |
|
16
|
|
|
'include' => 'sometimes|required|array', |
|
17
|
|
|
'exclude' => 'sometimes|required|array', |
|
18
|
|
|
'fields' => 'sometimes|required|array', |
|
19
|
|
|
'sort' => 'sometimes|required|string', |
|
20
|
|
|
'page' => 'sometimes|required|array', |
|
21
|
|
|
'page.number' => 'sometimes|required|numeric', |
|
22
|
|
|
'page.size' => 'sometimes|required|numeric', |
|
23
|
|
|
'page.limit' => 'sometimes|required|numeric', |
|
24
|
|
|
'page.offset' => 'sometimes|required|numeric', |
|
25
|
|
|
]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return array |
|
30
|
|
|
* @throws RestException |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function getData() |
|
33
|
|
|
{ |
|
34
|
2 |
|
if ((null === $data = $this->get('data')) || !is_array($data)) { |
|
35
|
1 |
|
throw RestException::missingRootData(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
return $data; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array|null |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function getOrderBy() |
|
45
|
|
|
{ |
|
46
|
2 |
|
if ($sort = $this->input('sort')) { |
|
47
|
1 |
|
$fields = explode(',', $sort); |
|
48
|
1 |
|
$orderBy = []; |
|
49
|
|
|
|
|
50
|
1 |
|
foreach ($fields as $field) { |
|
51
|
1 |
|
if (empty($field)) continue; |
|
52
|
|
|
|
|
53
|
1 |
|
$direction = 'ASC'; |
|
54
|
1 |
|
if ($field[0] === '-') { |
|
55
|
1 |
|
$field = substr($field, 1); |
|
56
|
1 |
|
$direction = 'DESC'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$orderBy[$field] = $direction; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $orderBy; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return null|int |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function getStart() |
|
72
|
|
|
{ |
|
73
|
2 |
|
if (null !== ($limit = $this->getLimit())) { |
|
74
|
1 |
|
if ($number = $this->input('page.number')) { |
|
75
|
1 |
|
return ($number - 1) * $limit; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
return $this->input('page.offset', 0); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return int|null |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function getLimit() |
|
88
|
|
|
{ |
|
89
|
2 |
|
if ($this->has('page')) { |
|
90
|
1 |
|
if ($this->has('page.number')) { |
|
91
|
1 |
|
return $this->input('page.size', static::DEFAULT_LIMIT); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
return $this->input('page.limit', static::DEFAULT_LIMIT); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
return null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return \Illuminate\Routing\Route|object|string |
|
102
|
|
|
*/ |
|
103
|
4 |
|
public function getId() |
|
104
|
|
|
{ |
|
105
|
4 |
|
return $this->route('id'); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return array|null |
|
110
|
|
|
*/ |
|
111
|
5 |
|
public function getExclude() |
|
112
|
|
|
{ |
|
113
|
5 |
|
return $this->input('exclude'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array|null |
|
118
|
|
|
*/ |
|
119
|
5 |
|
public function getInclude() |
|
120
|
|
|
{ |
|
121
|
5 |
|
return $this->input('include'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
5 |
|
public function getFields() |
|
125
|
|
|
{ |
|
126
|
5 |
|
return $this->input('fields'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return array|string|null |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function getFilter() |
|
133
|
|
|
{ |
|
134
|
2 |
|
return $this->input('filter'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
5 |
|
protected function passesAuthorization() |
|
141
|
|
|
{ |
|
142
|
5 |
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: