/** * {@inheritdoc} */ public function transform($object, ContextInterface $context) { if (!$object instanceof \Traversable) { throw UnsupportedClassException::create($object); } if (!$object instanceof \ArrayAccess) { throw new UnsupportedClassException(sprintf('The collection object for transform should implement \\ArrayAccess, %s given.', get_class($object))); } // Try crete a new collection try { $transformed = $this->createCollection($object); } catch (\Exception $e) { throw new UnsupportedClassException(sprintf('Could not create new collection instance with class "%s" (Use constructor).', get_class($object)), 0, $e); } // Try get a child context $childContext = $context->getAttribute('child_context', null); foreach ($object as $key => $child) { $transformed[$key] = $this->manager->transform($child, $childContext); } return $transformed; }
/** * {@inheritDoc} */ public function transform($object, ContextInterface $context) { $metadata = $this->metadataFactory->loadMetadata(get_class($object)); // Get properties for transformation if (!$context->getGroups()) { $transformProperties = $metadata->getProperties(); } else { $transformProperties = $metadata->getPropertiesForGroups($context->getGroups()); } // Try create transformed $transformedClass = $metadata->getTransformedClass(); $transformedReflection = Reflection::loadClassReflection($transformedClass); $transformed = $metadata->isEvaluateConstructor() ? $transformedReflection->newInstance() : $transformedReflection->newInstanceWithoutConstructor(); foreach ($transformProperties as $transformPropertyName => $propertyMetadata) { try { $objectPropertyReflection = Reflection::loadPropertyReflection($object, $transformPropertyName); } catch (\ReflectionException $e) { throw new \RuntimeException(sprintf('Error transform property: Not found property "%s" in class "%s".', $transformPropertyName, get_class($object)), 0, $e); } try { $transformedPropertyReflection = $transformedReflection->getProperty($propertyMetadata->getPropertyName()); } catch (\ReflectionException $e) { throw new \RuntimeException(sprintf('Error transform property: Not found property "%s" in class "%s".', $propertyMetadata->getPropertyName(), $transformedReflection->getName())); } if (!$transformedPropertyReflection->isPublic()) { $transformedPropertyReflection->setAccessible(true); } if (!$objectPropertyReflection->isPublic()) { $objectPropertyReflection->setAccessible(true); } $objectPropertyValue = $objectPropertyReflection->getValue($object); $transformedValue = $this->transformValue($object, $objectPropertyValue, $propertyMetadata, $transformedPropertyReflection); $transformedPropertyReflection->setValue($transformed, $transformedValue); } return $transformed; }