-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSerializer.php
More file actions
193 lines (164 loc) · 5.73 KB
/
Serializer.php
File metadata and controls
193 lines (164 loc) · 5.73 KB
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
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace MixerApi\CollectionView;
use Adbar\Dot;
use Cake\Core\Configure;
use Cake\Datasource\Paging\PaginatedResultSet;
use Cake\Datasource\ResultSetInterface;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Cake\Http\ServerRequest;
use Cake\Utility\Xml;
use Cake\View\Helper\PaginatorHelper;
use RuntimeException;
/**
* Serializes the CollectionView into either JSON or XML.
*/
class Serializer
{
public const BEFORE_SERIALIZE_EVENT = 'MixerApi.CollectionView.beforeSerialize';
public const AFTER_SERIALIZE_EVENT = 'MixerApi.CollectionView.afterSerialize';
/**
* serialized data
*
* @var array
*/
private mixed $data;
/**
* @var array
*/
private array $config;
/**
* If constructed without parameters collection meta data will not be added to HAL $data
*
* @param mixed $serialize the data to be converted into a HAL array
* @param \Cake\Http\ServerRequest|null $request optional ServerRequest
* @param \Cake\View\Helper\PaginatorHelper|null $paginator optional PaginatorHelper
*/
public function __construct(
mixed $serialize,
private ?ServerRequest $request = null,
private ?PaginatorHelper $paginator = null
) {
$this->config = Configure::read('CollectionView');
if ($serialize instanceof ResultSetInterface || $serialize instanceof PaginatedResultSet) {
$this->data = $this->collection($serialize);
} else {
$this->data = $serialize;
}
}
/**
* Serializes as JSON
*
* @param int $jsonOptions JSON options see https://www.php.net/manual/en/function.json-encode.php
* @return string
* @throws \RuntimeException
*/
public function asJson(int $jsonOptions = 0): string
{
EventManager::instance()->dispatch(new Event(self::BEFORE_SERIALIZE_EVENT, $this, [
'type' => 'json',
]));
$json = json_encode($this->data, $jsonOptions);
if ($json === false) {
throw new RuntimeException(json_last_error_msg(), json_last_error());
}
EventManager::instance()->dispatch(new Event(self::AFTER_SERIALIZE_EVENT, $this, [
'type' => 'json',
'data' => $json,
]));
return $json;
}
/**
* Serializes as XML
*
* @param array $options same as Cake\Utility\Xml
* @param string $rootNode the rootNode
* @return string
* @throws \RuntimeException
*/
public function asXml(array $options, string $rootNode = 'response'): string
{
EventManager::instance()->dispatch(new Event(self::BEFORE_SERIALIZE_EVENT, $this, [
'type' => 'xml',
]));
$xml = Xml::fromArray([$rootNode => $this->data], $options)->saveXML();
EventManager::instance()->dispatch(new Event(self::AFTER_SERIALIZE_EVENT, $this, [
'type' => 'xml',
'data' => $xml,
]));
return $xml;
}
/**
* @return mixed
*/
public function getData(): mixed
{
return $this->data;
}
/**
* @param mixed $data The data to be serialized
* @return void
*/
public function setData(mixed $data): void
{
$this->data = $data;
}
/**
* @return \Cake\Http\ServerRequest|null
*/
public function getRequest(): ?ServerRequest
{
return $this->request;
}
/**
* @return \Cake\View\Helper\PaginatorHelper|null
*/
public function getPaginatorHelper(): ?PaginatorHelper
{
return $this->paginator;
}
/**
* @param \Cake\Datasource\ResultSetInterface|\Cake\Datasource\Paging\PaginatedResultSet $resultSet the data to be converted into a HAL array
* @return array
*/
private function collection(mixed $resultSet): array
{
$dot = new Dot();
foreach ($this->config as $key => $value) {
$dot->set($key, $value);
}
$return = $dot->all();
$collection = array_search('{{collection}}', $this->config);
$url = array_search('{{url}}', $return[$collection]);
$count = array_search('{{count}}', $return[$collection]);
$pages = array_search('{{pages}}', $return[$collection]);
$total = array_search('{{total}}', $return[$collection]);
$next = array_search('{{next}}', $return[$collection]);
$prev = array_search('{{prev}}', $return[$collection]);
$first = array_search('{{first}}', $return[$collection]);
$last = array_search('{{last}}', $return[$collection]);
$data = array_search('{{data}}', $this->config);
$return[$collection][$count] = intval($resultSet->count());
$return[$collection][$url] = '';
if ($this->request instanceof ServerRequest) {
$uri = $this->request->getUri();
$query = $uri->getQuery();
$return[$collection][$url] = $uri->getPath();
$return[$collection][$url] .= !empty($query) ? '?' . $query : '';
}
if ($this->paginator instanceof PaginatorHelper) {
$return[$collection][$next] = $this->paginator->next();
$return[$collection][$prev] = $this->paginator->prev();
$return[$collection][$first] = $this->paginator->first();
$return[$collection][$last] = $this->paginator->last();
$return[$collection][$pages] = $this->paginator->total();
$return[$collection][$total] = intval($this->paginator->param('totalCount'));
}
if (empty($return[$collection][$first]) && !empty($return[$collection][$url])) {
$return[$collection][$first] = $return[$collection][$url];
}
$return[$data] = $resultSet->toArray();
return $return;
}
}