sfValidatorSchemaFilter.class.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * sfValidatorSchemaFilter executes non schema validator on a schema input value.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorSchemaFilter.class.php 11003 2008-08-20 16:39:20Z fabien $
  16. */
  17. class sfValidatorSchemaFilter extends sfValidatorSchema
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * @param string $field The field name
  23. * @param sfValidatorBase $validator The validator
  24. * @param array $options An array of options
  25. * @param array $messages An array of error messages
  26. *
  27. * @see sfValidatorBase
  28. */
  29. public function __construct($field, sfValidatorBase $validator, $options = array(), $messages = array())
  30. {
  31. $this->addOption('field', $field);
  32. $this->addOption('validator', $validator);
  33. parent::__construct(null, $options, $messages);
  34. }
  35. /**
  36. * @see sfValidatorBase
  37. */
  38. protected function doClean($values)
  39. {
  40. if (is_null($values))
  41. {
  42. $values = array();
  43. }
  44. if (!is_array($values))
  45. {
  46. throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
  47. }
  48. $value = isset($values[$this->getOption('field')]) ? $values[$this->getOption('field')] : null;
  49. try
  50. {
  51. $values[$this->getOption('field')] = $this->getOption('validator')->clean($value);
  52. }
  53. catch (sfValidatorError $error)
  54. {
  55. throw new sfValidatorErrorSchema($this, array($this->getOption('field') => $error));
  56. }
  57. return $values;
  58. }
  59. /**
  60. * @see sfValidatorBase
  61. */
  62. public function asString($indent = 0)
  63. {
  64. return sprintf('%s%s:%s', str_repeat(' ', $indent), $this->getOption('field'), $this->getOption('validator')->asString(0));
  65. }
  66. }