‪TYPO3CMS  11.5
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 
31 {
41 
47 
53  protected ‪$variablesToRender = ['value'];
54 
58  protected ‪$currentVariable = '';
59 
158  protected ‪$configuration = [];
159 
163  protected ‪$persistenceManager;
164 
169  protected ‪$controllerContext;
170 
177  protected ‪$variables = [];
178 
184  {
185  $this->persistenceManager = ‪$persistenceManager;
186  }
187 
195  {
196  $this->controllerContext = ‪$controllerContext;
197  }
198 
207  public function ‪assign($key, $value)
208  {
209  $this->variables[$key] = $value;
210  return $this;
211  }
212 
219  public function ‪assignMultiple(array $values)
220  {
221  foreach ($values as $key => $value) {
222  $this->‪assign($key, $value);
223  }
224  return $this;
225  }
226 
236  public function ‪canRender()
237  {
238  trigger_error('Method ' . __METHOD__ . ' has been deprecated in v11 and will be removed with v12.', E_USER_DEPRECATED);
239  return true;
240  }
241 
248  public function ‪initializeView() {}
249 
256  public function ‪setVariablesToRender(array ‪$variablesToRender): void
257  {
258  $this->variablesToRender = ‪$variablesToRender;
259  }
260 
264  public function ‪setConfiguration(array ‪$configuration): void
265  {
266  $this->configuration = ‪$configuration;
267  }
268 
272  public function ‪renderSection($sectionName, array ‪$variables = [], $ignoreUnknown = false)
273  {
274  // No-op: renderSection does not make sense for this view
275  return '';
276  }
277 
281  public function ‪renderPartial($partialName, $sectionName, array ‪$variables, $ignoreUnknown = false)
282  {
283  // No-op: renderPartial does not make sense for this view
284  return '';
285  }
286 
294  public function ‪render(): string
295  {
296  $propertiesToRender = $this->‪renderArray();
297  return json_encode($propertiesToRender, JSON_UNESCAPED_UNICODE);
298  }
299 
306  protected function ‪renderArray()
307  {
308  if (count($this->variablesToRender) === 1) {
309  $firstLevel = false;
310  $variableName = current($this->variablesToRender);
311  $this->currentVariable = $variableName;
312  $valueToRender = $this->variables[$variableName] ?? null;
313  ‪$configuration = $this->configuration[$variableName] ?? [];
314  } else {
315  $firstLevel = true;
316  $valueToRender = [];
317  foreach ($this->variablesToRender as $variableName) {
318  $valueToRender[$variableName] = $this->variables[$variableName] ?? null;
319  }
321  }
322  return $this->‪transformValue($valueToRender, ‪$configuration, $firstLevel);
323  }
324 
334  protected function ‪transformValue($value, array ‪$configuration, $firstLevel = false)
335  {
336  if (is_array($value) || $value instanceof \ArrayAccess) {
337  $array = [];
338  foreach ($value as $key => $element) {
339  if ($firstLevel) {
340  $this->currentVariable = $key;
341  }
342  if (isset(‪$configuration['_descendAll']) && is_array(‪$configuration['_descendAll'])) {
343  $array[$key] = $this->‪transformValue($element, ‪$configuration['_descendAll']);
344  } else {
345  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($key, ‪$configuration['_only'], true)) {
346  continue;
347  }
348  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($key, ‪$configuration['_exclude'], true)) {
349  continue;
350  }
351  $array[$key] = $this->‪transformValue($element, ‪$configuration[$key] ?? []);
352  }
353  }
354  return $array;
355  }
356  if (is_object($value)) {
357  return $this->‪transformObject($value, ‪$configuration);
358  }
359  return $value;
360  }
361 
370  protected function ‪transformObject(object $object, array ‪$configuration)
371  {
372  if ($object instanceof \DateTimeInterface) {
373  return $object->format(\DateTimeInterface::ATOM);
374  }
375  $propertyNames = ‪ObjectAccess::getGettablePropertyNames($object);
376 
377  $propertiesToRender = [];
378  foreach ($propertyNames as $propertyName) {
379  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($propertyName, ‪$configuration['_only'], true)) {
380  continue;
381  }
382  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($propertyName, ‪$configuration['_exclude'], true)) {
383  continue;
384  }
385 
386  $propertyValue = ‪ObjectAccess::getProperty($object, $propertyName);
387 
388  if (!is_array($propertyValue) && !is_object($propertyValue)) {
389  $propertiesToRender[$propertyName] = $propertyValue;
390  } elseif (isset(‪$configuration['_descend']) && array_key_exists($propertyName, ‪$configuration['_descend'])) {
391  $propertiesToRender[$propertyName] = $this->‪transformValue($propertyValue, ‪$configuration['_descend'][$propertyName]);
392  } elseif (isset(‪$configuration['_recursive']) && in_array($propertyName, ‪$configuration['_recursive'])) {
393  $propertiesToRender[$propertyName] = $this->‪transformValue($propertyValue, $this->configuration[$this->currentVariable]);
394  }
395  }
396  if (isset(‪$configuration['_exposeObjectIdentifier']) && ‪$configuration['_exposeObjectIdentifier'] === true) {
397  if (isset(‪$configuration['_exposedObjectIdentifierKey']) && strlen(‪$configuration['_exposedObjectIdentifierKey']) > 0) {
398  $identityKey = ‪$configuration['_exposedObjectIdentifierKey'];
399  } else {
400  $identityKey = '__identity';
401  }
402  $propertiesToRender[$identityKey] = $this->persistenceManager->getIdentifierByObject($object);
403  }
404  if (isset(‪$configuration['_exposeClassName']) && (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED || ‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_UNQUALIFIED)) {
405  $className = get_class($object);
406  $classNameParts = explode('\\', $className);
407  $propertiesToRender['__class'] = (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED ? $className : array_pop($classNameParts));
408  }
409 
410  return $propertiesToRender;
411  }
412 }
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: JsonView.php:159
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$variablesToRender
‪string[] $variablesToRender
Definition: JsonView.php:52
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration
‪array $configuration
Definition: JsonView.php:155
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getProperty
‪static mixed getProperty($subject, string $propertyName)
Definition: ObjectAccess.php:61
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
Definition: ControllerContext.php:30
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue
‪mixed transformValue($value, array $configuration, $firstLevel=false)
Definition: JsonView.php:328
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$controllerContext
‪ControllerContext $controllerContext
Definition: JsonView.php:164
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderSection
‪renderSection($sectionName, array $variables=[], $ignoreUnknown=false)
Definition: JsonView.php:266
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: JsonView.php:177
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettablePropertyNames
‪static array getGettablePropertyNames(object $object)
Definition: ObjectAccess.php:200
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\initializeView
‪initializeView()
Definition: JsonView.php:242
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:39
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\canRender
‪bool canRender()
Definition: JsonView.php:230
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject
‪array string transformObject(object $object, array $configuration)
Definition: JsonView.php:364
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\EXPOSE_CLASSNAME_FULLY_QUALIFIED
‪const EXPOSE_CLASSNAME_FULLY_QUALIFIED
Definition: JsonView.php:40
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setControllerContext
‪setControllerContext(ControllerContext $controllerContext)
Definition: JsonView.php:188
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$variables
‪array $variables
Definition: JsonView.php:171
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\assignMultiple
‪self assignMultiple(array $values)
Definition: JsonView.php:213
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:26
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setConfiguration
‪setConfiguration(array $configuration)
Definition: JsonView.php:258
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$currentVariable
‪string $currentVariable
Definition: JsonView.php:56
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\assign
‪self assign($key, $value)
Definition: JsonView.php:201
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderPartial
‪renderPartial($partialName, $sectionName, array $variables, $ignoreUnknown=false)
Definition: JsonView.php:275
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray
‪mixed renderArray()
Definition: JsonView.php:300
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\EXPOSE_CLASSNAME_UNQUALIFIED
‪const EXPOSE_CLASSNAME_UNQUALIFIED
Definition: JsonView.php:46
‪TYPO3\CMS\Extbase\Mvc\View
Definition: AbstractView.php:16
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setVariablesToRender
‪setVariablesToRender(array $variablesToRender)
Definition: JsonView.php:250
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\render
‪string render()
Definition: JsonView.php:288
‪TYPO3\CMS\Extbase\Mvc\View\JsonView
Definition: JsonView.php:31
‪TYPO3\CMS\Extbase\Mvc\View\AbstractView
Definition: AbstractView.php:25