8000 [Form] added `CallbackChoiceLoader` · symfony/symfony@afd7bf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit afd7bf8

Browse files
committed
[Form] added CallbackChoiceLoader
1 parent e707760 commit afd7bf8

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.2.0
5+
-----
6+
7+
* added `CallbackChoiceLoader`
8+
49
3.1.0
510
-----
611

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\ChoiceList\Loader;
13+
14+
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
15+
16+
/**
17+
* Loads an {@link ArrayChoiceList} instance from a callable returning an array of choices.
18+
*
19+
* @author Jules Pietri <jules@heahprod.com>
20+
*/
21+
class CallbackChoiceLoader implements ChoiceLoaderInterface
22+
{
23+
private $callback;
24+
25+
/**
26+
* The loaded choice list.
27+
*
28+
* @var ArrayChoiceList
29+
*/
30+
private $choiceList;
31+
32+
/**
33+
* @param callable $callback The callable returning an array of choices
34+
*/
35+
public function __construct(callable $callback)
36+
{
37+
$this->callback = $callback;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function loadChoiceList($value 8000 = null)
44+
{
45+
if (null !== $this->choiceList) {
46+
return $this->choiceList;
47+
}
48+
49+
return $this->choiceList = new ArrayChoiceList(call_user_func($this->callback), $value);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function loadChoicesForValues(array $values, $value = null)
56+
{
57+
// Optimize
58+
if (empty($values)) {
59+
return array();
60+
}
61+
62+
return $this->loadChoiceList($value)->getChoicesForValues($values);
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function loadValuesForChoices(array $choices, $value = null)
69+
{
70+
// Optimize
71+
if (empty($choices)) {
72+
return array();
73+
}
74+
75+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
76+
}
77+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\ChoiceList\Loader;
13+
14+
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
15+
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
16+
17+
/**
18+
* @author Jules Pietri <jules@heahprod.com>
19+
*/
20+
class CallbackChoiceLoaderTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var \Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader
24+
*/
25+
static private $loader;
26+
27+
/**
28+
* @var callable
29+
*/
30+
static private $value;
31+
32+
/**
33+
* @var array
34+
*/
35+
static private $choices;
36+
37+
/**
38+
* @var string[]
39+
*/
40+
static private $choiceValues;
41+
42+
/**
43+
* @var \Symfony\Component\Form\ChoiceList\LazyChoiceList
44+
*/
45+
static private $lazyChoiceList;
46+
47+
static public function setUpBeforeClass()
48+
{
49+
self::$loader = new CallbackChoiceLoader(function() {
50+
return self::$choices;
51+
});
52+
self::$value = function ($choice) {
53+
return isset($choice->value) ? $choice->value : null;
54+
};
55+
self::$choices = array(
56+
(object) array('value' => 'choice_one'),
57+
(object) array('value' => 'choice_two'),
58+
);
59+
self::$choiceValues = array('choice_one', 'choice_two');
60+
self::$lazyChoiceList = new LazyChoiceList(self::$loader, self::$value);
61+
}
62+
63+
public function testLoadChoiceList()
64+
{
65+
$this->assertInstanceOf('\Symfony\Component\Form\ChoiceList\ChoiceListInterface', self::$loader->loadChoiceList(self::$value));
66+
}
67+
68+
public function testLoadChoiceListOnlyOnce()
69+
{
70+
$loadedChoiceList = self::$loader->loadChoiceList(self::$value);
71+
72+
$this->assertSame($loadedChoiceList, self::$loader->loadChoiceList(self::$value));
73+
}
74+
75+
public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
76+
{
77+
$this->assertSame(
78+
self::$loader->loadChoicesForValues(self::$choiceValues, self::$value),
79+
self::$lazyChoiceList->getChoicesForValues(self::$choiceValues),
80+
'Choice list should not be reloaded.'
81+
);
82+
}
83+
84+
public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
85+
{
86+
$this->assertSame(
87+
self::$loader->loadValuesForChoices(self::$choices, self::$value),
88+
self::$lazyChoiceList->getValuesForChoices(self::$choices),
89+
'Choice list should not be reloaded.'
90+
);
91+
}
92+
93+
static public function tearDownAfterClass()
94+
{
95+
self::$loader = null;
96+
self::$value = null;
97+
self::$choices = array();
98+
self::$choiceValues = array();
99+
self::$lazyChoiceList = null;
100+
}
101+
}

0 commit comments

Comments
 (0)
0