|
1
|
|
|
<?php namespace Pz\Doctrine\Rest; |
|
2
|
|
|
|
|
3
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
4
|
|
|
|
|
5
|
|
|
abstract class RestRequestAbstract extends Request |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Json API type. |
|
9
|
|
|
*/ |
|
10
|
|
|
const JSON_API_CONTENT_TYPE = 'application/vnd.api+json'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Default limit for list. |
|
14
|
|
|
*/ |
|
15
|
|
|
const DEFAULT_LIMIT = 1000; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Authorize rest request. |
|
19
|
|
|
* Entity will be object for get,update,delete actions. |
|
20
|
|
|
* Entity will be string for index,create action. |
|
21
|
|
|
* |
|
22
|
|
|
* @param object|string $entity |
|
23
|
|
|
* |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract public function authorize($entity); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function isAcceptJsonApi() |
|
32
|
|
|
{ |
|
33
|
2 |
|
return in_array(static::JSON_API_CONTENT_TYPE, $this->getAcceptableContentTypes()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return int|array |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getId() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->get('id'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get jsonapi fieldsets. |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function getFields() |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $this->get('fields', []); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return mixed|null |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function getFilter() |
|
58
|
|
|
{ |
|
59
|
2 |
|
if ($query = $this->get('filter')) { |
|
60
|
2 |
|
if (is_string($query) && (null !== ($json = json_decode($query, true)))) { |
|
61
|
|
|
return $json; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
return $query; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array|null |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function getOrderBy() |
|
74
|
|
|
{ |
|
75
|
2 |
|
if ($fields = explode(',', $this->get('sort'))) { |
|
76
|
2 |
|
$orderBy = []; |
|
77
|
|
|
|
|
78
|
2 |
|
foreach ($fields as $field) { |
|
79
|
2 |
|
if (empty($field)) continue; |
|
80
|
|
|
|
|
81
|
2 |
|
$direction = 'ASC'; |
|
82
|
2 |
|
if ($field[0] === '-') { |
|
83
|
2 |
|
$field = substr($field, 1); |
|
84
|
2 |
|
$direction = 'DESC'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
$orderBy[$field] = $direction; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $orderBy; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return int|null |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getStart() |
|
100
|
|
|
{ |
|
101
|
2 |
|
if ($page = $this->get('page')) { |
|
102
|
2 |
|
return isset($page['offset']) && is_numeric($page['offset']) ? (int) $page['offset'] : 0; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return int|null |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function getLimit() |
|
112
|
|
|
{ |
|
113
|
2 |
|
if ($page = $this->get('page')) { |
|
114
|
2 |
|
return isset($page['limit']) && is_numeric($page['limit']) ? (int) $page['limit'] : static::DEFAULT_LIMIT; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return array|string|null |
|
122
|
|
|
*/ |
|
123
|
2 |
|
public function getInclude() |
|
124
|
|
|
{ |
|
125
|
2 |
|
return $this->get('include'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array|string|null |
|
130
|
|
|
*/ |
|
131
|
2 |
|
public function getExclude() |
|
132
|
|
|
{ |
|
133
|
2 |
|
return $this->get('exclude'); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|