‪TYPO3CMS  ‪main
JsonView.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 use TYPO3Fluid\Fluid\View\AbstractView;
24 
28 class ‪JsonView extends AbstractView
29 {
39 
45 
51  protected ‪$variablesToRender = ['value'];
52 
56  protected ‪$currentVariable = '';
57 
156  protected ‪$configuration = [];
157 
161  protected ‪$persistenceManager;
162 
169  protected ‪$variables = [];
170 
175  {
176  $this->persistenceManager = ‪$persistenceManager;
177  }
178 
187  public function ‪assign($key, $value)
188  {
189  $this->variables[$key] = $value;
190  return $this;
191  }
192 
199  public function ‪assignMultiple(array $values)
200  {
201  foreach ($values as $key => $value) {
202  $this->‪assign($key, $value);
203  }
204  return $this;
205  }
206 
211  public function ‪setVariablesToRender(array ‪$variablesToRender): void
212  {
213  $this->variablesToRender = ‪$variablesToRender;
214  }
215 
219  public function ‪setConfiguration(array ‪$configuration): void
220  {
221  $this->configuration = ‪$configuration;
222  }
223 
224  public function ‪renderSection($sectionName, array ‪$variables = [], $ignoreUnknown = false)
225  {
226  // No-op: renderSection does not make sense for this view
227  return '';
228  }
229 
230  public function ‪renderPartial($partialName, $sectionName, array ‪$variables, $ignoreUnknown = false)
231  {
232  // No-op: renderPartial does not make sense for this view
233  return '';
234  }
235 
243  public function ‪render(): string
244  {
245  $propertiesToRender = $this->‪renderArray();
246  return json_encode($propertiesToRender, JSON_UNESCAPED_UNICODE);
247  }
248 
255  protected function ‪renderArray()
256  {
257  if (count($this->variablesToRender) === 1) {
258  $firstLevel = false;
259  $variableName = current($this->variablesToRender);
260  $this->currentVariable = $variableName;
261  $valueToRender = $this->variables[$variableName] ?? null;
262  ‪$configuration = $this->configuration[$variableName] ?? [];
263  } else {
264  $firstLevel = true;
265  $valueToRender = [];
266  foreach ($this->variablesToRender as $variableName) {
267  $valueToRender[$variableName] = $this->variables[$variableName] ?? null;
268  }
270  }
271  return $this->‪transformValue($valueToRender, ‪$configuration, $firstLevel);
272  }
273 
283  protected function ‪transformValue($value, array ‪$configuration, $firstLevel = false)
284  {
285  // ObjectStorage returns $key as string, which causes the resulting JSON to be an object instead of the expected array
286  if ($value instanceof ObjectStorage) {
287  $value = $value->toArray();
288  }
289  if (is_array($value) || $value instanceof \ArrayAccess) {
290  $array = [];
291  foreach ($value as $key => $element) {
292  if ($firstLevel) {
293  $this->currentVariable = $key;
294  }
295  if (isset(‪$configuration['_descendAll']) && is_array(‪$configuration['_descendAll'])) {
296  $array[$key] = $this->‪transformValue($element, ‪$configuration['_descendAll']);
297  } else {
298  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($key, ‪$configuration['_only'], true)) {
299  continue;
300  }
301  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($key, ‪$configuration['_exclude'], true)) {
302  continue;
303  }
304  $array[$key] = $this->‪transformValue($element, ‪$configuration[$key] ?? []);
305  }
306  }
307  return $array;
308  }
309  if (is_object($value)) {
310  return $this->‪transformObject($value, ‪$configuration);
311  }
312  return $value;
313  }
314 
323  protected function ‪transformObject(object $object, array ‪$configuration)
324  {
325  if ($object instanceof \DateTimeInterface) {
326  return $object->format(\DateTimeInterface::ATOM);
327  }
328  $propertyNames = ‪ObjectAccess::getGettablePropertyNames($object);
329 
330  $propertiesToRender = [];
331  foreach ($propertyNames as $propertyName) {
332  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($propertyName, ‪$configuration['_only'], true)) {
333  continue;
334  }
335  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($propertyName, ‪$configuration['_exclude'], true)) {
336  continue;
337  }
338 
339  $propertyValue = ‪ObjectAccess::getProperty($object, $propertyName);
340 
341  if (!is_array($propertyValue) && !is_object($propertyValue)) {
342  $propertiesToRender[$propertyName] = $propertyValue;
343  } elseif (isset(‪$configuration['_descend']) && array_key_exists($propertyName, ‪$configuration['_descend'])) {
344  $propertiesToRender[$propertyName] = $this->‪transformValue($propertyValue, ‪$configuration['_descend'][$propertyName]);
345  } elseif (isset(‪$configuration['_recursive']) && in_array($propertyName, ‪$configuration['_recursive'])) {
346  $propertiesToRender[$propertyName] = $this->‪transformValue($propertyValue, $this->configuration[$this->currentVariable]);
347  }
348  }
349  if (isset(‪$configuration['_exposeObjectIdentifier']) && ‪$configuration['_exposeObjectIdentifier'] === true) {
350  if (isset(‪$configuration['_exposedObjectIdentifierKey']) && strlen(‪$configuration['_exposedObjectIdentifierKey']) > 0) {
351  $identityKey = ‪$configuration['_exposedObjectIdentifierKey'];
352  } else {
353  $identityKey = '__identity';
354  }
355  $propertiesToRender[$identityKey] = $this->persistenceManager->getIdentifierByObject($object);
356  }
357  if (isset(‪$configuration['_exposeClassName']) && (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED || ‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_UNQUALIFIED)) {
358  $className = get_class($object);
359  $classNameParts = explode('\\', $className);
360  $propertiesToRender['__class'] = (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED ? $className : array_pop($classNameParts));
361  }
362 
363  return $propertiesToRender;
364  }
365 }
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: JsonView.php:157
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$variablesToRender
‪string[] $variablesToRender
Definition: JsonView.php:50
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getProperty
‪static mixed getProperty(object|array $subject, string $propertyName)
Definition: ObjectAccess.php:59
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:24
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration
‪array $configuration
Definition: JsonView.php:153
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue
‪mixed transformValue($value, array $configuration, $firstLevel=false)
Definition: JsonView.php:278
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderSection
‪renderSection($sectionName, array $variables=[], $ignoreUnknown=false)
Definition: JsonView.php:219
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: JsonView.php:169
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:39
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:34
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject
‪array string transformObject(object $object, array $configuration)
Definition: JsonView.php:318
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\EXPOSE_CLASSNAME_FULLY_QUALIFIED
‪const EXPOSE_CLASSNAME_FULLY_QUALIFIED
Definition: JsonView.php:38
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$variables
‪array $variables
Definition: JsonView.php:164
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\assignMultiple
‪self assignMultiple(array $values)
Definition: JsonView.php:194
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettablePropertyNames
‪static list< string > getGettablePropertyNames(object $object)
Definition: ObjectAccess.php:185
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setConfiguration
‪setConfiguration(array $configuration)
Definition: JsonView.php:214
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$currentVariable
‪string $currentVariable
Definition: JsonView.php:54
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\assign
‪self assign($key, $value)
Definition: JsonView.php:182
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderPartial
‪renderPartial($partialName, $sectionName, array $variables, $ignoreUnknown=false)
Definition: JsonView.php:225
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray
‪mixed renderArray()
Definition: JsonView.php:250
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\EXPOSE_CLASSNAME_UNQUALIFIED
‪const EXPOSE_CLASSNAME_UNQUALIFIED
Definition: JsonView.php:44
‪TYPO3\CMS\Extbase\Mvc\View
Definition: GenericViewResolver.php:18
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setVariablesToRender
‪setVariablesToRender(array $variablesToRender)
Definition: JsonView.php:206
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\render
‪string render()
Definition: JsonView.php:238
‪TYPO3\CMS\Extbase\Mvc\View\JsonView
Definition: JsonView.php:29