123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- /*
- * This file is part of the Sonata project.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\BaseApplicationBundle\Form\ValueTransformer;
- use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
- use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
- use Symfony\Component\Form\Configurable;
- use Symfony\Component\Form\Exception\InvalidPropertyException;
- use Symfony\Component\Form\Exception\PropertyAccessDeniedException;
- class ArrayToObjectTransformer extends Configurable implements ValueTransformerInterface
- {
- protected function configure()
- {
- $this->addRequiredOption('em');
- $this->addRequiredOption('className');
- parent::configure();
- }
- /**
- * @param array $ids
- * @param Collection $collection
- */
- public function reverseTransform($array)
- {
- $class = $this->getOption('className');
- $metadata = $this->getOption('em')->getClassMetadata($class);
- // when the object is created the form return an array
- // one the object is persisted, the edit $array is the user instance
- if ($array instanceof $class)
- {
- return $array;
- }
- $instance = new $class;
- if (!is_array($array)) {
-
- return $instance;
- }
- $reflClass = $metadata->reflClass;
- foreach ($array as $name => $value) {
- $reflection_property = false;
- // property or association ?
- if (array_key_exists($name, $metadata->fieldMappings)) {
- $property = $metadata->fieldMappings[$name]['fieldName'];
- $reflection_property = $metadata->reflFields[$name];
- } else if (array_key_exists($name, $metadata->associationMappings)) {
- $property = $metadata->associationMappings[$name]['fieldName'];
- } else {
- $property = $name;
- }
- $setter = 'set'.$this->camelize($name);
- if ($reflClass->hasMethod($setter)) {
- if (!$reflClass->getMethod($setter)->isPublic()) {
- throw new PropertyAccessDeniedException(sprintf('Method "%s()" is not public in class "%s"', $setter, $reflClass->getName()));
- }
- $instance->$setter($value);
- } else if ($reflClass->hasMethod('__set')) {
- // needed to support magic method __set
- $instance->$property = $value;
- } else if ($reflClass->hasProperty($property)) {
- if (!$reflClass->getProperty($property)->isPublic()) {
- throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?', $property, $reflClass->getName(), ucfirst($property)));
- }
- $instance->$property = $value;
- } else if ($reflection_property) {
- $reflection_property->setValue($instance, $value);
- }
- }
- return $instance;
- }
- /**
- * @param Collection $value
- */
- public function transform($value)
- {
- return $value;
- }
- /**
- * method taken from PropertyPath
- *
- * @param $property
- * @return mixed
- */
- protected function camelize($property)
- {
- return preg_replace(array('/(^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
- }
- }
|