[Form] Fixed ChoiceList::get*By*() methods to preserve order and array keys

This commit is contained in:
Bernhard Schussek 2013-05-05 23:50:06 +02:00
parent 53f292adcc
commit 30aa1de8dc
4 changed files with 135 additions and 17 deletions

View File

@ -155,11 +155,11 @@ class ChoiceList implements ChoiceListInterface
$values = $this->fixValues($values);
$choices = array();
foreach ($values as $j => $givenValue) {
foreach ($this->values as $i => $value) {
foreach ($values as $i => $givenValue) {
foreach ($this->values as $j => $value) {
if ($value === $givenValue) {
$choices[] = $this->choices[$i];
unset($values[$j]);
$choices[$i] = $this->choices[$j];
unset($values[$i]);
if (0 === count($values)) {
break 2;
@ -179,11 +179,11 @@ class ChoiceList implements ChoiceListInterface
$choices = $this->fixChoices($choices);
$values = array();
foreach ($this->choices as $i => $choice) {
foreach ($choices as $j => $givenChoice) {
foreach ($choices as $i => $givenChoice) {
foreach ($this->choices as $j => $choice) {
if ($choice === $givenChoice) {
$values[] = $this->values[$i];
unset($choices[$j]);
$values[$i] = $this->values[$j];
unset($choices[$i]);
if (0 === count($choices)) {
break 2;
@ -203,11 +203,11 @@ class ChoiceList implements ChoiceListInterface
$choices = $this->fixChoices($choices);
$indices = array();
foreach ($this->choices as $i => $choice) {
foreach ($choices as $j => $givenChoice) {
foreach ($choices as $i => $givenChoice) {
foreach ($this->choices as $j => $choice) {
if ($choice === $givenChoice) {
$indices[] = $i;
unset($choices[$j]);
$indices[$i] = $j;
unset($choices[$i]);
if (0 === count($choices)) {
break 2;
@ -227,11 +227,11 @@ class ChoiceList implements ChoiceListInterface
$values = $this->fixValues($values);
$indices = array();
foreach ($this->values as $i => $value) {
foreach ($values as $j => $givenValue) {
foreach ($values as $i => $givenValue) {
foreach ($this->values as $j => $value) {
if ($value === $givenValue) {
$indices[] = $i;
unset($values[$j]);
$indices[$i] = $j;
unset($values[$i]);
if (0 === count($values)) {
break 2;

View File

@ -95,6 +95,11 @@ interface ChoiceListInterface
/**
* Returns the choices corresponding to the given values.
*
* The choices can have any data type.
*
* The choices must be returned with the same keys and in the same order
* as the corresponding values in the given array.
*
* @param array $values An array of choice values. Not existing values in
* this array are ignored.
*
@ -105,6 +110,11 @@ interface ChoiceListInterface
/**
* Returns the values corresponding to the given choices.
*
* The values must be strings.
*
* The values must be returned with the same keys and in the same order
* as the corresponding choices in the given array.
*
* @param array $choices An array of choices. Not existing choices in this
* array are ignored.
*
@ -116,6 +126,12 @@ interface ChoiceListInterface
/**
* Returns the indices corresponding to the given choices.
*
* The indices must be positive integers or strings accepted by
* {@link FormConfigBuilder::validateName()}.
*
* The indices must be returned with the same keys and in the same order
* as the corresponding choices in the given array.
*
* @param array $choices An array of choices. Not existing choices in this
* array are ignored.
*
@ -126,6 +142,14 @@ interface ChoiceListInterface
/**
* Returns the indices corresponding to the given values.
*
* The indices must be positive integers or strings accepted by
* {@link FormConfigBuilder::validateName()}.
*
* The index "placeholder" is internally reserved.
*
* The indices must be returned with the same keys and in the same order
* as the corresponding values in the given array.
*
* @param array $values An array of choice values. Not existing values in
* this array are ignored.
*

View File

@ -125,6 +125,18 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesKeys()
{
$choices = array(5 => $this->obj2, 8 => $this->obj3);
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesOrder()
{
$choices = array($this->obj3, $this->obj2);
$this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->obj2, $this->obj3, 'foobar');
@ -138,6 +150,20 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesKeys()
{
// values and indices are always the same
$values = array(5 => '1', 8 => '2');
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesOrder()
{
// values and indices are always the same
$values = array('2', '1');
$this->assertSame(array(2, 1), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$values = array('1', '2', '5');
@ -150,7 +176,13 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesCorrectOrderingOfResult()
public function testGetChoicesForValuesPreservesKeys()
{
$values = array(5 => '1', 8 => '2');
$this->assertSame(array(5 => $this->obj2, 8 => $this->obj3), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesOrder()
{
$values = array('2', '1');
$this->assertSame(array($this->obj3, $this->obj2), $this->list->getChoicesForValues($values));
@ -168,6 +200,20 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesKeys()
{
$choices = array(5 => $this->obj2, 8 => $this->obj3);
$this->assertSame(array(5 => '1', 8 => '2'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesOrder()
{
$choices = array($this->obj3, $this->obj2);
$this->assertSame(array('2', '1'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->obj2, $this->obj3, 'foobar');

View File

@ -79,6 +79,18 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesKeys()
{
$choices = array(5 => 'b', 8 => 'c');
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesOrder()
{
$choices = array('c', 'b');
$this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
{
$choices = array('b', 'c', 'foobar');
@ -98,6 +110,18 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesKeys()
{
$values = array(5 => 'b', 8 => 'c');
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesOrder()
{
$values = array('c', 'b');
$this->assertSame(array(2, 1), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$values = array('b', 'c', '100');
@ -117,6 +141,18 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesKeys()
{
$values = array(5 => 'b', 8 => 'c');
$this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesOrder()
{
$values = array('c', 'b');
$this->assertSame(array('c', 'b'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
$values = array('b', 'c', '100');
@ -136,6 +172,18 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesKeys()
{
$choices = array(5 => 'b', 8 => 'c');
$this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesOrder()
{
$choices = array('c', 'b');
$this->assertSame(array('c', 'b'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingValues()
{
$choices = array('b', 'c', 'foobar');