-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Ease immutable/value objects mapping #19367
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
Closed
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
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/Form/Extension/Core/DataMapper/CallbackFormDataToObjectConverter.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,41 @@ | ||
<?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\DataMapper; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
class CallbackFormDataToObjectConverter implements FormDataToObjectConverterInterface | ||
{ | ||
/** | ||
* The callable used to map form data to an object. | ||
* | ||
* @var callable | ||
*/ | ||
private $converter; | ||
|
||
/** | ||
* @param callable $converter | ||
*/ | ||
public function __construct(callable $converter) | ||
{ | ||
$this->converter = $converter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertFormDataToObject(array $data, $originalData) | ||
{ | ||
return call_user_func($this->converter, $data, $originalData); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Symfony/Component/Form/Extension/Core/DataMapper/FormDataToObjectConverterInterface.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,28 @@ | ||
<?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\DataMapper; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
interface FormDataToObjectConverterInterface | ||
{ | ||
/** | ||
* Convert the form data into an object. | ||
* | ||
* @param array $data Array of form data indexed by fields names. | ||
* @param object|null $originalData Original data set in the form (after FormEvents::PRE_SET_DATA). | ||
* | ||
* @return object|null | ||
*/ | ||
public function convertFormDataToObject(array $data, $originalData); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/Form/Extension/Core/DataMapper/ObjectToFormDataConverterInterface.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,27 @@ | ||
<?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\DataMapper; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
interface ObjectToFormDataConverterInterface | ||
{ | ||
/** | ||
* Convert given object to form data. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Converts. |
||
* | ||
* @param object|null $object The object to map to the form. | ||
* | ||
* @return array The array of form data indexed by fields names. | ||
*/ | ||
public function convertObjectToFormData($object); | ||
} |
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/Form/Extension/Core/DataMapper/SimpleObjectMapper.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,91 @@ | ||
<?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\DataMapper; | ||
|
||
use Symfony\Component\Form\DataMapperInterface; | ||
use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
class SimpleObjectMapper implements DataMapperInterface | ||
{ | ||
/** | ||
* @var FormDataToObjectConverterInterface | ||
*/ | ||
private $converter; | ||
|
||
/** | ||
* @var DataMapperInterface|null | ||
*/ | ||
private $originalMapper; | ||
|
||
/** | ||
* @param FormDataToObjectConverterInterface $converter | ||
* @param DataMapperInterface|null $originalMapper | ||
*/ | ||
public function __construct(FormDataToObjectConverterInterface $converter, DataMapperInterface $originalMapper = null) | ||
{ | ||
$this->converter = $converter; | ||
$this->originalMapper = $originalMapper; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function mapDataToForms($data, $forms) | ||
{ | ||
// Fallback to original mapper instance or default to "PropertyPathMapper" | ||
// mapper implementation if not an "ObjectToFormDataConverterInterface" instance: | ||
if (!$this->converter instanceof ObjectToFormDataConverterInterface) { | ||
$propertyPathMapper = $this->originalMapper ?: new PropertyPathMapper(); | ||
$propertyPathMapper->mapDataToForms($data, $forms); | ||
|
||
return; | ||
} | ||
|
||
if (!is_object($data) && null !== $data) { | ||
throw new UnexpectedTypeException($data, 'object or null'); | ||
} | ||
|
||
$data = $this->converter->convertObjectToFormData($data); | ||
|
||
if (!is_array($data)) { | ||
throw new UnexpectedTypeException($data, 'array'); | ||
} | ||
|
||
foreach ($forms as $form) { | ||
$config = $form->getConfig(); | ||
|
||
if ($config->getMapped() && isset($data[$form->getName()])) { | ||
$form->setData($data[$form->getName()]); | ||
|
||
continue; | ||
} | ||
|
||
$form->setData($config->getData()); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function mapFormsToData($forms, &$data) | ||
{ | ||
$fieldsData = array(); | ||
foreach ($forms as $form) { | ||
$fieldsData[$form->getName()] = $form->getData(); | ||
} | ||
|
||
$data = $this->converter->convertFormDataToObject($fieldsData, $data); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
.../Component/Form/Tests/Extension/Core/DataMapper/CallbackFormDataToObjectConverterTest.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,32 @@ | ||
<?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\Tests\Extension\Core\DataMapper; | ||
|
||
use Symfony\Component\Form\Extension\Core\DataMapper\CallbackFormDataToObjectConverter; | ||
|
||
class CallbackFormDataToObjectConverterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConvertFormDataToObject() | ||
{ | ||
$data = array('amount' => 15.0, 'currency' => 'EUR'); | ||
$originalData = (object) $data; | ||
|
||
$converter = new CallbackFormDataToObjectConverter(function ($arg1, $arg2) use ($data, $originalData) { | ||
$this->assertSame($data, $arg1); | ||
$this->assertSame($originalData, $arg2); | ||
|
||
return 'converted'; | ||
}); | ||
|
||
$this->assertSame('converted', $converter->convertFormDataToObject($data, $originalData)); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{@inheritdoc}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This interface does not extend any other interface.