‪TYPO3CMS  10.4
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 
24 
29 {
39 
45 
49  protected ‪$reflectionService;
50 
54  protected ‪$controllerContext;
55 
61  protected ‪$variablesToRender = ['value'];
62 
161  protected ‪$configuration = [];
162 
166  protected ‪$persistenceManager;
167 
173  {
174  $this->persistenceManager = ‪$persistenceManager;
175  }
176 
182  {
183  $this->reflectionService = ‪$reflectionService;
184  }
185 
192  public function ‪setVariablesToRender(array ‪$variablesToRender): void
193  {
194  $this->variablesToRender = ‪$variablesToRender;
195  }
196 
200  public function ‪setConfiguration(array ‪$configuration): void
201  {
202  $this->configuration = ‪$configuration;
203  }
204 
212  public function ‪render(): string
213  {
214  $response = $this->controllerContext->getResponse();
215  // @todo Ticket: #63643 This should be solved differently once request/response model is available for TSFE.
216  if (!empty(‪$GLOBALS['TSFE']) && ‪$GLOBALS['TSFE'] instanceof ‪TypoScriptFrontendController) {
218  $typoScriptFrontendController = ‪$GLOBALS['TSFE'];
219  if (empty($typoScriptFrontendController->config['config']['disableCharsetHeader'])) {
220  // If the charset header is *not* disabled in configuration,
221  // TypoScriptFrontendController will send the header later with the Content-Type which we set here.
222  $typoScriptFrontendController->setContentType('application/json');
223  } else {
224  // Although the charset header is disabled in configuration, we *must* send a Content-Type header here.
225  // Content-Type headers optionally carry charset information at the same time.
226  // Since we have the information about the charset, there is no reason to not include the charset information although disabled in TypoScript.
227  $response->setHeader('Content-Type', 'application/json; charset=' . trim($typoScriptFrontendController->metaCharset));
228  }
229  } else {
230  $response->setHeader('Content-Type', 'application/json');
231  }
232  $propertiesToRender = $this->‪renderArray();
233  return json_encode($propertiesToRender, JSON_UNESCAPED_UNICODE);
234  }
235 
242  protected function ‪renderArray()
243  {
244  if (count($this->variablesToRender) === 1) {
245  $variableName = current($this->variablesToRender);
246  $valueToRender = $this->variables[$variableName] ?? null;
247  ‪$configuration = $this->configuration[$variableName] ?? [];
248  } else {
249  $valueToRender = [];
250  foreach ($this->variablesToRender as $variableName) {
251  $valueToRender[$variableName] = $this->variables[$variableName] ?? null;
252  }
254  }
255  return $this->‪transformValue($valueToRender, ‪$configuration);
256  }
257 
266  protected function ‪transformValue($value, array ‪$configuration)
267  {
268  if (is_array($value) || $value instanceof \ArrayAccess) {
269  $array = [];
270  foreach ($value as $key => $element) {
271  if (isset(‪$configuration['_descendAll']) && is_array(‪$configuration['_descendAll'])) {
272  $array[$key] = $this->‪transformValue($element, ‪$configuration['_descendAll']);
273  } else {
274  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($key, ‪$configuration['_only'], true)) {
275  continue;
276  }
277  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($key, ‪$configuration['_exclude'], true)) {
278  continue;
279  }
280  $array[$key] = $this->‪transformValue($element, ‪$configuration[$key] ?? []);
281  }
282  }
283  return $array;
284  }
285  if (is_object($value)) {
286  return $this->‪transformObject($value, ‪$configuration);
287  }
288  return $value;
289  }
290 
299  protected function ‪transformObject(object $object, array ‪$configuration)
300  {
301  if ($object instanceof \DateTimeInterface) {
302  return $object->format(\DateTimeInterface::ATOM);
303  }
304  $propertyNames = ‪ObjectAccess::getGettablePropertyNames($object);
305 
306  $propertiesToRender = [];
307  foreach ($propertyNames as $propertyName) {
308  if (isset(‪$configuration['_only']) && is_array(‪$configuration['_only']) && !in_array($propertyName, ‪$configuration['_only'], true)) {
309  continue;
310  }
311  if (isset(‪$configuration['_exclude']) && is_array(‪$configuration['_exclude']) && in_array($propertyName, ‪$configuration['_exclude'], true)) {
312  continue;
313  }
314 
315  $propertyValue = ‪ObjectAccess::getProperty($object, $propertyName);
316 
317  if (!is_array($propertyValue) && !is_object($propertyValue)) {
318  $propertiesToRender[$propertyName] = $propertyValue;
319  } elseif (isset(‪$configuration['_descend']) && array_key_exists($propertyName, ‪$configuration['_descend'])) {
320  $propertiesToRender[$propertyName] = $this->‪transformValue($propertyValue, ‪$configuration['_descend'][$propertyName]);
321  }
322  }
323  if (isset(‪$configuration['_exposeObjectIdentifier']) && ‪$configuration['_exposeObjectIdentifier'] === true) {
324  if (isset(‪$configuration['_exposedObjectIdentifierKey']) && strlen(‪$configuration['_exposedObjectIdentifierKey']) > 0) {
325  $identityKey = ‪$configuration['_exposedObjectIdentifierKey'];
326  } else {
327  $identityKey = '__identity';
328  }
329  $propertiesToRender[$identityKey] = $this->persistenceManager->getIdentifierByObject($object);
330  }
331  if (isset(‪$configuration['_exposeClassName']) && (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED || ‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_UNQUALIFIED)) {
332  $className = get_class($object);
333  $classNameParts = explode('\\', $className);
334  $propertiesToRender['__class'] = (‪$configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED ? $className : array_pop($classNameParts));
335  }
336 
337  return $propertiesToRender;
338  }
339 }
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: JsonView.php:161
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$variablesToRender
‪string[] $variablesToRender
Definition: JsonView.php:58
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration
‪array $configuration
Definition: JsonView.php:157
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$reflectionService
‪ReflectionService $reflectionService
Definition: JsonView.php:48
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: JsonView.php:167
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettablePropertyNames
‪static array getGettablePropertyNames(object $object)
Definition: ObjectAccess.php:217
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:38
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:31
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject
‪array string transformObject(object $object, array $configuration)
Definition: JsonView.php:294
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\injectReflectionService
‪injectReflectionService(ReflectionService $reflectionService)
Definition: JsonView.php:176
‪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\setConfiguration
‪setConfiguration(array $configuration)
Definition: JsonView.php:195
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray
‪mixed renderArray()
Definition: JsonView.php:237
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\$controllerContext
‪TYPO3 CMS Extbase Mvc Controller ControllerContext $controllerContext
Definition: JsonView.php:52
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\EXPOSE_CLASSNAME_UNQUALIFIED
‪const EXPOSE_CLASSNAME_UNQUALIFIED
Definition: JsonView.php:44
‪TYPO3\CMS\Extbase\Mvc\View
Definition: AbstractView.php:16
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\setVariablesToRender
‪setVariablesToRender(array $variablesToRender)
Definition: JsonView.php:187
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\render
‪string render()
Definition: JsonView.php:207
‪TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue
‪mixed transformValue($value, array $configuration)
Definition: JsonView.php:261
‪TYPO3\CMS\Extbase\Mvc\View\JsonView
Definition: JsonView.php:29
‪TYPO3\CMS\Extbase\Mvc\View\AbstractView
Definition: AbstractView.php:24
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getProperty
‪static mixed getProperty($subject, string $propertyName, bool $forceDirectAccess=false)
Definition: ObjectAccess.php:62