forked from joomla/joomla-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonApiView.php
More file actions
365 lines (310 loc) · 11 KB
/
JsonApiView.php
File metadata and controls
365 lines (310 loc) · 11 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\MVC\View;
use Joomla\CMS\Document\JsonapiDocument;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\View\Event\OnGetApiFields;
use Joomla\CMS\Router\Exception\RouteNotFoundException;
use Joomla\CMS\Serializer\JoomlaSerializer;
use Joomla\CMS\Uri\Uri;
use Tobscure\JsonApi\AbstractSerializer;
use Tobscure\JsonApi\Collection;
use Tobscure\JsonApi\Resource;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Base class for a Joomla Json List View
*
* Class holding methods for displaying presentation data.
*
* @since 4.0.0
*/
abstract class JsonApiView extends JsonView
{
/**
* The active document object (Redeclared for typehinting)
*
* @var JsonapiDocument
* @since 3.0
*/
public $document;
/**
* The content type
*
* @var string
*/
protected $type;
/**
* Item relationship
*
* @var array
*
* @since 4.0.0
*/
protected $relationship = [];
/**
* Serializer data
*
* @var AbstractSerializer
* @since 4.0.0
*/
protected $serializer;
/**
* The fields to render item in the documents
*
* @var array
* @since 4.0.0
*/
protected $fieldsToRenderItem = [];
/**
* The fields to render items in the documents
*
* @var array
* @since 4.0.0
*/
protected $fieldsToRenderList = [];
/**
* Constructor.
*
* @param array $config A named configuration array for object construction.
* contentType: the name (optional) of the content type to use for the serialization
*
* @since 4.0.0
*/
public function __construct($config = [])
{
if (\array_key_exists('contentType', $config)) {
$this->type = $config['contentType'];
}
if ($this->serializer === null) {
$this->serializer = new JoomlaSerializer($this->type);
}
parent::__construct($config);
}
/**
* Execute and display a template script.
*
* @param ?array $items Array of items
*
* @return string
*
* @since 4.0.0
*/
public function displayList(?array $items = null)
{
/** @var \Joomla\CMS\MVC\Model\ListModel $model */
$model = $this->getModel();
// Get page query
$currentUrl = Uri::getInstance();
$currentPageDefaultInformation = ['offset' => 0, 'limit' => 20];
$currentPageQuery = $currentUrl->getVar('page', $currentPageDefaultInformation);
if ($items === null) {
$items = [];
foreach ($model->getItems() as $item) {
$items[] = $this->prepareItem($item);
}
}
$pagination = $model->getPagination();
// Check for errors.
if (\count($errors = $this->get('Errors'))) {
throw new GenericDataException(implode("\n", $errors), 500);
}
if ($this->type === null) {
throw new \RuntimeException('Content type missing');
}
// Set up links for pagination
$totalItemsCount = ($pagination->pagesTotal * $pagination->limit);
$this->getDocument()->addMeta('total-pages', $pagination->pagesTotal)
->addLink('self', (string) $currentUrl);
// Check for first and previous pages
if ($pagination->limitstart > 0) {
$firstPage = clone $currentUrl;
$firstPageQuery = $currentPageQuery;
$firstPageQuery['offset'] = 0;
$firstPage->setVar('page', $firstPageQuery);
$previousPage = clone $currentUrl;
$previousPageQuery = $currentPageQuery;
$previousOffset = $currentPageQuery['offset'] - $pagination->limit;
$previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0;
$previousPage->setVar('page', $previousPageQuery);
$this->getDocument()->addLink('first', $this->queryEncode((string) $firstPage))
->addLink('previous', $this->queryEncode((string) $previousPage));
}
// Check for next and last pages
if ($pagination->limitstart + $pagination->limit < $totalItemsCount) {
$nextPage = clone $currentUrl;
$nextPageQuery = $currentPageQuery;
$nextOffset = $currentPageQuery['offset'] + $pagination->limit;
$nextPageQuery['offset'] = ($nextOffset > ($pagination->pagesTotal * $pagination->limit)) ? $pagination->pagesTotal - $pagination->limit : $nextOffset;
$nextPage->setVar('page', $nextPageQuery);
$lastPage = clone $currentUrl;
$lastPageQuery = $currentPageQuery;
$lastPageQuery['offset'] = ($pagination->pagesTotal - 1) * $pagination->limit;
$lastPage->setVar('page', $lastPageQuery);
$this->getDocument()->addLink('next', $this->queryEncode((string) $nextPage))
->addLink('last', $this->queryEncode((string) $lastPage));
}
$eventData = ['type' => OnGetApiFields::LIST, 'fields' => $this->fieldsToRenderList, 'context' => $this->type];
$event = new OnGetApiFields('onApiGetFields', $eventData);
/** @var OnGetApiFields $eventResult */
$eventResult = Factory::getApplication()->getDispatcher()->dispatch('onApiGetFields', $event);
$collection = (new Collection($items, $this->serializer))
->fields([$this->type => $eventResult->getAllPropertiesToRender()]);
if (!empty($this->relationship)) {
$collection->with($this->relationship);
}
// Set the data into the document and render it
$this->getDocument()->setData($collection);
return $this->getDocument()->render();
}
/**
* Execute and display a template script.
*
* @param object $item Item
*
* @return string
*
* @since 4.0.0
*/
public function displayItem($item = null)
{
if ($item === null) {
/** @var \Joomla\CMS\MVC\Model\AdminModel $model */
$model = $this->getModel();
$item = $this->prepareItem($model->getItem());
}
if (!$item || $item->id === null) {
throw new RouteNotFoundException('Item does not exist');
}
// Check for errors.
if (\count($errors = $this->get('Errors'))) {
throw new GenericDataException(implode("\n", $errors), 500);
}
if ($this->type === null) {
throw new \RuntimeException('Content type missing');
}
$eventData = [
'type' => OnGetApiFields::ITEM,
'fields' => $this->fieldsToRenderItem,
'relations' => $this->relationship,
'context' => $this->type,
];
$event = new OnGetApiFields('onApiGetFields', $eventData);
/** @var OnGetApiFields $eventResult */
$eventResult = Factory::getApplication()->getDispatcher()->dispatch('onApiGetFields', $event);
$element = (new Resource($item, $this->serializer))
->fields([$this->type => $eventResult->getAllPropertiesToRender()]);
if (!empty($this->relationship)) {
$element->with($eventResult->getAllRelationsToRender());
}
$this->getDocument()->setData($element);
$this->getDocument()->addLink('self', Uri::current());
return $this->getDocument()->render();
}
/**
* Prepare item before render.
*
* @param object $item The model item
*
* @return object
*
* @since 4.0.0
*/
protected function prepareItem($item)
{
return $item;
}
/**
* Get the effective API field key for a custom field, using the cf_ prefix
* when it would otherwise collide with an existing property or a core field.
*
* @param object|null $item The item being prepared, if available
* @param string $fieldName The original custom field name
*
* @return string
*
* @since 5.4.0
*/
protected function getApiFieldKey(?object $item, string $fieldName): string
{
if ($item !== null && property_exists($item, $fieldName)) {
return 'cf_' . $fieldName;
}
if (\in_array($fieldName, $this->fieldsToRenderItem, true)
|| \in_array($fieldName, $this->fieldsToRenderList, true)
) {
return 'cf_' . $fieldName;
}
return $fieldName;
}
/**
* Register custom fields for rendering in list or item responses, using
* a collision-safe key derived from the original field name.
*
* @param array $fields The list of custom field objects
* @param bool $forList True when registering for list views, false for item views
*
* @return void
*
* @since 5.4.0
*/
protected function registerApiFields(array $fields, bool $forList): void
{
foreach ($fields as $field) {
$fieldKey = $this->getApiFieldKey(null, $field->name);
if ($forList) {
if (!\in_array($fieldKey, $this->fieldsToRenderList, true)) {
$this->fieldsToRenderList[] = $fieldKey;
}
} else {
if (!\in_array($fieldKey, $this->fieldsToRenderItem, true)) {
$this->fieldsToRenderItem[] = $fieldKey;
}
}
}
}
/**
* Assign values of custom fields to the item and register them for
* rendering in both item and list responses using a collision-safe key.
*
* @param array $fields The list of custom field objects
* @param object $item The item being prepared
*
* @return void
*
* @since 5.4.0
*/
protected function assignApiFieldValues(array $fields, $item): void
{
foreach ($fields as $field) {
$value = $field->apivalue ?? $field->rawvalue ?? null;
$fieldKey = $this->getApiFieldKey($item, $field->name);
$item->{$fieldKey} = $value;
if (!\in_array($fieldKey, $this->fieldsToRenderItem, true)) {
$this->fieldsToRenderItem[] = $fieldKey;
}
if (!\in_array($fieldKey, $this->fieldsToRenderList, true)) {
$this->fieldsToRenderList[] = $fieldKey;
}
}
}
/**
* Encode square brackets in the URI query, according to JSON API specification.
*
* @param string $query The URI query
*
* @return string
*
* @since 4.0.0
*/
protected function queryEncode($query)
{
return str_replace(['[', ']'], ['%5B', '%5D'], $query);
}
}