Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitafd7bf8

Browse files
committed
[Form] addedCallbackChoiceLoader
1 parente707760 commitafd7bf8

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+
namespaceSymfony\Component\Form\ChoiceList\Loader;
13+
14+
useSymfony\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 CallbackChoiceLoaderimplements 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+
publicfunction__construct(callable$callback)
36+
{
37+
$this->callback =$callback;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
publicfunctionloadChoiceList($value =null)
44+
{
45+
if (null !==$this->choiceList) {
46+
return$this->choiceList;
47+
}
48+
49+
return$this->choiceList =newArrayChoiceList(call_user_func($this->callback),$value);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
publicfunctionloadChoicesForValues(array$values,$value =null)
56+
{
57+
// Optimize
58+
if (empty($values)) {
59+
returnarray();
60+
}
61+
62+
return$this->loadChoiceList($value)->getChoicesForValues($values);
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
publicfunctionloadValuesForChoices(array$choices,$value =null)
69+
{
70+
// Optimize
71+
if (empty($choices)) {
72+
returnarray();
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+
namespaceSymfony\Component\Form\Tests\ChoiceList\Loader;
13+
14+
useSymfony\Component\Form\ChoiceList\LazyChoiceList;
15+
useSymfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
16+
17+
/**
18+
* @author Jules Pietri <jules@heahprod.com>
19+
*/
20+
class CallbackChoiceLoaderTestextends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var \Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader
24+
*/
25+
staticprivate$loader;
26+
27+
/**
28+
* @var callable
29+
*/
30+
staticprivate$value;
31+
32+
/**
33+
* @var array
34+
*/
35+
staticprivate$choices;
36+
37+
/**
38+
* @var string[]
39+
*/
40+
staticprivate$choiceValues;
41+
42+
/**
43+
* @var \Symfony\Component\Form\ChoiceList\LazyChoiceList
44+
*/
45+
staticprivate$lazyChoiceList;
46+
47+
staticpublicfunctionsetUpBeforeClass()
48+
{
49+
self::$loader =newCallbackChoiceLoader(function() {
50+
returnself::$choices;
51+
});
52+
self::$value =function ($choice) {
53+
returnisset($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 =newLazyChoiceList(self::$loader,self::$value);
61+
}
62+
63+
publicfunctiontestLoadChoiceList()
64+
{
65+
$this->assertInstanceOf('\Symfony\Component\Form\ChoiceList\ChoiceListInterface',self::$loader->loadChoiceList(self::$value));
66+
}
67+
68+
publicfunctiontestLoadChoiceListOnlyOnce()
69+
{
70+
$loadedChoiceList =self::$loader->loadChoiceList(self::$value);
71+
72+
$this->assertSame($loadedChoiceList,self::$loader->loadChoiceList(self::$value));
73+
}
74+
75+
publicfunctiontestLoadChoicesForValuesLoadsChoiceListOnFirstCall()
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+
publicfunctiontestLoadValuesForChoicesLoadsChoiceListOnFirstCall()
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+
staticpublicfunctiontearDownAfterClass()
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)

[8]ページ先頭

©2009-2025 Movatter.jp