Skip to content

Commit

Permalink
[Form] Replace methods in ChoiceView by public properties (PHP +100ms…
Browse files Browse the repository at this point in the history
…, Twig +400ms)
  • Loading branch information
webmozart committed Jul 21, 2012
1 parent d072f35 commit 400c95b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 28 deletions.
Expand Up @@ -2,10 +2,10 @@
<?php if ($view['form']->isChoiceGroup($choice)): ?>
<optgroup label="<?php echo $view->escape($view['translator']->trans($index, array(), $translation_domain)) ?>">
<?php foreach ($choice as $nested_choice): ?>
<option value="<?php echo $view->escape($nested_choice->getValue()) ?>"<?php if ($view['form']->isChoiceSelected($form, $nested_choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($nested_choice->getLabel(), array(), $translation_domain)) ?></option>
<option value="<?php echo $view->escape($nested_choice->value) ?>"<?php if ($view['form']->isChoiceSelected($form, $nested_choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($nested_choice->label, array(), $translation_domain)) ?></option>
<?php endforeach ?>
</optgroup>
<?php else: ?>
<option value="<?php echo $view->escape($choice->getValue()) ?>"<?php if ($view['form']->isChoiceSelected($form, $choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($choice->getLabel(), array(), $translation_domain)) ?></option>
<option value="<?php echo $view->escape($choice->value) ?>"<?php if ($view['form']->isChoiceSelected($form, $choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($choice->label, array(), $translation_domain)) ?></option>
<?php endif ?>
<?php endforeach ?>
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Expand Up @@ -226,8 +226,8 @@ private function addSubForms(FormBuilderInterface $builder, array $choiceViews,
$this->addSubForms($builder, $choiceView, $options);
} else {
$choiceOpts = array(
'value' => $choiceView->getValue(),
'label' => $choiceView->getLabel(),
'value' => $choiceView->value,
'label' => $choiceView->label,
'translation_domain' => $options['translation_domain'],
);

Expand Down
24 changes: 2 additions & 22 deletions src/Symfony/Component/Form/Extension/Core/View/ChoiceView.php
Expand Up @@ -23,14 +23,14 @@ class ChoiceView
*
* @var string
*/
private $value;
public $value;

/**
* The label displayed to humans.
*
* @var string
*/
private $label;
public $label;

/**
* Creates a new ChoiceView.
Expand All @@ -43,24 +43,4 @@ public function __construct($value, $label)
$this->value = $value;
$this->label = $label;
}

/**
* Returns the choice value.
*
* @return string The view representation of the choice.
*/
public function getValue()
{
return $this->value;
}

/**
* Returns the choice label.
*
* @return string The label displayed to humans.
*/
public function getLabel()
{
return $this->label;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FormRenderer.php
Expand Up @@ -186,7 +186,7 @@ public function isChoiceGroup($choice)
public function isChoiceSelected(FormView $view, ChoiceView $choice)
{
$value = $view->vars['value'];
$choiceValue = $choice->getValue();
$choiceValue = $choice->value;

if (is_array($value)) {
return false !== array_search($choiceValue, $value, true);
Expand Down
Expand Up @@ -38,7 +38,7 @@ public function testUnknownCountryIsNotIncluded()
$choices = $view->vars['choices'];

foreach ($choices as $choice) {
if ('ZZ' === $choice->getValue()) {
if ('ZZ' === $choice->value) {
$this->fail('Should not contain choice "ZZ"');
}
}
Expand Down

0 comments on commit 400c95b

Please sign in to comment.