Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] label path in ObjectChoiceList can also be callable #3479

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 2 commits 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 @@ -71,9 +71,11 @@ class ObjectChoiceList extends ChoiceList
* key pointing to the nested array.
* @param string $labelPath A property path pointing to the property used
* for the choice labels. The value is obtained
* by calling the getter on the object. If the
* path is NULL, the object's __toString() method
* is used instead.
* by calling the getter on the object. You can
* also pass any callable. When callable will be
* executed it will get the choice parameter passed.

Choose a reason for hiding this comment

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

I would say :

Alternatively, $labelPath can be a PHP callable
taking an element from the ObjectChoiceList as
parameter and returning a string.

But then $labelPath should be documented as a mixed parameter ?

* If the path is NULL, the object's __toString()
* method is used instead.
* @param array $preferredChoices A flat array of choices that should be
* presented to the user with priority.
* @param string $groupPath A property path pointing to the property used
Expand All @@ -88,7 +90,14 @@ class ObjectChoiceList extends ChoiceList
*/
public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, $indexPath = null)
{
$this->labelPath = $labelPath ? new PropertyPath($labelPath) : null;
if (is_scalar($labelPath)) {
$this->labelPath = new PropertyPath($labelPath);
} elseif (is_callable($labelPath)) {
$this->labelPath = $labelPath;
} else {
$this->labelPath = null;
}

$this->groupPath = $groupPath ? new PropertyPath($groupPath) : null;
$this->valuePath = $valuePath ? new PropertyPath($valuePath) : null;
$this->indexPath = $indexPath ? new PropertyPath($indexPath) : null;
Expand Down Expand Up @@ -195,8 +204,10 @@ private function extractLabels($choices, array &$labels)
if (is_array($choice) || $choice instanceof \Traversable) {
$labels[$i] = array();
$this->extractLabels($choice, $labels[$i]);
} elseif ($this->labelPath) {
} elseif ($this->labelPath instanceof PropertyPath) {
$labels[$i] = $this->labelPath->getValue($choice);
} elseif (is_callable($this->labelPath)) {
$labels[$i] = call_user_func($this->labelPath, $choice);
} elseif (method_exists($choice, '__toString')) {
$labels[$i] = (string) $choice;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ protected function setUp()
{
parent::setUp();

$this->obj1 = (object) array('name' => 'A');
$this->obj2 = (object) array('name' => 'B');
$this->obj3 = (object) array('name' => 'C');
$this->obj4 = (object) array('name' => 'D');
$this->obj1 = (object) array('name' => 'A', 'number' => 1);
$this->obj2 = (object) array('name' => 'B', 'number' => 2);
$this->obj3 = (object) array('name' => 'C', 'number' => 3);
$this->obj4 = (object) array('name' => 'D', 'number' => 4);

$this->list = new ObjectChoiceList(
array(
Expand Down Expand Up @@ -228,4 +228,16 @@ public function testInitArrayThrowsExceptionIfToStringNotFound()
array($this->obj1, $this->obj2, $this->obj3, $this->obj4)
);
}

public function testCallableArray()
{
$this->list = new ObjectChoiceList(
array($this->obj1, $this->obj2, $this->obj3, $this->obj4),
function ($choice) {
return $choice->name . $choice->number;
}
);

$this->assertEquals(array(0 => new ChoiceView('0', 'A1'), 1 => new ChoiceView('1', 'B2'), 2 => new ChoiceView('2', 'C3'), 3 => new ChoiceView('3', 'D4')), $this->list->getRemainingViews());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.