public function testCreateChoiceListWithGroupedChoices() { $choiceList = new ArrayChoiceList(array('Group 1' => array('A' => 'a', 'B' => 'b'), 'Group 2' => array('C' => 'c', 'D' => 'd'))); $this->assertSame(array('0', '1', '2', '3'), $choiceList->getValues()); $this->assertSame(array('Group 1' => array('A' => '0', 'B' => '1'), 'Group 2' => array('C' => '2', 'D' => '3')), $choiceList->getStructuredValues()); $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'), $choiceList->getChoices()); $this->assertSame(array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'), $choiceList->getOriginalKeys()); $this->assertSame(array(1 => 'a', 2 => 'b'), $choiceList->getChoicesForValues(array(1 => '0', 2 => '1'))); $this->assertSame(array(1 => '0', 2 => '1'), $choiceList->getValuesForChoices(array(1 => 'a', 2 => 'b'))); }
public function testCreateChoiceListWithValueCallback() { $callback = function ($choice) { return ':' . $choice; }; $choiceList = new ArrayChoiceList(array(2 => 'foo', 7 => 'bar', 10 => 'baz'), $callback); $this->assertSame(array(2 => ':foo', 7 => ':bar', 10 => ':baz'), $choiceList->getValues()); $this->assertSame(array(1 => 'foo', 2 => 'baz'), $choiceList->getChoicesForValues(array(1 => ':foo', 2 => ':baz'))); $this->assertSame(array(1 => ':foo', 2 => ':baz'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => 'baz'))); }
public function testGetChoicesForValuesWithContainingEmptyStringAndBooleans() { $choiceList = new ArrayChoiceList(array('Empty String' => '', 'True' => true, 'False' => false)); $this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array(''))); $this->assertSame(array(0 => true), $choiceList->getChoicesForValues(array('1'))); $this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0'))); }
/** * {@inheritdoc} */ public function getChoicesForValues(array $values) { if ($this->useChoicesAsValues) { $values = array_map('strval', $values); // If the values are identical to the choices, so we can just return // them to improve performance a little bit return array_map(array(__CLASS__, 'toArrayKey'), array_intersect($values, array_keys($this->choices))); } return parent::getChoicesForValues($values); }
public function testGetChoicesForValuesWithContainingNull() { $choiceList = new ArrayChoiceList(array('Null' => null)); $this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('0'))); }
public function testGetChoicesForValuesWithContainingEmptyStringAndFloats() { $choiceList = new ArrayChoiceList(array('Empty String' => '', '1/3' => 0.3, '1/2' => 0.5)); $this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array(''))); $this->assertSame(array(0 => 0.3), $choiceList->getChoicesForValues(array('0.3'))); $this->assertSame(array(0 => 0.5), $choiceList->getChoicesForValues(array('0.5'))); }