Skip to content

[POC] [Form] Support get/set accessors for form fields #37614

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Form] Support immutable objects in AccessorMapper
  • Loading branch information
alcaeus committed Jul 20, 2020
commit 2055af53d8cd448d32a4606c6c29bd4492216e9e
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ public function mapFormsToData(iterable $forms, &$data)
// Write-back is disabled if the form is not synchronized (transformation failed),
// if the form was not submitted and if the form is disabled (modification not allowed)
if (null !== $this->set && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be possible for $this->set to be null as you checked it a few lines earlier (to fallback on the old datamapper).

Unless I read something wrong ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. I'll update this.

($this->set)($data, $form->getData());
$returnValue = ($this->set)($data, $form->getData());
$type = is_object($returnValue) ? get_class($returnValue) : gettype($returnValue);

if (
(is_scalar($data) && gettype($data) === $type)
|| (is_array($data) && is_array($returnValue))
|| (is_object($data) && $returnValue instanceof $type)) {
Copy link
Member

@yceruto yceruto Jul 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Immutability] We could also apply the same approach as data mappers but on the closure &$data and get rid of extra checks and rules in our side, so it's the user responsability and it'll work same:

$builder->add('name', null, [
    'set' => function(Category &$category, string $name) { 
        $category = $category->rename($name); 
    },
]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea. Passing the argument by reference also makes it clearer that you are always modifying the data directly, apart from being consistent with the data mapper API.

$data = $returnValue;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,47 @@ public function getBar()
$this->assertSame('bar', $data->getBar());
}

public function testSetAccessorSupportsImmutableObjects()
{
$this->setupPropertyPathMapper($this->any(), $this->any());

$data = new class('petrol') {
private $engine;

public function __construct(string $engine)
{
$this->engine = $engine;
}

public function setEngineClosure($data)
{
return new self($data);
}

public function getEngineClosure()
{
return $this->engine;
}
};

$config = new FormConfigBuilder('car', null, $this->dispatcher);
$config->setCompound(true);
$config->setDataMapper($this->createMapper(true, true));
$config->setData($data);
$form = new Form($config);
$form
->add(new Form(new FormConfigBuilder('engine', null, $this->dispatcher)));

$form->submit(['engine' => 'electric']);
$this->assertNull($form->getTransformationFailure());

$formData = $form->getData();

$this->assertNotSame($data, $formData);
$this->assertSame('electric', $formData->getEngineClosure());
$this->assertSame('petrol', $data->getEngineClosure());
}

private function setupPropertyPathMapper(Invocation $dataToFormsMatcher, Invocation $formsToDataMatcher): void
{
$propertyPathMapper = new PropertyPathMapper($this->propertyAccessor);
Expand All @@ -217,7 +258,7 @@ private function createMapper(bool $useGetClosure, bool $useSetClosure)
{
return new AccessorMapper(
$useGetClosure ? function ($object) { return $object->getEngineClosure(); } : null,
$useSetClosure ? function ($object, $value) { $object->setEngineClosure($value); } : null,
$useSetClosure ? function ($object, $value) { return $object->setEngineClosure($value); } : null,
$this->propertyPathMapper
);
}
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