Skip to content

Commit

Permalink
Merge pull request #23 from biig-io/fix/broken-custom-datamapper-feature
Browse files Browse the repository at this point in the history
This is some fix for custom data mapper
  • Loading branch information
Maxime Veber authored Aug 1, 2019
2 parents 76b6730 + ecf751c commit 91b0c5f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('melodiia');

if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/Bridge/Symfony/Form/DomainObjectsDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Biig\Melodiia\Bridge\Symfony\Form;

use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\FormInterface;

Expand All @@ -18,7 +17,7 @@ public function mapFormsToData($forms, &$data)
}

/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function createObject(iterable $form, string $dataClass = null)
{
Expand Down
9 changes: 5 additions & 4 deletions src/Bridge/Symfony/Form/Type/ApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Biig\Melodiia\Bridge\Symfony\Form\Type;

use Biig\Melodiia\Bridge\Symfony\Form\DomainObjectDataMapperInterface;
use Biig\Melodiia\Bridge\Symfony\Form\DomainObjectsDataMapper;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
Expand All @@ -23,12 +24,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$dataMapper = $this->dataMapper;

if ($options['customDataMapper'] !== null) {
if (null !== $options['customDataMapper']) {
$dataMapper = $options['customDataMapper'];
}

if ($options['value_object']) {
$builder->setDataMapper($this->dataMapper);
$builder->setDataMapper($dataMapper);
}
}

Expand All @@ -49,14 +50,14 @@ public function configureOptions(OptionsResolver $resolver)
*/
'empty_data' => function (FormInterface $form) {
$dataMapper = $this->dataMapper;
if ($form->getConfig()->getOption('customDataMapper') !== null) {
if (null !== $form->getConfig()->getOption('customDataMapper')) {
$dataMapper = $form->getConfig()->getOption('customDataMapper');
}

return $dataMapper->createObject($form);
},
]);

$resolver->setAllowedTypes('customDataMapper', ['null', DomainObjectsDataMapper::class]);
$resolver->setAllowedTypes('customDataMapper', ['null', DomainObjectDataMapperInterface::class]);
}
}
11 changes: 8 additions & 3 deletions tests/Melodiia/Bridge/Symfony/Form/ApiTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@ public function testItReturnsDataForEmptyDataInCaseOfDataAvailable()

public function testItSupportsCustomDataMapper()
{
$customDataMapper = new class extends DomainObjectsDataMapper {
$customDataMapper = new class() extends DomainObjectsDataMapper {
private $hasBeenCalled = false;

public function createObject(iterable $form, string $dataClass = null)
{
$this->hasBeenCalled = true;

return parent::createObject($form, $dataClass);
}

public function hasBeenCalled() { return $this->hasBeenCalled; }
public function hasBeenCalled()
{
return $this->hasBeenCalled;
}
};
$form = $this->factory->createNamed('', FakeTypeUsingApiType::class, null, [
'customDataMapper' => $customDataMapper
'customDataMapper' => $customDataMapper,
]);
$form->submit(['foo' => 'some content']);
$data = $form->getData();
Expand Down

0 comments on commit 91b0c5f

Please sign in to comment.