sfValidatorI18nChoiceCountry.class.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorI18nChoiceCountry validates than the value is a valid country.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorI18nChoiceCountry.class.php 11700 2008-09-21 10:53:44Z fabien $
  16. */
  17. class sfValidatorI18nChoiceCountry extends sfValidatorChoice
  18. {
  19. /**
  20. * Configures the current validator.
  21. *
  22. * Available options:
  23. *
  24. * * countries: An array of country codes to use (ISO 3166)
  25. *
  26. * @param array $options An array of options
  27. * @param array $messages An array of error messages
  28. *
  29. * @see sfValidatorChoice
  30. */
  31. protected function configure($options = array(), $messages = array())
  32. {
  33. parent::configure($options, $messages);
  34. // culture is deprecated
  35. $this->addOption('culture');
  36. $this->addOption('countries');
  37. // populate choices with all countries
  38. $countries = array_keys(sfCultureInfo::getInstance()->getCountries());
  39. // restrict countries to a sub-set
  40. if (isset($options['countries']))
  41. {
  42. if ($problems = array_diff($options['countries'], $countries))
  43. {
  44. throw new InvalidArgumentException(sprintf('The following countries do not exist: %s.', implode(', ', $problems)));
  45. }
  46. $countries = $options['countries'];
  47. }
  48. sort($countries);
  49. $this->setOption('choices', $countries);
  50. }
  51. }