Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
class PropertyPathAccessor implements DataAccessorInterface
{
private $propertyAccessor;
private $fallbackCallbackAccessorForCustomGetter;

public function __construct(?PropertyAccessorInterface $propertyAccessor = null)
{
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
$this->fallbackCallbackAccessorForCustomGetter = new CallbackAccessor();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simpler, but it's limited to falling back on CallbackAccessor only, when it could be any DataAccessorInterface adapter.

See #54723 for a more generic solution.

}

/**
Expand All @@ -45,7 +47,7 @@ public function getValue($data, FormInterface $form)
throw new AccessException('Unable to read from the given form data as no property path is defined.');
}

return $this->getPropertyValue($data, $propertyPath);
return $this->getPropertyValue($data, $propertyPath, $form);
}

/**
Expand All @@ -59,13 +61,17 @@ public function setValue(&$data, $propertyValue, FormInterface $form): void

// If the field is of type DateTimeInterface and the data is the same skip the update to
// keep the original object hash
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) {
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath, $form)) {
return;
}

// If the data is identical to the value in $data, we are
// dealing with a reference
if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) {
if (
\is_object($data) ||
!$form->getConfig()->getByReference() ||
$propertyValue !== $this->getPropertyValue($data, $propertyPath, $form)
) {
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue);
}
}
Expand All @@ -86,7 +92,7 @@ public function isWritable($data, FormInterface $form): bool
return null !== $form->getPropertyPath();
}

private function getPropertyValue($data, PropertyPathInterface $propertyPath)
private function getPropertyValue($data, PropertyPathInterface $propertyPath, FormInterface $form)
{
try {
return $this->propertyAccessor->getValue($data, $propertyPath);
Expand All @@ -95,6 +101,12 @@ private function getPropertyValue($data, PropertyPathInterface $propertyPath)
return null;
}

try {
return $this->fallbackCallbackAccessorForCustomGetter->getValue($data, $form);
} catch (AccessException $accessException) {
// todo what do we do with this exception? throw it? ignore it?
}

if (!$e instanceof UninitializedPropertyException
// For versions without UninitializedPropertyException check the exception message
&& (class_exists(UninitializedPropertyException::class) || false === strpos($e->getMessage(), 'You should initialize it'))
Expand Down