[Form] Fixed ChoiceList::get*By*() methods to preserve order and array keys
This commit is contained in:
parent
53f292adcc
commit
30aa1de8dc
@ -155,11 +155,11 @@ class ChoiceList implements ChoiceListInterface
|
|||||||
$values = $this->fixValues($values);
|
$values = $this->fixValues($values);
|
||||||
$choices = array();
|
$choices = array();
|
||||||
|
|
||||||
foreach ($values as $j => $givenValue) {
|
foreach ($values as $i => $givenValue) {
|
||||||
foreach ($this->values as $i => $value) {
|
foreach ($this->values as $j => $value) {
|
||||||
if ($value === $givenValue) {
|
if ($value === $givenValue) {
|
||||||
$choices[] = $this->choices[$i];
|
$choices[$i] = $this->choices[$j];
|
||||||
unset($values[$j]);
|
unset($values[$i]);
|
||||||
|
|
||||||
if (0 === count($values)) {
|
if (0 === count($values)) {
|
||||||
break 2;
|
break 2;
|
||||||
@ -179,11 +179,11 @@ class ChoiceList implements ChoiceListInterface
|
|||||||
$choices = $this->fixChoices($choices);
|
$choices = $this->fixChoices($choices);
|
||||||
$values = array();
|
$values = array();
|
||||||
|
|
||||||
foreach ($this->choices as $i => $choice) {
|
foreach ($choices as $i => $givenChoice) {
|
||||||
foreach ($choices as $j => $givenChoice) {
|
foreach ($this->choices as $j => $choice) {
|
||||||
if ($choice === $givenChoice) {
|
if ($choice === $givenChoice) {
|
||||||
$values[] = $this->values[$i];
|
$values[$i] = $this->values[$j];
|
||||||
unset($choices[$j]);
|
unset($choices[$i]);
|
||||||
|
|
||||||
if (0 === count($choices)) {
|
if (0 === count($choices)) {
|
||||||
break 2;
|
break 2;
|
||||||
@ -203,11 +203,11 @@ class ChoiceList implements ChoiceListInterface
|
|||||||
$choices = $this->fixChoices($choices);
|
$choices = $this->fixChoices($choices);
|
||||||
$indices = array();
|
$indices = array();
|
||||||
|
|
||||||
foreach ($this->choices as $i => $choice) {
|
foreach ($choices as $i => $givenChoice) {
|
||||||
foreach ($choices as $j => $givenChoice) {
|
foreach ($this->choices as $j => $choice) {
|
||||||
if ($choice === $givenChoice) {
|
if ($choice === $givenChoice) {
|
||||||
$indices[] = $i;
|
$indices[$i] = $j;
|
||||||
unset($choices[$j]);
|
unset($choices[$i]);
|
||||||
|
|
||||||
if (0 === count($choices)) {
|
if (0 === count($choices)) {
|
||||||
break 2;
|
break 2;
|
||||||
@ -227,11 +227,11 @@ class ChoiceList implements ChoiceListInterface
|
|||||||
$values = $this->fixValues($values);
|
$values = $this->fixValues($values);
|
||||||
$indices = array();
|
$indices = array();
|
||||||
|
|
||||||
foreach ($this->values as $i => $value) {
|
foreach ($values as $i => $givenValue) {
|
||||||
foreach ($values as $j => $givenValue) {
|
foreach ($this->values as $j => $value) {
|
||||||
if ($value === $givenValue) {
|
if ($value === $givenValue) {
|
||||||
$indices[] = $i;
|
$indices[$i] = $j;
|
||||||
unset($values[$j]);
|
unset($values[$i]);
|
||||||
|
|
||||||
if (0 === count($values)) {
|
if (0 === count($values)) {
|
||||||
break 2;
|
break 2;
|
||||||
|
@ -95,6 +95,11 @@ interface ChoiceListInterface
|
|||||||
/**
|
/**
|
||||||
* Returns the choices corresponding to the given values.
|
* 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
|
* @param array $values An array of choice values. Not existing values in
|
||||||
* this array are ignored.
|
* this array are ignored.
|
||||||
*
|
*
|
||||||
@ -105,6 +110,11 @@ interface ChoiceListInterface
|
|||||||
/**
|
/**
|
||||||
* Returns the values corresponding to the given choices.
|
* 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
|
* @param array $choices An array of choices. Not existing choices in this
|
||||||
* array are ignored.
|
* array are ignored.
|
||||||
*
|
*
|
||||||
@ -116,6 +126,12 @@ interface ChoiceListInterface
|
|||||||
/**
|
/**
|
||||||
* Returns the indices corresponding to the given choices.
|
* 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
|
* @param array $choices An array of choices. Not existing choices in this
|
||||||
* array are ignored.
|
* array are ignored.
|
||||||
*
|
*
|
||||||
@ -126,6 +142,14 @@ interface ChoiceListInterface
|
|||||||
/**
|
/**
|
||||||
* Returns the indices corresponding to the given values.
|
* 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
|
* @param array $values An array of choice values. Not existing values in
|
||||||
* this array are ignored.
|
* this array are ignored.
|
||||||
*
|
*
|
||||||
|
@ -125,6 +125,18 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
|
$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()
|
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
|
||||||
{
|
{
|
||||||
$choices = array($this->obj2, $this->obj3, 'foobar');
|
$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));
|
$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()
|
public function testGetIndicesForValuesIgnoresNonExistingValues()
|
||||||
{
|
{
|
||||||
$values = array('1', '2', '5');
|
$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));
|
$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');
|
$values = array('2', '1');
|
||||||
$this->assertSame(array($this->obj3, $this->obj2), $this->list->getChoicesForValues($values));
|
$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));
|
$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()
|
public function testGetValuesForChoicesIgnoresNonExistingChoices()
|
||||||
{
|
{
|
||||||
$choices = array($this->obj2, $this->obj3, 'foobar');
|
$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));
|
$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()
|
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
|
||||||
{
|
{
|
||||||
$choices = array('b', 'c', 'foobar');
|
$choices = array('b', 'c', 'foobar');
|
||||||
@ -98,6 +110,18 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
|
$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()
|
public function testGetIndicesForValuesIgnoresNonExistingValues()
|
||||||
{
|
{
|
||||||
$values = array('b', 'c', '100');
|
$values = array('b', 'c', '100');
|
||||||
@ -117,6 +141,18 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
|
$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()
|
public function testGetChoicesForValuesIgnoresNonExistingValues()
|
||||||
{
|
{
|
||||||
$values = array('b', 'c', '100');
|
$values = array('b', 'c', '100');
|
||||||
@ -136,6 +172,18 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
|
$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()
|
public function testGetValuesForChoicesIgnoresNonExistingValues()
|
||||||
{
|
{
|
||||||
$choices = array('b', 'c', 'foobar');
|
$choices = array('b', 'c', 'foobar');
|
||||||
|
Reference in New Issue
Block a user