Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8539aa

Browse files
committed
[Form] implemented ChoiceLoaderInterface in children of ChoiceType
1 parent 3de5c7e commit d8539aa

File tree

6 files changed

+328
-38
lines changed

6 files changed

+328
-38
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* deprecated support for data objects that implements both `Traversable` and
99
`ArrayAccess` in `ResizeFormListener::preSubmit` method
1010
* added `CallbackChoiceLoader`
11+
* implemented `ChoiceLoaderInterface` in children of `ChoiceType`
1112

1213
3.0.0
1314
-----

src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
16+
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1517
use Symfony\Component\Intl\Intl;
1618
use Symfony\Component\OptionsResolver\OptionsResolver;
1719

18-
class CountryType extends AbstractType
20+
class CountryType extends AbstractType implements ChoiceLoaderInterface
1921
{
22+
/**
23+
* Country loaded choice list.
24+
*
25+
* The choices are lazy loaded and generated from the Intl component.
26+
*
27+
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
28+
*
29+
* @var ArrayChoiceList
30+
*/
31+
private $choiceList;
32+
2033
/**
2134
* {@inheritdoc}
2235
*/
2336
public function configureOptions(OptionsResolver $resolver)
2437
{
2538
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getRegionBundle()->getCountryNames()),
39+
'choice_loader' => $this,
2740
'choice_translation_domain' => false,
2841
));
2942
}
@@ -43,4 +56,52 @@ public function getBlockPrefix()
4356
{
4457
return 'country';
4558
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function loadChoiceList($value = null)
64+
{
65+
if (null !== $this->choiceList) {
66+
return $this->choiceList;
67+
}
68+
69+
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getRegionBundle()->getCountryNames()), $value);
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function loadChoicesForValues(array $values, $value = null)
76+
{
77+
// Optimize
78+
if (empty($values)) {
79+
return array();
80+
}
81+
82+
// If no callable is set, values are the same as choices
83+
if (null === $value) {
84+
return $values;
85+
}
86+
87+
return $this->loadChoiceList($value)->getChoicesForValues($values);
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function loadValuesForChoices(array $choices, $value = null)
94+
{
95+
// Optimize
96+
if (empty($choices)) {
97+
return array();
98+
}
99+
100+
// If no callable is set, choices are the same as values
101+
if (null === $value) {
102+
return $choices;
103+
}
104+
105+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
106+
}
46107
}

src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
16+
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1517
use Symfony\Component\Intl\Intl;
1618
use Symfony\Component\OptionsResolver\OptionsResolver;
1719

18-
class CurrencyType extends AbstractType
20+
class CurrencyType extends AbstractType implements ChoiceLoaderInterface
1921
{
22+
/**
23+
* Currency loaded choice list.
24+
*
25+
* The choices are lazy loaded and generated from the Intl component.
26+
*
27+
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
28+
*
29+
* @var ArrayChoiceList
30+
*/
31+
private $choiceList;
32+
2033
/**
2134
* {@inheritdoc}
2235
*/
2336
public function configureOptions(OptionsResolver $resolver)
2437
{
2538
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getCurrencyBundle()->getCurrencyNames()),
39+
'choice_loader' => $this,
2740
'choice_translation_domain' => false,
2841
));
2942
}
@@ -43,4 +56,52 @@ public function getBlockPrefix()
4356
{
4457
return 'currency';
4558
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function loadChoiceList($value = null)
64+
{
65+
if (null !== $this->choiceList) {
66+
return $this->choiceList;
67+
}
68+
69+
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getCurrencyBundle()->getCurrencyNames()), $value);
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function loadChoicesForValues(array $values, $value = null)
76+
{
77+
// Optimize
78+
if (empty($values)) {
79+
return array();
80+
}
81+
82+
// If no callable is set, values are the same as choices
83+
if (null === $value) {
84+
return $values;
85+
}
86+
87+
return $this->loadChoiceList($value)->getChoicesForValues($values);
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function loadValuesForChoices(array $choices, $value = null)
94+
{
95+
// Optimize
96+
if (empty($choices)) {
97+
return array();
98+
}
99+
100+
// If no callable is set, choices are the same as values
101+
if (null === $value) {
102+
return $choices;
103+
}
104+
105+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
106+
}
46107
}

src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
16+
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1517
use Symfony\Component\Intl\Intl;
1618
use Symfony\Component\OptionsResolver\OptionsResolver;
1719

18-
class LanguageType extends AbstractType
20+
class LanguageType extends AbstractType implements ChoiceLoaderInterface
1921
{
22+
/**
23+
* Language loaded choice list.
24+
*
25+
* The choices are lazy loaded and generated from the Intl component.
26+
*
27+
* {@link \Symfony\Component\Intl\Intl::getLanguageBundle()}.
28+
*
29+
* @var ArrayChoiceList
30+
*/
31+
private $choiceList;
32+
2033
/**
2134
* {@inheritdoc}
2235
*/
2336
public function configureOptions(OptionsResolver $resolver)
2437
{
2538
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getLanguageBundle()->getLanguageNames()),
39+
'choice_loader' => $this,
2740
'choice_translation_domain' => false,
2841
));
2942
}
@@ -43,4 +56,52 @@ public function getBlockPrefix()
4356
{
4457
return 'language';
4558
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function loadChoiceList($value = null)
64+
{
65+
if (null !== $this->choiceList) {
66+
return $this->choiceList;
67+
}
68+
69+
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getLanguageBundle()->getLanguageNames()), $value);
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function loadChoicesForValues(array $values, $value = null)
76+
{
77+
// Optimize
78+
if (empty($values)) {
79+
return array();
80+
}
81+
82+
// If no callable is set, values are the same as choices
83+
if (null === $value) {
84+
return $values;
85+
}
86+
87+
return $this->loadChoiceList($value)->getChoicesForValues($values);
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function loadValuesForChoices(array $choices, $value = null)
94+
{
95+
// Optimize
96+
if (empty($choices)) {
97+
return array();
98+
}
99+
100+
// If no callable is set, choices are the same as values
101+
if (null === $value) {
102+
return $choices;
103+
}
104+
105+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
106+
}
46107
}

src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
16+
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1517
use Symfony\Component\Intl\Intl;
1618
use Symfony\Component\OptionsResolver\OptionsResolver;
1719

18-
class LocaleType extends AbstractType
20+
class LocaleType extends AbstractType implements ChoiceLoaderInterface
1921
{
22+
/**
23+
* Locale loaded choice list.
24+
*
25+
* The choices are lazy loaded and generated from the Intl component.
26+
*
27+
* {@link \Symfony\Component\Intl\Intl::getLocaleBundle()}.
28+
*
29+
* @var ArrayChoiceList
30+
*/
31+
private $choiceList;
32+
2033
/**
2134
* {@inheritdoc}
2235
*/
2336
public function configureOptions(OptionsResolver $resolver)
2437
{
2538
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getLocaleBundle()->getLocaleNames()),
39+
'choice_loader' => $this,
2740
'choice_translation_domain' => false,
2841
));
2942
}
@@ -43,4 +56,52 @@ public function getBlockPrefix()
4356
{
4457
return 'locale';
4558
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function loadChoiceList($value = null)
64+
{
65+
if (null !== $this->choiceList) {
66+
return $this->choiceList;
67+
}
68+
69+
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getLocaleBundle()->getLocaleNames()), $value);
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function loadChoicesForValues(array $values, $value = null)
76+
{
77+
// Optimize
78+
if (empty($values)) {
79+
return array();
80+
}
81+
82+
// If no callable is set, values are the same as choices
83+
if (null === $value) {
84+
return $values;
85+
}
86+
87+
return $this->loadChoiceList($value)->getChoicesForValues($values);
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function loadValuesForChoices(array $choices, $value = null)
94+
{
95+
// Optimize
96+
if (empty($choices)) {
97+
return array();
98+
}
99+
100+
// If no callable is set, choices are the same as values
101+
if (null === $value) {
102+
return $choices;
103+
}
104+
105+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
106+
}
46107
}

0 commit comments

Comments
 (0)