Skip to content

Commit 97edd18

Browse files
committed
add new way to write/read values to/from an object or array using callback functions
1 parent 4ee85e8 commit 97edd18

24 files changed

+932
-60
lines changed

UPGRADE-5.2.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ FrameworkBundle
1616
used to be added by default to the seed, which is not the case anymore. This allows sharing caches between
1717
apps or different environments.
1818

19+
Form
20+
----
21+
22+
* Deprecated `PropertyPathMapper` in favor of `DataMapper` and `PropertyPathAccessor`.
23+
24+
Before:
25+
26+
```php
27+
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
28+
29+
$builder->setDataMapper(new PropertyPathMapper());
30+
```
31+
32+
After:
33+
34+
```php
35+
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
36+
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
37+
38+
$builder->setDataMapper(new DataMapper(new PropertyPathAccessor()));
39+
```
40+
1941
Mime
2042
----
2143

UPGRADE-6.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Form
4848
* Added argument `callable|null $filter` to `ChoiceListFactoryInterface::createListFromChoices()` and `createListFromLoader()`.
4949
* The `Symfony\Component\Form\Extension\Validator\Util\ServerParams` class has been removed, use its parent `Symfony\Component\Form\Util\ServerParams` instead.
5050
* The `NumberToLocalizedStringTransformer::ROUND_*` constants have been removed, use `\NumberFormatter::ROUND_*` instead.
51+
* Removed `PropertyPathMapper` in favor of `DataMapper` and `PropertyPathAccessor`.
5152

5253
FrameworkBundle
5354
---------------

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ CHANGELOG
44
5.2.0
55
-----
66

7-
* added `FormErrorNormalizer`
7+
* Added `FormErrorNormalizer`
88
* Added support for using the `{{ label }}` placeholder in constraint messages, which is replaced in the `ViolationMapper` by the corresponding field form label.
9+
* Added `DataMapper`, `ChainAccessor`, `PropertyPathAccessor` and `CallbackAccessor` with new callable `get` and `set` options for each form type
10+
* Deprecated `PropertyPathMapper` in favor of `DataMapper` and `PropertyPathAccessor`
911

1012
5.1.0
1113
-----
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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;
13+
14+
/**
15+
* Writes and reads values to/from an object or array bound to a form.
16+
*
17+
* @author Yonel Ceruto <yonelceruto@gmail.com>
18+
*/
19+
interface DataAccessorInterface
20+
{
21+
/**
22+
* Returns the value at the end of the property of the object graph.
23+
*
24+
* @param object|array $objectOrArray The object or array to traverse
25+
* @param FormInterface $form The {@link FormInterface()} instance to check
26+
*
27+
* @return mixed The value at the end of the property
28+
*/
29+
public function getValue($objectOrArray, FormInterface $form);
30+
31+
/**
32+
* Sets the value at the end of the property of the object graph.
33+
*
34+
* @param object|array $objectOrArray The object or array to modify
35+
* @param mixed $value The value to set at the end of the object graph
36+
* @param FormInterface $form The {@link FormInterface()} instance to check
37+
*/
38+
public function setValue(&$objectOrArray, $value, FormInterface $form): void;
39+
40+
/**
41+
* Returns whether a value can be read from an object graph.
42+
*
43+
* Whenever this method returns true, {@link getValue()} is guaranteed not
44+
* to throw an exception when called with the same arguments.
45+
*
46+
* @param object|array $objectOrArray The object or array to check
47+
* @param FormInterface $form The {@link FormInterface()} instance to check
48+
*
49+
* @return bool Whether the value can be read
50+
*/
51+
public function isReadable($objectOrArray, FormInterface $form): bool;
52+
53+
/**
54+
* Returns whether a value can be written at a given object graph.
55+
*
56+
* Whenever this method returns true, {@link setValue()} is guaranteed not
57+
* to throw an exception when called with the same arguments.
58+
*
59+
* @param object|array $objectOrArray The object or array to check
60+
* @param FormInterface $form The {@link FormInterface()} instance to check
61+
*
62+
* @return bool Whether the value can be set
63+
*/
64+
public function isWritable($objectOrArray, FormInterface $form): bool;
65+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\RuntimeException;
16+
use Symfony\Component\Form\FormInterface;
17+
18+
/**
19+
* Writes and reads values to/from an object or array using callback functions.
20+
*
21+
* @author Yonel Ceruto <yonelceruto@gmail.com>
22+
*/
23+
class CallbackAccessor implements DataAccessorInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function getValue($data, FormInterface $form)
29+
{
30+
if (null !== $getter = $form->getConfig()->getOption('get')) {
31+
return ($getter)($data, $form);
32+
}
33+
34+
throw new RuntimeException('Unable to read the given form data.');
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function setValue(&$data, $value, FormInterface $form): void
41+
{
42+
if (null !== $setter = $form->getConfig()->getOption('set')) {
43+
($setter)($data, $form->getData(), $form);
44+
45+
return;
46+
}
47+
48+
throw new RuntimeException('Unable to write the given value.');
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function isReadable($data, FormInterface $form): bool
55+
{
56+
return null !== $form->getConfig()->getOption('get');
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function isWritable($data, FormInterface $form): bool
63+
{
64+
return null !== $form->getConfig()->getOption('set');
65+
}
66+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\RuntimeException;
16+
use Symfony\Component\Form\FormInterface;
17+
18+
/**
19+
* @author Yonel Ceruto <yonelceruto@gmail.com>
20+
*/
21+
class ChainAccessor implements DataAccessorInterface
22+
{
23+
private $accessors;
24+
25+
/**
26+
* @param DataAccessorInterface[]|iterable $accessors
27+
*/
28+
public function __construct(iterable $accessors)
29+
{
30+
$this->accessors = $accessors;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function getValue($data, FormInterface $form)
37+
{
38+
foreach ($this->accessors as $accessor) {
39+
if ($accessor->isReadable($data, $form)) {
40+
return $accessor->getValue($data, $form);
41+
}
42+
}
43+
44+
throw new RuntimeException('Unable to read the given form data.');
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function setValue(&$data, $value, FormInterface $form): void
51+
{
52+
foreach ($this->accessors as $accessor) {
53+
if ($accessor->isWritable($data, $form)) {
54+
$accessor->setValue($data, $value, $form);
55+
56+
return;
57+
}
58+
}
59+
60+
throw new RuntimeException('Unable to write the given value.');
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function isReadable($data, FormInterface $form): bool
67+
{
68+
foreach ($this->accessors as $accessor) {
69+
if ($accessor->isReadable($data, $form)) {
70+
return true;
71+
}
72+
}
73+
74+
return false;
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function isWritable($data, FormInterface $form): bool
81+
{
82+
foreach ($this->accessors as $accessor) {
83+
if ($accessor->isWritable($data, $form)) {
84+
return true;
85+
}
86+
}
87+
88+
return false;
89+
}
90+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\RuntimeException;
16+
use Symfony\Component\Form\FormInterface;
17+
use Symfony\Component\PropertyAccess\Exception\AccessException;
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+
return $this->getPropertyValue($data, $propertyPath);
44+
}
45+
46+
throw new RuntimeException('Unable to read the given form data.');
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function setValue(&$data, $propertyValue, FormInterface $form): void
53+
{
54+
if (null !== $propertyPath = $form->getPropertyPath()) {
55+
// If the field is of type DateTimeInterface and the data is the same skip the update to
56+
// keep the original object hash
57+
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) {
58+
return;
59+
}
60+
61+
// If the data is identical to the value in $data, we are
62+
// dealing with a reference
63+
if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) {
64+
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue);
65+
}
66+
67+
return;
68+
}
69+
70+
throw new RuntimeException('Unable to write the given value.');
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function isReadable($data, FormInterface $form): bool
77+
{
78+
return null !== $form->getPropertyPath();
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function isWritable($data, FormInterface $form): bool
85+
{
86+
return null !== $form->getPropertyPath();
87+
}
88+
89+
private function getPropertyValue($data, $propertyPath)
90+
{
91+
try {
92+
return $this->propertyAccessor->getValue($data, $propertyPath);
93+
} catch (AccessException $e) {
94+
if (!$e instanceof UninitializedPropertyException
95+
// For versions without UninitializedPropertyException check the exception message
96+
&& (class_exists(UninitializedPropertyException::class) || false === strpos($e->getMessage(), 'You should initialize it'))
97+
) {
98+
throw $e;
99+
}
100+
101+
return null;
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)
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