[Form] Added class LazyChoiceList

This commit is contained in:
Bernhard Schussek 2012-01-30 10:56:58 +01:00
parent 916597eb29
commit a676598d74
2 changed files with 330 additions and 0 deletions

View File

@ -0,0 +1,214 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\Core\ChoiceList;
use Symfony\Component\Form\Exception\FormException;
/**
* A choice list that is loaded lazily
*
* This list loads itself as soon as any of the getters is accessed for the
* first time. You should implement loadChoiceList() in your child classes,
* which should return a ChoiceListInterface instance.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class LazyChoiceList implements ChoiceListInterface
{
/**
* The loaded choice list
*
* @var ChoiceListInterface
*/
private $choiceList;
/**
* Returns the list of choices
*
* @return array The choices with their indices as keys.
*/
public function getChoices()
{
if (!$this->choiceList) {
$this->load();
}
return $this->choiceList->getChoices();
}
/**
* Returns the values for the choices
*
* @return array The values with the corresponding choice indices as keys.
*/
public function getValues()
{
if (!$this->choiceList) {
$this->load();
}
return $this->choiceList->getValues();
}
/**
* Returns the choice views of the preferred choices as nested array with
* the choice groups as top-level keys.
*
* Example:
*
* <source>
* array(
* 'Group 1' => array(
* 10 => ChoiceView object,
* 20 => ChoiceView object,
* ),
* 'Group 2' => array(
* 30 => ChoiceView object,
* ),
* )
* </source>
*
* @return array A nested array containing the views with the corresponding
* choice indices as keys on the lowest levels and the choice
* group names in the keys of the higher levels.
*/
public function getPreferredViews()
{
if (!$this->choiceList) {
$this->load();
}
return $this->choiceList->getPreferredViews();
}
/**
* Returns the choice views of the choices that are not preferred as nested
* array with the choice groups as top-level keys.
*
* Example:
*
* <source>
* array(
* 'Group 1' => array(
* 10 => ChoiceView object,
* 20 => ChoiceView object,
* ),
* 'Group 2' => array(
* 30 => ChoiceView object,
* ),
* )
* </source>
*
* @return array A nested array containing the views with the corresponding
* choice indices as keys on the lowest levels and the choice
* group names in the keys of the higher levels.
*
* @see getPreferredValues
*/
public function getRemainingViews()
{
if (!$this->choiceList) {
$this->load();
}
return $this->choiceList->getRemainingViews();
}
/**
* Returns the choices corresponding to the given values.
*
* @param array $values An array of choice values. Not existing values in
* this array are ignored.
*
* @return array An array of choices with ascending, 0-based numeric keys
*/
public function getChoicesForValues(array $values)
{
if (!$this->choiceList) {
$this->load();
}
return $this->choiceList->getChoicesForValues($values);
}
/**
* Returns the values corresponding to the given choices.
*
* @param array $choices An array of choices. Not existing choices in this
* array are ignored.
*
* @return array An array of choice values with ascending, 0-based numeric
* keys
*/
public function getValuesForChoices(array $choices)
{
if (!$this->choiceList) {
$this->load();
}
return $this->choiceList->getValuesForChoices($choices);
}
/**
* Returns the indices corresponding to the given choices.
*
* @param array $choices An array of choices. Not existing choices in this
* array are ignored.
*
* @return array An array of indices with ascending, 0-based numeric keys
*/
public function getIndicesForChoices(array $choices)
{
if (!$this->choiceList) {
$this->load();
}
return $this->choiceList->getIndicesForChoices($choices);
}
/**
* Returns the indices corresponding to the given values.
*
* @param array $values An array of choice values. Not existing values in
* this array are ignored.
*
* @return array An array of indices with ascending, 0-based numeric keys
*/
public function getIndicesForValues(array $values)
{
if (!$this->choiceList) {
$this->load();
}
return $this->choiceList->getIndicesForValues($values);
}
/**
* Loads the choice list
*
* Should be implemented by child classes.
*
* @return ChoiceListInterface The loaded choice list
*/
abstract protected function loadChoiceList();
private function load()
{
$choiceList = $this->loadChoiceList();
if (!$choiceList instanceof ChoiceListInterface) {
throw new FormException('loadChoiceList() should return a ChoiceListInterface instance. Got ' . gettype($choiceList));
}
$this->choiceList = $choiceList;
}
}

View File

@ -0,0 +1,116 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form\Extension\Core\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class LazyChoiceListTest extends \PHPUnit_Framework_TestCase
{
private $list;
protected function setUp()
{
parent::setUp();
$this->list = new LazyChoiceListTest_Impl(new SimpleChoiceList(array(
'a' => 'A',
'b' => 'B',
'c' => 'C',
), array('b')));
}
protected function tearDown()
{
parent::tearDown();
$this->list = null;
}
public function testGetChoices()
{
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getChoices());
}
public function testGetValues()
{
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getValues());
}
public function testGetPreferredViews()
{
$this->assertEquals(array(1 => new ChoiceView('b', 'B')), $this->list->getPreferredViews());
}
public function testGetRemainingViews()
{
$this->assertEquals(array(0 => new ChoiceView('a', 'A'), 2 => new ChoiceView('c', 'C')), $this->list->getRemainingViews());
}
public function testGetIndicesForChoices()
{
$choices = array('b', 'c');
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForValues()
{
$values = array('b', 'c');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetChoicesForValues()
{
$values = array('b', 'c');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
}
public function testGetValuesForChoices()
{
$choices = array('b', 'c');
$this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
}
/**
* @expectedException Symfony\Component\Form\Exception\FormException
*/
public function testLoadChoiceListShouldReturnChoiceList()
{
$list = new LazyChoiceListTest_InvalidImpl();
$list->getChoices();
}
}
class LazyChoiceListTest_Impl extends LazyChoiceList
{
private $choiceList;
public function __construct($choiceList)
{
$this->choiceList = $choiceList;
}
protected function loadChoiceList()
{
return $this->choiceList;
}
}
class LazyChoiceListTest_InvalidImpl extends LazyChoiceList
{
protected function loadChoiceList()
{
return new \stdClass();
}
}