ChoiceListView.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\ChoiceList\View;
  11. /**
  12. * Represents a choice list in templates.
  13. *
  14. * A choice list contains choices and optionally preferred choices which are
  15. * displayed in the very beginning of the list. Both choices and preferred
  16. * choices may be grouped in {@link ChoiceGroupView} instances.
  17. *
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. */
  20. class ChoiceListView
  21. {
  22. public $choices;
  23. public $preferredChoices;
  24. /**
  25. * Creates a new choice list view.
  26. *
  27. * @param ChoiceGroupView[]|ChoiceView[] $choices The choice views
  28. * @param ChoiceGroupView[]|ChoiceView[] $preferredChoices the preferred choice views
  29. */
  30. public function __construct(array $choices = [], array $preferredChoices = [])
  31. {
  32. $this->choices = $choices;
  33. $this->preferredChoices = $preferredChoices;
  34. }
  35. /**
  36. * Returns whether a placeholder is in the choices.
  37. *
  38. * A placeholder must be the first child element, not be in a group and have an empty value.
  39. *
  40. * @return bool
  41. */
  42. public function hasPlaceholder()
  43. {
  44. if ($this->preferredChoices) {
  45. $firstChoice = reset($this->preferredChoices);
  46. return $firstChoice instanceof ChoiceView && '' === $firstChoice->value;
  47. }
  48. $firstChoice = reset($this->choices);
  49. return $firstChoice instanceof ChoiceView && '' === $firstChoice->value;
  50. }
  51. }