-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONApiRequest.php
183 lines (158 loc) · 3.96 KB
/
JSONApiRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace JsonApiBundle\Request;
use ICanBoogie\Inflector;
use Symfony\Component\HttpFoundation\RequestStack;
class JSONApiRequest
{
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var string
*/
private $contentType = 'application/vnd.api+json';
/**
* TestRequest constructor.
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
/**
* @return null|\Symfony\Component\HttpFoundation\Request
*/
public function getRequest()
{
return $this->requestStack->getCurrentRequest();
}
/**
* @param $separator
* @param $request
*
* @return array
*/
private function getArraySeparator($separator, $request)
{
$requestString = str_replace(' ', '', $request);
return explode($separator, $requestString);
}
/**
* @return string
*/
public function getMethod()
{
return $this->getRequest()->getMethod();
}
/**
* Return attributes for sort action
*
* @return array
*/
public function getSortAttributes()
{
return $this->getArraySeparator(',', $this->getRequest()->query->get('sort'));
}
/**
* @return array
*/
public function getSparseFieldAttributes()
{
$result = [];
$fields = $this->getRequest()->query->get('fields');
if ($fields) {
foreach ($fields as $key => $value) {
$result[$key] = $this->getArraySeparator(',', $value);
}
}
return $result;
}
/**
* @return array
*/
public function getIncludeAttributes()
{
return $this->getArraySeparator(',', $this->getRequest()->query->get('include'));
}
/**
* @return array
*/
public function getPaginationAttributes()
{
return $this->getRequest()->query->get('page');
}
/**
* @return array
*/
private function parseJson()
{
$data = [];
if ($this->getRequest()->getMethod() === 'POST' ||
$this->getRequest()->getMethod() === 'PUT'
) {
if ($this->getRequest()->headers->get('content-type') == $this->contentType) {
$data = json_decode($this->getRequest()->getContent(), true);
// TODO check correct format of request json
}
}
return $data;
}
/**
* @return array
*/
public function getDataSection()
{
$data = $this->parseJson();
return (!empty($data)) ? $data['data'] : [];
}
/**
* @param $key
*
* @return array
*/
public function parseDataSectionByKey($key)
{
$data = $this->parseJson();
if (array_key_exists($key, $data['data'])) {
return (!empty($data)) ? $data['data'][$key] : [];
}
return [];
}
/**
* @return array
*/
public function getDataAttributes()
{
return $this->parseDataSectionByKey('attributes');
}
/**
* @return array
*/
public function getRelationSection()
{
return $this->parseDataSectionByKey('relationships');
}
/**
* @return string
*/
public function getClassNameByType($type)
{
$invector = Inflector::get(Inflector::DEFAULT_LOCALE);
return $invector->singularize($invector->camelize($type, Inflector::UPCASE_FIRST_LETTER));
}
/**
* @return array|string
*/
public function getType()
{
// if method get we can get type from url
if ($this->getRequest()->getMethod() === 'GET') {
$array = explode('/', $this->getRequest()->getPathInfo());
return end($array);
} else {
// in another situation it must be in body of request (methods POST | PUT)
return $this->parseDataSectionByKey('type');
}
}
}