[Form] Fixed ChoiceList::get*By*() methods to preserve order and array keys
This commit is contained in:
parent
62fbed608a
commit
79a214fa75
@ -156,11 +156,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;
|
||||
@ -180,11 +180,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;
|
||||
@ -204,11 +204,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;
|
||||
@ -228,11 +228,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;
|
||||
|
@ -97,6 +97,9 @@ interface ChoiceListInterface
|
||||
*
|
||||
* 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
|
||||
*
|
||||
@ -109,6 +112,9 @@ interface ChoiceListInterface
|
||||
*
|
||||
* 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
|
||||
*
|
||||
@ -125,6 +131,9 @@ interface ChoiceListInterface
|
||||
*
|
||||
* The index "placeholder" is internally reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
@ -140,6 +149,9 @@ interface ChoiceListInterface
|
||||
*
|
||||
* 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
|
||||
*
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
Reference in New Issue
Block a user