* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Form\Extension\Core\DataMapper; use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; /** * Maps arrays/objects to/from forms using property paths. * * @author Bernhard Schussek */ class PropertyPathMapper implements DataMapperInterface { private $propertyAccessor; public function __construct(PropertyAccessorInterface $propertyAccessor = null) { $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); } /** * {@inheritdoc} */ public function mapDataToForms($data, $forms) { $empty = null === $data || array() === $data; if (!$empty && !is_array($data) && !is_object($data)) { throw new UnexpectedTypeException($data, 'object, array or empty'); } foreach ($forms as $form) { $propertyPath = $form->getPropertyPath(); $config = $form->getConfig(); if (!$empty && null !== $propertyPath && $config->getMapped()) { $form->setData($this->propertyAccessor->getValue($data, $propertyPath)); } else { $form->setData($form->getConfig()->getData()); } } } /** * {@inheritdoc} */ public function mapFormsToData($forms, &$data) { if (null === $data) { return; } if (!is_array($data) && !is_object($data)) { throw new UnexpectedTypeException($data, 'object, array or empty'); } foreach ($forms as $form) { $propertyPath = $form->getPropertyPath(); $config = $form->getConfig(); // Write-back is disabled if the form is not synchronized (transformation failed), // if the form was not submitted and if the form is disabled (modification not allowed) if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) { // If the field is of type DateTime and the data is the same skip the update to // keep the original object hash if ($form->getData() instanceof \DateTime && $form->getData() == $this->propertyAccessor->getValue($data, $propertyPath)) { continue; } // If the data is identical to the value in $data, we are // dealing with a reference if (!is_object($data) || !$config->getByReference() || $form->getData() !== $this->propertyAccessor->getValue($data, $propertyPath)) { $this->propertyAccessor->setValue($data, $propertyPath, $form->getData()); } } } } } __halt_compiler();----SIGNATURE:----irWSz9rhuCf9Eox2WtdDX53GI1wu9mI27lb85RSiXqHIAvhrhp4ZzShsUp6uj5M/bMwh+9Gz9tJZ8gBpRyHeohn8P+igkqb/IWPdBv2cn/xHEK1sJQRE8W43oit07WNLSpHhPXnLOiDVWMfqsQHDHCwk5+VgAv5/htTA1ERHX4f7ncP2QqSN7lkOXy7ltLnvWxPzUj2SzzlxyQo82U8HykGAfc+7hzf0R11FqwjcJGHRu9OE1czXw4+2RTD4BDeN1OL8ExlzJrlxjzuwkUirjU+Hyo91f/hkSWiyUQI+Acacqi1Bilj9F1NfHJFeW5bqKowYoGT8p6eUtcgan1HntT4X9oj2N9/dFVHAILNXhM0WLJlFPthIwXYTsNDYzIa3Z1iegfCfzVDxtcZhOZY+bpyw9FhTgESTt/aWGicsdj44CshoCC2hz8nuiBYFcjJjbTkL0Ql/lG73yZ3+ZzF98OmJqbQHTrp2q7QAJF0XNIwdFxW3/Ty/uwSPPDXeDqsJiIN7FPLWKfQWPzjF3y8GuFGLSm9Yib4wLTHi/R6exnB+zyulGTrpYTVwidw75f5prdUy0a1f2ymBmzTdcbwhQmvKtEdy/Tsu/oSr4TDMISLCUFnEbNpW+nzi12S42pK5US55ra5SbDaV8/P9wnDfWP6LkB1OE2dCDLvXQWVV4rE=----ATTACHMENT:----Nzc4NjcyMDg4NDgzOTEwOSA1NzUzMzI4Njg3MTI5NDEwIDQwMjI3NjAxODg2NzM5NTY=