8000 [Form] handle callable strings in PropertyAccessDecorator by HeahDude · Pull Request #18057 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] handle callable strings in PropertyAccessDecorator #18057

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($label) && !is_callable($label)) {
$label = new PropertyPath($label);
} elseif (is_string($label) && is_callable($label)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe this should be the responsibility of the DefaultChoiceListFactory as the interface is supposed to support callable anyway.

Then it would just need to make PropertyAccessDecorator treat callable strings as strings, instead of passing them to the decorated factory as it is.

But one using the DefaultChoiceListFactory without decorator should not experience this bug.

What do you think @webmozart ?

// Prevent a fatal error since a callable string may not handle the right arguments
Copy link
Member

Choose a reason for hiding this comment

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

How does it prevent a fatal error ? You call it with the same argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof Actually choice_label, choice_name, choice_attr, group_by and preferred_choices as callable get passed 3 arguments ($choice, $key, $value) and we got a fatal error with callable such as 'end' but I found out that was not valid because 'MyStatic::getMeChoiceName' would then fail if it needs 3 arguments, so this is a "won't fix" and I closed this PR.

$label = function ($choice) use ($label) {
return $label($choice);
};
}

if ($label instanceof PropertyPath) {
Expand All @@ -182,6 +187,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($preferredChoices) && !is_callable($preferredChoices)) {
$preferredChoices = new PropertyPath($preferredChoices);
} elseif (is_string($preferredChoices) && is_callable($preferredChoices)) {
// Prevent a fatal error since a callable string may not handle the right arguments
$preferredChoices = function ($choice) use ($preferredChoices) {
return $preferredChoices($choice);
};
}

if ($preferredChoices instanceof PropertyPath) {
Expand All @@ -197,6 +207,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($index) && !is_callable($index)) {
$index = new PropertyPath($index);
} elseif (is_string($index) && is_callable($index)) {
// Prevent a fatal error since a callable string may not handle the right arguments
$index = function ($choice) use ($index) {
return $index($choice);
};
}

if ($index instanceof PropertyPath) {
Expand All @@ -207,6 +222,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($groupBy) && !is_callable($groupBy)) {
$groupBy = new PropertyPath($groupBy);
} elseif (is_string($groupBy) && is_callable($groupBy)) {
// Prevent a fatal error since a callable string may not handle the right arguments
$groupBy = function ($choice) use ($groupBy) {
return $groupBy($choice);
};
}

if ($groupBy instanceof PropertyPath) {
Expand All @@ -221,6 +241,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($attr) && !is_callable($attr)) {
$attr = new PropertyPath($attr);
} elseif (is_string($attr) && is_callable($attr)) {
// Prevent a fatal error since a callable string may not handle the right arguments
$attr = function ($choice) use ($attr) {
return $attr($choice);
};
}

if ($attr instanceof PropertyPath) {
Expand Down
5CD8
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests\ChoiceList\Factory;

use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
use Symfony\Component\PropertyAccess\PropertyPath;

Expand Down Expand Up @@ -192,6 +193,25 @@ public function testCreateViewAssumeNullIfPreferredChoicesPropertyPathUnreadable
));
}

public function testCreateViewPreferredChoicesAsCallableString()
{
$list = new ArrayChoiceList(array(
array('end' => 'RESULT'),
));

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred) {
return $preferred(array('end' => 'RESULT'));
}));

$this->assertSame('RESULT', $this->factory->createView(
$list,
'end'
));
}

public function testCreateViewLabelsAsPropertyPath()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down Expand Up @@ -228,6 +248,26 @@ public function testCreateViewLabelsAsPropertyPathInstance()
));
}

public function testCreateViewLabelAsCallableString()
{
$list = new ArrayChoiceList(array(
array('end' => 'RESULT'),
));

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label) {
return $label(array('end' => 'RESULT'));
}));

$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
'end'
));
}

public function testCreateViewIndicesAsPropertyPath()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down Expand Up @@ -266,6 +306,27 @@ public function testCreateViewIndicesAsPropertyPathInstance()
));
}

public function testCreateViewIndicesAsCallableString()
{
$list = new ArrayChoiceList(array(
array('end' => 'RESULT'),
));

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index) {
return $index(array('end' => 'RESULT'));
}));

$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
'end'
));
}

public function testCreateViewGroupsAsPropertyPath()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down Expand Up @@ -327,6 +388,28 @@ public function testCreateViewAssumeNullIfGroupsPropertyPathUnreadable()
));
}

public function testCreateViewGroupsAsCallableString()
{
$list = new ArrayChoiceList(array(
array('end' => 'RESULT'),
));

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy) {
return $groupBy(array('end' => 'RESULT'));
}));

$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
'end'
));
}

public function testCreateViewAttrAsPropertyPath()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down Expand Up @@ -368,4 +451,27 @@ public function testCreateViewAttrAsPropertyPathInstance()
new PropertyPath('property')
));
}

public function testCreateViewAttrAsCallableString()
{
$list = new ArrayChoiceList(array(
array('end' => 'RESULT'),
));

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy, $attr) {
return $attr(array('end' => 'RESULT'));
}));

$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
null, // groups
'end'
));
}
}
0