-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Form] Add new way of mapping data using callback functions #37968
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form; | ||
|
||
/** | ||
* Writes and reads values to/from an object or array bound to a form. | ||
* | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
*/ | ||
interface DataAccessorInterface | ||
{ | ||
/** | ||
* Returns the value at the end of the property of the object graph. | ||
* | ||
* @param object|array $viewData The view data of the compound form | ||
* @param FormInterface $form The {@link FormInterface()} instance to check | ||
* | ||
* @return mixed The value at the end of the property | ||
* | ||
* @throws Exception\AccessException If unable to read from the given form data | ||
*/ | ||
public function getValue($viewData, FormInterface $form); | ||
|
||
/** | ||
* Sets the value at the end of the property of the object graph. | ||
* | ||
* @param object|array $viewData The view data of the compound form | ||
* @param mixed $value The value to set at the end of the object graph | ||
* @param FormInterface $form The {@link FormInterface()} instance to check | ||
* | ||
* @throws Exception\AccessException If unable to write the given value | ||
*/ | ||
public function setValue(&$viewData, $value, FormInterface $form): void; | ||
xabbuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Returns whether a value can be read from an object graph. | ||
* | ||
* Whenever this method returns true, {@link getValue()} is guaranteed not | ||
* to throw an exception when called with the same arguments. | ||
* | ||
* @param object|array $viewData The view data of the compound form | ||
* @param FormInterface $form The {@link FormInterface()} instance to check | ||
* | ||
* @return bool Whether the value can be read | ||
*/ | ||
public function isReadable($viewData, FormInterface $form): bool; | ||
|
||
/** | ||
* Returns whether a value can be written at a given object graph. | ||
* | ||
* Whenever this method returns true, {@link setValue()} is guaranteed not | ||
* to throw an exception when called with the same arguments. | ||
* | ||
* @param object|array $viewData The view data of the compound form | ||
* @param FormInterface $form The {@link FormInterface()} instance to check | ||
* | ||
* @return bool Whether the value can be set | ||
*/ | ||
public function isWritable($viewData, FormInterface $form): bool; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Exception; | ||
|
||
class AccessException extends RuntimeException | ||
{ | ||
} |
64 changes: 64 additions & 0 deletions
64
src/Symfony/Component/Form/Extension/Core/DataAccessor/CallbackAccessor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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\DataAccessor; | ||
|
||
use Symfony\Component\Form\DataAccessorInterface; | ||
use Symfony\Component\Form\Exception\AccessException; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
/** | ||
* Writes and reads values to/from an object or array using callback functions. | ||
* | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
*/ | ||
class CallbackAccessor implements DataAccessorInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValue($data, FormInterface $form) | ||
{ | ||
if (null === $getter = $form->getConfig()->getOption('getter')) { | ||
throw new AccessException('Unable to read from the given form data as no getter is defined.'); | ||
} | ||
|
||
return ($getter)($data, $form); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setValue(&$data, $value, FormInterface $form): void | ||
{ | ||
if (null === $setter = $form->getConfig()->getOption('setter')) { | ||
throw new AccessException('Unable to write the given value as no setter is defined.'); | ||
} | ||
|
||
($setter)($data, $form->getData(), $form); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isReadable($data, FormInterface $form): bool | ||
{ | ||
return null !== $form->getConfig()->getOption('getter'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isWritable($data, FormInterface $form): bool | ||
{ | ||
return null !== $form->getConfig()->getOption('setter'); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/Symfony/Component/Form/Extension/Core/DataAccessor/ChainAccessor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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\DataAccessor; | ||
|
||
use Symfony\Component\Form\DataAccessorInterface; | ||
use Symfony\Component\Form\Exception\AccessException; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
/** | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
*/ | ||
class ChainAccessor implements DataAccessorInterface | ||
{ | ||
private $accessors; | ||
|
||
/** | ||
* @param DataAccessorInterface[]|iterable $accessors | ||
*/ | ||
public function __construct(iterable $accessors) | ||
{ | ||
$this->accessors = $accessors; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValue($data, FormInterface $form) | ||
{ | ||
foreach ($this->accessors as $accessor) { | ||
if ($accessor->isReadable($data, $form)) { | ||
return $accessor->getValue($data, $form); | ||
} | ||
} | ||
|
||
throw new AccessException('Unable to read from the given form data as no accessor in the chain is able to read the data.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setValue(&$data, $value, FormInterface $form): void | ||
{ | ||
foreach ($this->accessors as $accessor) { | ||
if ($accessor->isWritable($data, $form)) { | ||
$accessor->setValue($data, $value, $form); | ||
|
||
return; | ||
} | ||
} | ||
|
||
throw new AccessException('Unable to write the given value as no accessor in the chain is able to set the data.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isReadable($data, FormInterface $form): bool | ||
{ | ||
foreach ($this->accessors as $accessor) { | ||
if ($accessor->isReadable($data, $form)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isWritable($data, FormInterface $form): bool | ||
{ | ||
foreach ($this->accessors as $accessor) { | ||
if ($accessor->isWritable($data, $form)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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\DataAccessor; | ||
|
||
use Symfony\Component\Form\DataAccessorInterface; | ||
use Symfony\Component\Form\Exception\AccessException; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException; | ||
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
|
||
/** | ||
* Writes and reads values to/from an object or array using property path. | ||
* | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
*/ | ||
class PropertyPathAccessor implements DataAccessorInterface | ||
{ | ||
private $propertyAccessor; | ||
|
||
public function __construct(PropertyAccessorInterface $propertyAccessor = null) | ||
{ | ||
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValue($data, FormInterface $form) | ||
{ | ||
if (null === $propertyPath = $form->getPropertyPath()) { | ||
throw new AccessException('Unable to read from the given form data as no property path is defined.'); | ||
} | ||
|
||
return $this->getPropertyValue($data, $propertyPath); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setValue(&$data, $propertyValue, FormInterface $form): void | ||
{ | ||
if (null === $propertyPath = $form->getPropertyPath()) { | ||
throw new AccessException('Unable to write the given value as no property path is defined.'); | ||
} | ||
|
||
// 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)) { | ||
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)) { | ||
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isReadable($data, FormInterface $form): bool | ||
{ | ||
return null !== $form->getPropertyPath(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isWritable($data, FormInterface $form): bool | ||
{ | ||
return null !== $form->getPropertyPath(); | ||
} | ||
|
||
private function getPropertyValue($data, $propertyPath) | ||
{ | ||
try { | ||
return $this->propertyAccessor->getValue($data, $propertyPath); | ||
} catch (PropertyAccessException $e) { | ||
if (!$e instanceof UninitializedPropertyException | ||
// For versions without UninitializedPropertyException check the exception message | ||
&& (class_exists(UninitializedPropertyException::class) || false === strpos($e->getMessage(), 'You should initialize it')) | ||
) { | ||
throw $e; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.