Skip to content

#48167 add fallbackCallbackAccessor to PropertyPathAccessor to suppor… #53589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy