/** * Returns the names of all properties of this node. * * @return array Property names */ public function getPropertyNames() { if (is_object($this->contentObjectProxy)) { return ObjectAccess::getGettablePropertyNames($this->contentObjectProxy->getObject()); } return array_keys($this->properties); }
/** * @param $entityName * @return string */ protected function getJsonRepresentation($entityName) { $entityData = $this->getObjectsFromRepository($entityName); $entityArray = []; foreach ($entityData as $object) { $propertyNames = \TYPO3\Flow\Reflection\ObjectAccess::getGettablePropertyNames($object); $identifier = \TYPO3\Flow\Reflection\ObjectAccess::getProperty($object, 'uUID'); foreach ($propertyNames as $propertyName) { $propertyValue = \TYPO3\Flow\Reflection\ObjectAccess::getProperty($object, $propertyName); if (!is_array($propertyValue) && !is_object($propertyValue)) { $propertiesToRender[$propertyName] = $propertyValue; } elseif (isset($configuration['_descend']) && array_key_exists($propertyName, $configuration['_descend'])) { $propertiesToRender[$propertyName] = $this->transformValue($propertyValue, $configuration['_descend'][$propertyName]); } $entityArray[] = $propertiesToRender; } } return json_encode(['data' => [$entityArray]]); }
/** * @test */ public function getGettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() { $expectedPropertyNames = array('anotherProperty', 'booleanProperty', 'property', 'property2', 'publicProperty', 'publicProperty2'); $actualPropertyNames = ObjectAccess::getGettablePropertyNames($this->dummyObject); $this->assertEquals($expectedPropertyNames, $actualPropertyNames, 'getGettablePropertyNames returns not all gettable properties.'); }
/** * Traverses the given object structure in order to transform it into an * array structure. * * @param object $object Object to traverse * @param array $configuration Configuration for transforming the given object or NULL * @return array Object structure as an array */ protected function transformObject($object, array $configuration) { if ($object instanceof \DateTime) { return $object->format(\DateTime::ISO8601); } else { $propertyNames = \TYPO3\Flow\Reflection\ObjectAccess::getGettablePropertyNames($object); $propertiesToRender = array(); foreach ($propertyNames as $propertyName) { if (isset($configuration['_only']) && is_array($configuration['_only']) && !in_array($propertyName, $configuration['_only'])) { continue; } if (isset($configuration['_exclude']) && is_array($configuration['_exclude']) && in_array($propertyName, $configuration['_exclude'])) { continue; } $propertyValue = \TYPO3\Flow\Reflection\ObjectAccess::getProperty($object, $propertyName); if (!is_array($propertyValue) && !is_object($propertyValue)) { $propertiesToRender[$propertyName] = $propertyValue; } elseif (isset($configuration['_descend']) && array_key_exists($propertyName, $configuration['_descend'])) { $propertiesToRender[$propertyName] = $this->transformValue($propertyValue, $configuration['_descend'][$propertyName]); } } if (isset($configuration['_exposeObjectIdentifier']) && $configuration['_exposeObjectIdentifier'] === true) { if (isset($configuration['_exposedObjectIdentifierKey']) && strlen($configuration['_exposedObjectIdentifierKey']) > 0) { $identityKey = $configuration['_exposedObjectIdentifierKey']; } else { $identityKey = '__identity'; } $propertiesToRender[$identityKey] = $this->persistenceManager->getIdentifierByObject($object); } if (isset($configuration['_exposeClassName']) && ($configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED || $configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_UNQUALIFIED)) { $className = TypeHandling::getTypeForValue($object); $classNameParts = explode('\\', $className); $propertiesToRender['__class'] = $configuration['_exposeClassName'] === self::EXPOSE_CLASSNAME_FULLY_QUALIFIED ? $className : array_pop($classNameParts); } return $propertiesToRender; } }
/** * map data to simple data structures * * @param mixed $data * @return mixed */ protected function mapToStructure($data) { return $data; if (is_array($data) || is_object($data) && $data instanceof \Traversable) { $return = array(); foreach ($data as $index => $item) { $return[$index] = $this->mapToStructure($item); } } else { if (is_object($data)) { $return = new \stdClass(); $properties = \TYPO3\Flow\Reflection\ObjectAccess::getGettablePropertyNames($data); foreach ($properties as $property) { $return->{$property} = $this->mapToStructure(\TYPO3\Flow\Reflection\ObjectAccess::getProperty($data, $property)); } } else { $return = $data; } } return $return; }
/** * Traverses the given object structure in order to transform it into an * array structure. * * @param object $object Object to traverse * @param array $configuration Configuration for transforming the given object * @param boolean $onlyIdentifier * @return array Object structure as an array * @todo implement sideloading */ protected function transformObject($object, array $configuration, $onlyIdentifier = TRUE) { if ($object instanceof \DateTime) { return $object->format('Y-m-d\\TH:i:s'); } elseif ($onlyIdentifier === TRUE) { $objectIdentifier = $this->persistenceManager->getIdentifierByObject($object); if ($objectIdentifier === NULL && method_exists($object, 'getId')) { $objectIdentifier = $object->getId(); } return $objectIdentifier; } else { $transformedObject = array(); if ($this->excludeIdentifier === FALSE) { $objectIdentifier = $this->persistenceManager->getIdentifierByObject($object); $transformedObject['id'] = $objectIdentifier; } $propertyNames = ObjectAccess::getGettablePropertyNames($object); foreach ($propertyNames as $propertyName) { if (substr($propertyName, 0, 1) !== '_') { if (isset($configuration['_only']) && is_array($configuration['_only']) && !in_array($propertyName, $configuration['_only'])) { continue; } if ($this->propertyIsExcluded($propertyName, $configuration)) { continue; } $propertyValue = ObjectAccess::getProperty($object, $propertyName); $objectName = $this->objectManager->getObjectNameByClassName($this->reflectionService->getClassNameByObject($object)); if (!$this->reflectionService->isPropertyAnnotatedWith($objectName, $propertyName, 'TYPO3\\Flow\\Annotations\\Transient')) { if (is_array($propertyValue) || $propertyValue instanceof \ArrayAccess) { $transformedObject[EmberDataUtility::uncamelize($propertyName)] = $this->transformValue($propertyValue, array()); } elseif ($propertyValue instanceof \DateTime) { $transformedObject[EmberDataUtility::uncamelize($propertyName)] = $propertyValue->format('Y-m-d\\TH:i:s'); } elseif (is_object($propertyValue)) { $objectName = $this->objectManager->getObjectNameByClassName($this->reflectionService->getClassNameByObject($object)); if ($this->objectManager->getScope($objectName) !== \TYPO3\Flow\Object\Configuration\Configuration::SCOPE_SINGLETON) { $transformedObject[EmberDataUtility::uncamelize($propertyName) . '_id'] = $this->persistenceManager->getIdentifierByObject($propertyValue); if (isset($configuration['sideloadedAssociations']) && in_array($propertyName, $configuration['sideloadedAssociations'])) { // sideload this propertyvalue } } } else { $transformedObject[EmberDataUtility::uncamelize($propertyName)] = $propertyValue; } } } } return $transformedObject; } }
/** * Traverses the given object structure in order to transform it into an * array structure. * * @param object $object Object to traverse * @param array $configuration Configuration for transforming the given object * @param boolean $onlyIdentifier * @param boolean $enableSideLoading * @return array Object structure as an array * @todo implement sideloading */ protected function transformObject($object, array $configuration, $onlyIdentifier = TRUE, $enableSideLoading = TRUE) { if ($object instanceof \DateTime) { return $object->format('Y-m-d\\TH:i:s'); } elseif ($onlyIdentifier === TRUE) { return $this->persistenceManager->getIdentifierByObject($object); } else { $transformedObject = array('id' => $this->persistenceManager->getIdentifierByObject($object)); $objectName = $this->objectManager->getObjectNameByClassName($this->reflectionService->getClassNameByObject($object)); foreach (ObjectAccess::getGettablePropertyNames($object) as $propertyName) { if ($this->isPropertyIgnored($objectName, $propertyName)) { continue; } $propertyValue = ObjectAccess::getProperty($object, $propertyName); if (is_array($propertyValue) || $propertyValue instanceof \ArrayAccess) { $propertyValue = $this->transformValue($propertyValue, array(), TRUE, $enableSideLoading); if ($this->isRelation($objectName, $propertyName)) { $this->addLink($transformedObject, $propertyName, $propertyValue); if ($enableSideLoading === TRUE && !$this->reflectionService->isPropertyAnnotatedWith($objectName, $propertyName, 'TYPO3\\Flow\\Annotations\\Lazy')) { $propertyTags = $this->reflectionService->getPropertyTagValues($objectName, $propertyName, 'var'); $propertyObjectType = TypeHandling::parseType($propertyTags[0]); $this->sideLoad($propertyObjectType['elementType'], $propertyValue); } $propertyValue = NULL; } elseif (empty($propertyValue)) { $propertyValue = NULL; } } elseif ($this->isSimpleValue($propertyValue)) { if (is_object($propertyValue)) { $propertyValue = $this->transformObject($propertyValue, $configuration, FALSE, $enableSideLoading); } } elseif (is_object($propertyValue)) { $propertyObjectName = $this->objectManager->getObjectNameByClassName($this->reflectionService->getClassNameByObject($propertyValue)); if (!$this->isObjectIgnored($propertyObjectName)) { $propertyValue = $this->transformObject($propertyValue, $configuration, TRUE, $enableSideLoading); if ($this->isRelation($objectName, $propertyName)) { $this->addLink($transformedObject, $propertyName, $propertyValue); if ($enableSideLoading === TRUE && !$this->reflectionService->isPropertyAnnotatedWith($objectName, $propertyName, 'TYPO3\\Flow\\Annotations\\Lazy')) { $this->sideLoad($propertyObjectName, $propertyValue); } $propertyValue = NULL; } elseif (empty($propertyValue)) { $propertyValue = NULL; } } } if ($propertyValue !== NULL) { if (is_object($propertyValue)) { $propertyObjectName = $this->objectManager->getObjectNameByClassName($this->reflectionService->getClassNameByObject($propertyValue)); if (!$this->isObjectIgnored($propertyObjectName)) { $transformedObject[$propertyName] = $propertyValue; } } else { $transformedObject[$propertyName] = $propertyValue; } } } return $transformedObject; } }