‪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 
20 use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
24 use TYPO3Fluid\Fluid\View\AbstractView;
25 
29 #[Autoconfigure(public: true, shared: false)]
30 class ‪JsonView extends AbstractView
31 {
41 
47 
53  protected ‪$variablesToRender = ['value'];
54 
58  protected ‪$currentVariable = '';
59 
158  protected ‪$configuration = [];
159 
163  protected ‪$persistenceManager;
164 
171  protected ‪$variables = [];
172 
177  {
178  $this->persistenceManager = ‪$persistenceManager;
179  }
180 
189  public function ‪assign($key, $value)
190  {
191  $this->variables[$key] = $value;
192  return $this;
193  }
194 
201  public function ‪assignMultiple(array $values)
202  {
203  foreach ($values as $key => $value) {
204  $this->‪assign($key, $value);
205  }
206  return $this;
207  }
208 
213  public function ‪setVariablesToRender(array ‪$variablesToRender): void
214  {
215  $this->variablesToRender = ‪$variablesToRender;
216  }
217 
221  public function ‪setConfiguration(array ‪$configuration): void
222  {
223  $this->configuration = ‪$configuration;
224  }
225 
226  public function ‪renderSection($sectionName, array ‪$variables = [], $ignoreUnknown = false)
227  {
228  // No-op: renderSection does not make sense for this view
229  return '';
230  }
231 
232  public function ‪renderPartial($partialName, $sectionName, array ‪$variables, $ignoreUnknown = false)
233  {
234  // No-op: renderPartial does not make sense for this view
235  return '';
236  }
237 
245  public function ‪render(): string
246  {
247  $propertiesToRender = $this->‪renderArray();
248  return json_encode($propertiesToRender, JSON_UNESCAPED_UNICODE);
249  }
250 
257  protected function ‪renderArray()
258  {
259  if (count($this->variablesToRender) === 1) {
260  $firstLevel = false;
261  $variableName = current($this->variablesToRender);
262  $this->currentVariable = $variableName;
263  $valueToRender = $this->variables[$variableName] ?? null;
264  ‪$configuration = $this->configuration[$variableName] ?? [];
265  } else {
266  $firstLevel = true;
267  $valueToRender = [];
268  foreach ($this->variablesToRender as $variableName) {
269  $valueToRender[$variableName] = $this->variables[$variableName] ?? null;
270  }
272  }
273  return $this->‪transformValue($valueToRender, ‪$configuration, $firstLevel);
274  }
275 
285  protected function ‪transformValue($value, array ‪$configuration, $firstLevel = false)
286  {
287  // ObjectStorage returns $key as string, which causes the resulting JSON to be an object instead of the expected array
288  if ($value instanceof ObjectStorage) {
289  $value = $value->toArray();
290  }
291  if (is_array($value) || $value instanceof \ArrayAccess) {
292  $array = [];
293  foreach ($value as $key => $element) {
294  if ($firstLevel) {
295  $this->currentVariable = $key;
296  }
297  if (isset(‪$configuration['_descendAll']) && is_array(‪$configuration['_descendAll'])) {
298  $array[$key] = $this->‪transformValue($element, ‪$configuration['_descendAll']);
299  } else {
300  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($key, ‪$configuration['_only'], true)) {
301  continue;
302  }
303  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($key, ‪$configuration['_exclude'], true)) {
304  continue;
305  }
306  $array[$key] = $this->‪transformValue($element, ‪$configuration[$key] ?? []);
307  }
308  }
309  return $array;
310  }
311  if (is_object($value)) {
312  return $this->‪transformObject($value, ‪$configuration);
313  }
314  return $value;
315  }
316 
325  protected function ‪transformObject(object $object, array ‪$configuration)
326  {
327  if ($object instanceof \DateTimeInterface) {
328  return $object->format(\DateTimeInterface::ATOM);
329  }
330  $propertyNames = ‪ObjectAccess::getGettablePropertyNames($object);
331 
332  $propertiesToRender = [];
333  foreach ($propertyNames as $propertyName) {
334  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($propertyName, ‪$configuration['_only'], true)) {
335  continue;
336  }
337  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($propertyName, ‪$configuration['_exclude'], true)) {
338  continue;
339  }
340 
341  $propertyValue = ‪ObjectAccess::getProperty($object, $propertyName);
342 
343  if (!is_array($propertyValue) && !is_object($propertyValue)) {
344  $propertiesToRender[$propertyName] = $propertyValue;
345  } elseif (isset(‪$configuration['_descend']) && array_key_exists($propertyName, ‪$configuration['_descend'])) {
346  $propertiesToRender[$propertyName] = $this->‪transformValue($propertyValue, ‪$configuration['_descend'][$propertyName]);
347  } elseif (isset(‪$configuration['_recursive']) && in_array($propertyName, ‪$configuration['_recursive'])) {
348  $propertiesToRender[$propertyName] = $this->‪transformValue($propertyValue, $this->configuration[$this->currentVariable]);
349  }
350  }
351  if (isset(‪$configuration['_exposeObjectIdentifier']) && ‪$configuration['_exposeObjectIdentifier'] === true) {
352  if (isset(‪$configuration['_exposedObjectIdentifierKey']) && strlen(‪$configuration['_exposedObjectIdentifierKey']) > 0) {
353  $identityKey = ‪$configuration['_exposedObjectIdentifierKey'];
354  } else {
355  $identityKey = '__identity';
356  }
357  $propertiesToRender[$identityKey] = $this->persistenceManager->getIdentifierByObject($object);
358  }
359  if (isset(‪$configuration['_exposeClassName']) && (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED || ‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_UNQUALIFIED)) {
360  $className = get_class($object);
361  $classNameParts = explode('\\', $className);
362  $propertiesToRender['__class'] = (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED ? $className : array_pop($classNameParts));
363  }
364 
365  return $propertiesToRender;
366  }
367 }
‪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\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:155
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue
‪mixed transformValue($value, array $configuration, $firstLevel=false)
Definition: JsonView.php:280
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderSection
‪renderSection($sectionName, array $variables=[], $ignoreUnknown=false)
Definition: JsonView.php:221
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: JsonView.php:171
‪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:320
‪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\$variables
‪array $variables
Definition: JsonView.php:166
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\assignMultiple
‪self assignMultiple(array $values)
Definition: JsonView.php:196
‪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:216
‪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:184
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderPartial
‪renderPartial($partialName, $sectionName, array $variables, $ignoreUnknown=false)
Definition: JsonView.php:227
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray
‪mixed renderArray()
Definition: JsonView.php:252
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\EXPOSE_CLASSNAME_UNQUALIFIED
‪const EXPOSE_CLASSNAME_UNQUALIFIED
Definition: JsonView.php:46
‪TYPO3\CMS\Extbase\Mvc\View
Definition: GenericViewResolver.php:18
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setVariablesToRender
‪setVariablesToRender(array $variablesToRender)
Definition: JsonView.php:208
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\render
‪string render()
Definition: JsonView.php:240
‪TYPO3\CMS\Extbase\Mvc\View\JsonView
Definition: JsonView.php:31