TYPO3 CMS  TYPO3_6-2
JsonView.php
Go to the documentation of this file.
1 <?php
3 
22 
32 
38 
43  protected $reflectionService;
44 
48  protected $controllerContext;
49 
55  protected $variablesToRender = array('value');
56 
155  protected $configuration = array();
156 
162 
171  public function setVariablesToRender(array $variablesToRender) {
172  $this->variablesToRender = $variablesToRender;
173  }
174 
179  public function setConfiguration(array $configuration) {
180  $this->configuration = $configuration;
181  }
182 
191  public function render() {
192  $this->controllerContext->getResponse()->setHeader('Content-Type', 'application/json');
193  $propertiesToRender = $this->renderArray();
194  return json_encode($propertiesToRender);
195  }
196 
204  protected function renderArray() {
205  if (count($this->variablesToRender) === 1) {
206  $variableName = current($this->variablesToRender);
207  $valueToRender = isset($this->variables[$variableName]) ? $this->variables[$variableName] : NULL;
208  $configuration = isset($this->configuration[$variableName]) ? $this->configuration[$variableName] : array();
209  } else {
210  $valueToRender = array();
211  foreach ($this->variablesToRender as $variableName) {
212  $valueToRender[$variableName] = isset($this->variables[$variableName]) ? $this->variables[$variableName] : NULL;
213  }
215  }
216  return $this->transformValue($valueToRender, $configuration);
217  }
218 
227  protected function transformValue($value, array $configuration) {
228  if (is_array($value) || $value instanceof \ArrayAccess) {
229  $array = array();
230  foreach ($value as $key => $element) {
231  if (isset($configuration['_descendAll']) && is_array($configuration['_descendAll'])) {
232  $array[$key] = $this->transformValue($element, $configuration['_descendAll']);
233  } else {
234  if (isset($configuration['_only']) && is_array($configuration['_only']) && !in_array($key, $configuration['_only'])) {
235  continue;
236  }
237  if (isset($configuration['_exclude']) && is_array($configuration['_exclude']) && in_array($key, $configuration['_exclude'])) {
238  continue;
239  }
240  $array[$key] = $this->transformValue($element, isset($configuration[$key]) ? $configuration[$key] : array());
241  }
242  }
243  return $array;
244  } elseif (is_object($value)) {
245  return $this->transformObject($value, $configuration);
246  } else {
247  return $value;
248  }
249  }
250 
259  protected function transformObject($object, array $configuration) {
260  if ($object instanceof \DateTime) {
261  return $object->format(\DateTime::ISO8601);
262  } else {
264 
265  $propertiesToRender = array();
266  foreach ($propertyNames as $propertyName) {
267  if (isset($configuration['_only']) && is_array($configuration['_only']) && !in_array($propertyName, $configuration['_only'])) {
268  continue;
269  }
270  if (isset($configuration['_exclude']) && is_array($configuration['_exclude']) && in_array($propertyName, $configuration['_exclude'])) {
271  continue;
272  }
273 
274  $propertyValue = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, $propertyName);
275 
276  if (!is_array($propertyValue) && !is_object($propertyValue)) {
277  $propertiesToRender[$propertyName] = $propertyValue;
278  } elseif (isset($configuration['_descend']) && array_key_exists($propertyName, $configuration['_descend'])) {
279  $propertiesToRender[$propertyName] = $this->transformValue($propertyValue, $configuration['_descend'][$propertyName]);
280  }
281  }
282  if (isset($configuration['_exposeObjectIdentifier']) && $configuration['_exposeObjectIdentifier'] === TRUE) {
283  if (isset($configuration['_exposedObjectIdentifierKey']) && strlen($configuration['_exposedObjectIdentifierKey']) > 0) {
284  $identityKey = $configuration['_exposedObjectIdentifierKey'];
285  } else {
286  $identityKey = '__identity';
287  }
288  $propertiesToRender[$identityKey] = $this->persistenceManager->getIdentifierByObject($object);
289  }
290  if (isset($configuration['_exposeClassName']) && ($configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED || $configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_UNQUALIFIED)) {
291  $className = get_class($object);
292  $classNameParts = explode('\\', $className);
293  $propertiesToRender['__class'] = ($configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED ? $className : array_pop($classNameParts));
294  }
295 
296  return $propertiesToRender;
297  }
298  }
299 }
transformValue($value, array $configuration)
Definition: JsonView.php:227
transformObject($object, array $configuration)
Definition: JsonView.php:259
setConfiguration(array $configuration)
Definition: JsonView.php:179
static getProperty($subject, $propertyName, $forceDirectAccess=FALSE)
setVariablesToRender(array $variablesToRender)
Definition: JsonView.php:171