1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SendsRequestsTrait.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec https://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:JsonAPIClient! |
9
|
|
|
* @subpackage Clients |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 05.05.18 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\JsonAPIClient\Clients; |
18
|
|
|
|
19
|
|
|
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface; |
20
|
|
|
use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface; |
21
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ContainerInterface; |
22
|
|
|
use Neomerx\JsonApi\Encoder\Parameters\EncodingParameters; |
23
|
|
|
use Neomerx\JsonApi\Http\Headers\MediaType; |
24
|
|
|
|
25
|
|
|
use IPub\JsonAPIClient\Encoders; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Sender trait |
29
|
|
|
* |
30
|
|
|
* @package iPublikuj:JsonAPIClient! |
31
|
|
|
* @subpackage Clients |
32
|
|
|
* |
33
|
|
|
* @author Adam Kadlec <[email protected]> |
34
|
|
|
* |
35
|
|
|
* @property array $headers |
36
|
|
|
*/ |
37
|
1 |
|
trait TSendsRequests |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var ContainerInterface |
41
|
|
|
*/ |
42
|
|
|
protected $schemas; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Encoders\ISerializer |
46
|
|
|
*/ |
47
|
|
|
protected $serializer; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param mixed $record |
51
|
|
|
* @param string[]|NULL $fields |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
protected function serializeRecord($record, array $fields = NULL) : array |
56
|
|
|
{ |
57
|
|
|
$parameters = NULL; |
58
|
|
|
|
59
|
|
|
if ($fields !== NULL) { |
60
|
|
|
$resourceType = $this->schemas->getSchema($record)->getResourceType(); |
61
|
|
|
$parameters = $this->createQueryParameters(NULL, [$resourceType => $fields]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->serializer->serializeData($record, $parameters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param bool $body whether HTTP request body is being sent |
69
|
|
|
* |
70
|
|
|
* @return string[] |
71
|
|
|
*/ |
72
|
|
|
protected function jsonApiHeaders($body = FALSE) : array |
73
|
|
|
{ |
74
|
|
|
$headers = ['Accept' => MediaType::JSON_API_MEDIA_TYPE]; |
75
|
|
|
|
76
|
|
|
if ($body === TRUE) { |
77
|
|
|
$headers['Content-Type'] = MediaType::JSON_API_MEDIA_TYPE; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$headers = array_merge($this->headers, $headers); |
81
|
|
|
|
82
|
|
|
return $headers; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param EncodingParametersInterface $parameters |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function parseQuery(EncodingParametersInterface $parameters) : array |
91
|
|
|
{ |
92
|
|
|
return array_filter(array_merge(/*(array) $parameters->getUnrecognizedParameters(),*/ |
93
|
|
|
[ |
94
|
|
|
BaseQueryParserInterface::PARAM_INCLUDE => |
95
|
|
|
implode(',', (array) $parameters->getIncludePaths()), |
96
|
|
|
BaseQueryParserInterface::PARAM_FIELDS => |
97
|
|
|
$this->parseQueryFieldsets((array) $parameters->getFieldSets()), |
98
|
|
|
], [])); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param EncodingParametersInterface $parameters |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
protected function parseSearchQuery(EncodingParametersInterface $parameters) : array |
107
|
|
|
{ |
108
|
|
|
return array_filter(array_merge($this->parseQuery($parameters)/*, [ |
109
|
|
|
BaseQueryParserInterface::PARAM_SORT => |
110
|
|
|
implode(',', (array) $parameters->getSortParameters()), |
111
|
|
|
BaseQueryParserInterface::PARAM_PAGE => |
112
|
|
|
$parameters->getPaginationParameters(), |
113
|
|
|
BaseQueryParserInterface::PARAM_FILTER => |
114
|
|
|
$parameters->getFilteringParameters(), |
115
|
|
|
]*/, [])); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $fieldsets |
120
|
|
|
* |
121
|
|
|
* @return string[] |
122
|
|
|
*/ |
123
|
|
|
private function parseQueryFieldsets(array $fieldsets) : array |
124
|
|
|
{ |
125
|
|
|
return array_map(function ($values) : string { |
126
|
|
|
return implode(',', (array) $values); |
127
|
|
|
}, $fieldsets); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array|NULL $includePaths |
132
|
|
|
* @param array|NULL $fieldSets |
133
|
|
|
* |
134
|
|
|
* @return EncodingParametersInterface |
135
|
|
|
*/ |
136
|
|
|
private function createQueryParameters(array $includePaths = NULL, array $fieldSets = NULL) : EncodingParametersInterface |
137
|
|
|
{ |
138
|
|
|
return new EncodingParameters($includePaths, $fieldSets); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|