8000 [Form] added `CallbackChoiceLoader` and refactored ChoiceType's children by HeahDude · Pull Request #18332 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] added CallbackChoiceLoader and refactored ChoiceType's children #18332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

3.2.0
-----

* added `CallbackChoiceLoader`
* implemented `ChoiceLoaderInterface` in children of `ChoiceType`

3.1.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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\ChoiceList\Loader;

use Symfony\Component\Form\ChoiceList\ArrayChoiceList;

/**
* Loads an {@link ArrayChoiceList} instance from a callable returning an array of choices.
*
* @author Jules Pietri <jules@heahprod.com>
*/
class CallbackChoiceLoader implements ChoiceLoaderInterface
{
private $callback;

/**
* The loaded choice list.
*
* @var ArrayChoiceList
*/
private $choiceList;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add documentation about what the callable is expected to return (namely an array of choices)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HeahDude You seemed to agree, but I don't see any documentation here. Can you add a phpdoc? I would then remove the doc on the property above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot Sorry, I don't understand where you want me to add and remove things.

Back then I had changed the first phpdoc from The callable used to load the choices. to The callable used to load the array of choices. thinking that both phpdocs would be less confusing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was not very clear. Let me try again. It would be better to add a phpdoc to the constructor:

 /**
  * @param callable $callback The callable used to load the array of choices
  */
public function __construct(callable $callback)
 {
     $this->callback = $callback;
}

and remove the one on the private property:

private $callback;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, makes sense. I will do it very soon.

/**
* @param callable $callback The callable returning an array of choices
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}

/**
* {@inheritdoc}
*/
public function loadChoiceList($value = null)
{
if (null !== $this->choiceList) {
return $this->choiceList;
}

return $this->choiceList = new ArrayChoiceList(call_user_func($this->callback), $value);
}

/**
* {@inheritdoc}
*/
public function loadChoicesForValues(array $values, $value = null)
{
// Optimize
if (empty($values)) {
return array();
}

return $this->loadChoiceList($value)->getChoicesForValues($values);
}

/**
* {@inheritdoc}
*/
public function loadValuesForChoices(array $choices, $value = null)
{
// Optimize
if (empty($choices)) {
return array();
}

return $this->loadChoiceList($value)->getValuesForChoices($choices);
}
}
65 changes: 63 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/CountryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,31 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CountryType extends AbstractType
class CountryType extends AbstractType implements ChoiceLoaderInterface
{
/**
* Country loaded choice list.
*
* The choices are lazy loaded and generated from the Intl component.
*
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
*
* @var ArrayChoiceList
*/
private $choiceList;

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choices' => array_flip(Intl::getRegionBundle()->getCountryNames()),
'choice_loader' => $this,
'choice_translation_domain' => false,
));
}
Expand All @@ -43,4 +56,52 @@ public function getBlockPrefix()
{
return 'country';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this? The block prefix is "country" by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should switch in split view :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what you're trying to say. The default return value of that method is "country". This method seems superfluous to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean you might be looking at the diff "unified", and should try "split", because this method is not changed at all :)

}

/**
* {@inheritdoc}
*/
public function loadChoiceList($value = null)
{
if (null !== $this->choiceList) {
return $this->choiceList;
}

return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getRegionBundle()->getCountryNames()), $value);
}

/**
* {@inheritdoc}
*/
public function loadChoicesForValues(array $values, $value = null)
{
// Optimize
if (empty($values)) {
return array();
}

// If no callable is set, values are the same as choices
if (null === $value) {
return $values;
}

return $this->loadChoiceList($value)->getChoicesForValues($values);
}

/**
* {@inheritdoc}
*/
public function loadValuesForChoices(array $choices, $value = null)
{
// Optimize
if (empty($choices)) {
return array();
}

// If no callable is set, choices are the same as values
if (null === $value) {
return $choices;
}

return $this->loadChoiceList($value)->getValuesForChoices($choices);
}
}
65 changes: 63 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,31 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CurrencyType extends AbstractType
class CurrencyType extends AbstractType implements ChoiceLoaderInterface
{
/**
* Currency loaded choice list.
*
* The choices are lazy loaded and generated from the Intl component.
*
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
*
* @var ArrayChoiceList
*/
private $choiceList;

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choices' => array_flip(Intl::getCurrencyBundle()->getCurrencyNames()),
'choice_loader' => $this,
'choice_translation_domain' => false,
));
}
Expand All @@ -43,4 +56,52 @@ public function getBlockPrefix()
{
return 'currency';
}

/**
* {@inheritdoc}
*/
public function loadChoiceList($value = null)
{
if (null !== $this->choiceList) {
return $this->choiceList;
}

return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getCurrencyBundle()->getCurrencyNames()), $value);
}

/**
* {@inheritdoc}
*/
public function loadChoicesForValues(array $values, $value = null)
{
// Optimize
if (empty($values)) {
return array();
}

// If no callable is set, values are the same as choices
if (null === $value) {
return $values;
}

return $this->loadChoiceList($value)->getChoicesForValues($values);
}

/**
* {@inheritdoc}
*/
public function loadValuesForChoices(array $choices, $value = null)
{
// Optimize
if (empty($choices)) {
return array();
}

// If no callable is set, choices are the same as values
if (null === $value) {
return $choices;
}

return $this->loadChoiceList($value)->getValuesForChoices($choices);
}
}
65 changes: 63 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,31 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\OptionsResolver;

class LanguageType extends AbstractType
class LanguageType extends AbstractType implements ChoiceLoaderInterface
{
/**
* Language loaded choice list.
*
* The choices are lazy loaded and generated from the Intl component.
*
* {@link \Symfony\Component\Intl\Intl::getLanguageBundle()}.
*
* @var ArrayChoiceList
*/
private $choiceList;

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choices' => array_flip(Intl::getLanguageBundle()->getLanguageNames()),
'choice_loader' => $this,
'choice_translation_domain' => false,
));
}
Expand All @@ -43,4 +56,52 @@ public function getBlockPrefix()
{
return 'language';
}

/**
* {@inheritdoc}
*/
public function loadChoiceList($value = null)
{
if (null !== $this->choiceList) {
return $this->choiceList;
}

return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getLanguageBundle()->getLanguageNames()), $value);
}

/**
* {@inheritdoc}
*/
public function loadChoicesForValues(array $values, $value = null)
{
// Optimize
if (empty($values)) {
return array();
}

// If no callable is set, values are the same as choices
if (null === $value) {
return $values;
}

return $this->loadChoiceList($value)->getChoicesForValues($values);
}

/**
* {@inheritdoc}
*/
public function loadValuesForChoices(array $choices, $value = null)
{
// Optimize
if (empty($choices)) {
return array();
}

// If no callable is set, choices are the same as values
if (null === $value) {
return $choices;
}

return $this->loadChoiceList($value)->getValuesForChoices($choices);
}
}
Loading
0