[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/validator/ -> sfStringValidator.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * (c) 2004-2006 Sean Kerr.
   7   * 
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  /**
  13   * sfStringValidator allows you to apply string-related constraints to a
  14   * parameter.
  15   *
  16   * <b>Optional parameters:</b>
  17   *
  18   * # <b>insensitive</b>  - [false]              - Whether or not the value check
  19   *                                                against the array of values is
  20   *                                                case-insensitive. <b>Note:</b>
  21   *                                                When using this option, values
  22   *                                                in the values array must be
  23   *                                                entered in lower-case.
  24   * # <b>max</b>          - [none]               - Maximum string length.
  25   * # <b>max_error</b>    - [Input is too long]  - An error message to use when
  26   *                                                input is too long.
  27   * # <b>min</b>          - [none]               - Minimum string length.
  28   * # <b>min_error</b>    - [Input is too short] - An error message to use when
  29   *                                                input is too short.
  30   * # <b>values</b>       - [none]               - An array of values the input
  31   *                                                is allowed to match.
  32   * # <b>values_error</b> - [Invalid selection]  - An error message to use when
  33   *                                                input does not match a value
  34   *                                                listed in the values array.
  35   *
  36   * @package    symfony
  37   * @subpackage validator
  38   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  39   * @author     Sean Kerr <skerr@mojavi.org>
  40   * @version    SVN: $Id: sfStringValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
  41   */
  42  class sfStringValidator extends sfValidator
  43  {
  44    /**
  45     * Executes this validator.
  46     *
  47     * @param mixed A parameter value
  48     * @param error An error message reference
  49     *
  50     * @return bool true, if this validator executes successfully, otherwise false
  51     */
  52    public function execute(&$value, &$error)
  53    {
  54      $decodedValue = sfToolkit::isUTF8($value) && function_exists('utf8_decode') ? utf8_decode($value) : $value;
  55  
  56      $min = $this->getParameterHolder()->get('min');
  57      if ($min !== null && strlen(trim($decodedValue)) < $min)
  58      {
  59        // too short
  60        $error = $this->getParameterHolder()->get('min_error');
  61  
  62        return false;
  63      }
  64  
  65      $max = $this->getParameterHolder()->get('max');
  66      if ($max !== null && strlen(trim($decodedValue)) > $max)
  67      {
  68        // too long
  69        $error = $this->getParameterHolder()->get('max_error');
  70  
  71        return false;
  72      }
  73  
  74      $values = $this->getParameterHolder()->get('values');
  75      if ($values !== null)
  76      {
  77        if ($this->getParameterHolder()->get('insensitive'))
  78        {
  79          $value = strtolower($value);
  80          $found = false;
  81          foreach ($values as $avalue)
  82          {
  83            if ($value == strtolower($avalue))
  84            {
  85              $found = true;
  86              break;
  87            }
  88          }
  89          if (!$found)
  90          {
  91            // can't find a match
  92            $error = $this->getParameterHolder()->get('values_error');
  93  
  94            return false;
  95          }
  96        }
  97        else
  98        {
  99          if (!in_array($value, (array) $values))
 100          {
 101            // can't find a match
 102            $error = $this->getParameterHolder()->get('values_error');
 103  
 104            return false;
 105          }
 106        }
 107      }
 108  
 109      return true;
 110    }
 111  
 112    /**
 113     * Initializes this validator.
 114     *
 115     * @param sfContext The current application context
 116     * @param array   An associative array of initialization parameters
 117     *
 118     * @return bool true, if initialization completes successfully, otherwise false
 119     */
 120    public function initialize($context, $parameters = null)
 121    {
 122      // initialize parent
 123      parent::initialize($context);
 124  
 125      // set defaults
 126      $this->getParameterHolder()->set('insensitive',  false);
 127      $this->getParameterHolder()->set('max',          null);
 128      $this->getParameterHolder()->set('max_error',    'Input is too long');
 129      $this->getParameterHolder()->set('min',          null);
 130      $this->getParameterHolder()->set('min_error',    'Input is too short');
 131      $this->getParameterHolder()->set('values',       null);
 132      $this->getParameterHolder()->set('values_error', 'Invalid selection');
 133  
 134      $this->getParameterHolder()->add($parameters);
 135  
 136      return true;
 137    }
 138  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7