/** * Copy a singe object based on field annotations about how to copy the object * * @param Tx_Extbase_DomainObject_DomainObjectInterface $object The object to be copied * @return Tx_Extbase_DomainObject_DomainObjectInterface $copy * @api */ public function copy($object) { $className = get_class($object); $this->recursionService->in(); $this->recursionService->check($className); $copy = $this->objectManager->get($className); $properties = $this->reflectionService->getClassPropertyNames($className); foreach ($properties as $propertyName) { $tags = $this->reflectionService->getPropertyTagsValues($className, $propertyName); $getter = 'get' . ucfirst($propertyName); $setter = 'set' . ucfirst($propertyName); $copyMethod = $tags['copy'][0]; $copiedValue = NULL; if ($copyMethod !== NULL && $copyMethod !== 'ignore') { $originalValue = $object->{$getter}(); if ($copyMethod == 'reference') { $copiedValue = $this->copyAsReference($originalValue); } elseif ($copyMethod == 'clone') { $copiedValue = $this->copyAsClone($originalValue); } if ($copiedValue != NULL) { $copy->{$setter}($copiedValue); } } } $this->recursionService->out(); return $copy; }
/** * Returns an array of property names and values by searching the $object * for annotations based on $annotation and $value. If $annotation is provided * but $value is not, All properties which simply have the annotation present. * Relational values which have the annotation are parsed through the same * function - sub-elements' properties are exported based on the same * annotation and value * * @param mixed $object The object or classname to read * @param string $annotation The annotation on which to base output * @param string|boolean $value The value to search for; multiple values may be used in the annotation; $value must be present among them. If TRUE, all properties which have the annotation are returned * @param boolean $addUid If TRUE, the UID of the DomainObject will be force-added to the output regardless of annotation * @return array * @api */ public function getValuesByAnnotation($object, $annotation = 'json', $value = TRUE, $addUid = TRUE) { if (is_array($object)) { $array = array(); foreach ($object as $k => $v) { $array[$k] = $this->getValuesByAnnotation($v, $annotation, $value, $addUid); } return $array; } if (is_object($object)) { $className = get_class($object); } else { $className = $object; $object = $this->objectManager->get($className); } $this->recursionService->in(); $this->recursionService->check($className); $properties = $this->reflectionService->getClassPropertyNames($className); $return = array(); if ($addUid === TRUE) { $return['uid'] = $object->getUid(); } foreach ($properties as $propertyName) { $getter = 'get' . ucfirst($propertyName); if (method_exists($object, $getter) === FALSE) { continue; } if ($this->hasAnnotation($className, $propertyName, $annotation, $value)) { $returnValue = $object->{$getter}(); if ($returnValue instanceof Tx_Extbase_Persistence_ObjectStorage) { $array = $returnValue->toArray(); foreach ($array as $k => $v) { $array[$k] = $this->getValuesByAnnotation($v, $annotation, $value, $addUid); } $returnValue = $array; } elseif ($returnValue instanceof Tx_Extbase_DomainObject_DomainObjectInterface) { $returnValue = $this->getValuesByAnnotation($returnValue, $annotation, $value, $addUid); } elseif ($returnValue instanceof DateTime) { $returnValue = $returnValue->format('r'); } $return[$propertyName] = $returnValue; } } $this->recursionService->out(); return $return; }