|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Form\Extension\Core\DataAccessor; |
| 13 | + |
| 14 | +use Symfony\Component\Form\DataAccessorInterface; |
| 15 | +use Symfony\Component\Form\Exception\AccessException; |
| 16 | +use Symfony\Component\Form\FormInterface; |
| 17 | +use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException; |
| 18 | +use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException; |
| 19 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
| 20 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Writes and reads values to/from an object or array using property path. |
| 24 | + * |
| 25 | + * @author Yonel Ceruto <yonelceruto@gmail.com> |
| 26 | + * @author Bernhard Schussek <bschussek@gmail.com> |
| 27 | + */ |
| 28 | +class PropertyPathAccessor implements DataAccessorInterface |
| 29 | +{ |
| 30 | + private $propertyAccessor; |
| 31 | + |
| 32 | + public function __construct(PropertyAccessorInterface $propertyAccessor = null) |
| 33 | + { |
| 34 | + $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritdoc} |
| 39 | + */ |
| 40 | + public function getValue($data, FormInterface $form) |
| 41 | + { |
| 42 | + if (null === $propertyPath = $form->getPropertyPath()) { |
| 43 | + throw new AccessException('Unable to read from the given form data.'); |
| 44 | + } |
| 45 | + |
| 46 | + return $this->getPropertyValue($data, $propertyPath); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * {@inheritdoc} |
| 51 | + */ |
| 52 | + public function setValue(&$data, $propertyValue, FormInterface $form): void |
| 53 | + { |
| 54 | + if (null === $propertyPath = $form->getPropertyPath()) { |
| 55 | + throw new AccessException('Unable to write the given value.'); |
| 56 | + } |
| 57 | + |
| 58 | + // If the field is of type DateTimeInterface and the data is the same skip the update to |
| 59 | + // keep the original object hash |
| 60 | + if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // If the data is identical to the value in $data, we are |
| 65 | + // dealing with a reference |
| 66 | + if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) { |
| 67 | + $this->propertyAccessor->setValue($data, $propertyPath, $propertyValue); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * {@inheritdoc} |
| 73 | + */ |
| 74 | + public function isReadable($data, FormInterface $form): bool |
| 75 | + { |
| 76 | + return null !== $form->getPropertyPath(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritdoc} |
| 81 | + */ |
| 82 | + public function isWritable($data, FormInterface $form): bool |
| 83 | + { |
| 84 | + return null !== $form->getPropertyPath(); |
| 85 | + } |
| 86 | + |
| 87 | + private function getPropertyValue($data, $propertyPath) |
| 88 | + { |
| 89 | + try { |
| 90 | + return $this->propertyAccessor->getValue($data, $propertyPath); |
| 91 | + } catch (PropertyAccessException $e) { |
| 92 | + if (!$e instanceof UninitializedPropertyException |
| 93 | + // For versions without UninitializedPropertyException check the exception message |
| 94 | + && (class_exists(UninitializedPropertyException::class) || false === strpos($e->getMessage(), 'You should initialize it')) |
| 95 | + ) { |
| 96 | + throw $e; |
| 97 | + } |
| 98 | + |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments