[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/validator/ -> sfValidatorManager.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   * sfValidatorManager provides management for request parameters and their
  14   * associated validators.
  15   *
  16   * @package    symfony
  17   * @subpackage validator
  18   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  19   * @author     Sean Kerr <skerr@mojavi.org>
  20   * @version    SVN: $Id: sfValidatorManager.class.php 3233 2007-01-11 21:01:08Z fabien $
  21   */
  22  class sfValidatorManager
  23  {
  24    protected
  25      $groups  = array(),
  26      $names   = array(),
  27      $request = null;
  28  
  29    /**
  30     * Clears this validator manager so it can be reused.
  31     */
  32    public function clear()
  33    {
  34      $this->groups = null;
  35      $this->groups = array();
  36      $this->names  = null;
  37      $this->names  = array();
  38    }
  39  
  40    /**
  41     * Executes all validators and determine the validation status.
  42     *
  43     * @return bool true, if validation completed successfully, otherwise false
  44     */
  45    public function execute()
  46    {
  47      if (sfConfig::get('sf_logging_enabled'))
  48      {
  49        sfContext::getInstance()->getLogger()->info('{sfValidator} validation execution');
  50      }
  51  
  52      $retval = true;
  53  
  54      // loop through the names and start our validation
  55      // if 1 or more groups exist, we'll have to do a second pass
  56      $pass = 1;
  57  
  58      while (true)
  59      {
  60        foreach ($this->names as $name => &$data)
  61        {
  62          if (isset($data['_is_parent']))
  63          {
  64            // this is a parent
  65            foreach ($data as $subname => &$subdata)
  66            {
  67              if ($subname == '_is_parent')
  68              {
  69                // this isn't an actual index, but more of a flag
  70                continue;
  71              }
  72  
  73              if ($subdata['validation_status'] == true && !$this->validate($subname, $subdata, $name))
  74              {
  75                // validation failed
  76                $retval = false;
  77              }
  78            }
  79          }
  80          else
  81          {
  82            // single parameter
  83            if ($data['validation_status'] == true && !$this->validate($name, $data, null))
  84            {
  85              // validation failed
  86              $retval = false;
  87            }
  88          }
  89        }
  90  
  91        if (count($this->groups) == 0 || $pass == 2)
  92        {
  93          break;
  94        }
  95  
  96        // increase our pass indicator
  97        ++$pass;
  98      }
  99  
 100      return $retval;
 101    }
 102  
 103    /**
 104     * Initializes this validator manager.
 105     *
 106     * @param sfContext A sfContext instance
 107     */
 108    public function initialize($context)
 109    {
 110      $this->request = $context->getRequest();
 111    }
 112  
 113    /**
 114     * Registers a file or parameter.
 115     *
 116     * @param string  A file or parameter name
 117     * @param bool    The required status
 118     * @param string  A required error message
 119     * @param string  A group name
 120     * @param string  A parent array
 121     */
 122    public function registerName($name, $required = true, $message = 'Required', $parent = null, $group = null, $isFile = false)
 123    {
 124      // create the entry
 125      $entry                      = array();
 126      $entry['group']             = null;
 127      $entry['is_file']           = $isFile;
 128      $entry['required']          = $required;
 129      $entry['required_msg']      = $message;
 130      $entry['validation_status'] = true;
 131      $entry['validators']        = array();
 132  
 133      if ($parent != null)
 134      {
 135        // this parameter has a parent array
 136        if (!isset($this->names[$parent]))
 137        {
 138          // create the parent array
 139          $this->names[$parent] = array('_is_parent' => true);
 140        }
 141  
 142        // register this parameter
 143        $this->names[$parent][$name] =& $entry;
 144      }
 145      else
 146      {
 147        // no parent
 148  
 149        // register this parameter
 150        $this->names[$name] =& $entry;
 151      }
 152  
 153      if ($group != null)
 154      {
 155        // set group
 156        if (!isset($this->groups[$group]))
 157        {
 158          // create our group
 159          $this->groups[$group] = array('_force' => false);
 160        }
 161  
 162        // add this file/parameter name to the group
 163        $this->groups[$group][] = $name;
 164  
 165        // add a reference back to the group array to the file/param array
 166        $entry['group'] =& $this->groups[$group];
 167      }
 168    }
 169  
 170    /**
 171     * Registers a validator for a file or parameter.
 172     *
 173     * @param string    A file or parameter name
 174     * @param Validator A validator implementation instance
 175     * @param string    A parent array name
 176     */
 177    public function registerValidator($name, $validator, $parent = null)
 178    {
 179      if ($parent != null)
 180      {
 181        // this parameter has a parent
 182        $this->names[$parent][$name]['validators'][] = $validator;
 183      }
 184      else
 185      {
 186        // no parent
 187        $this->names[$name]['validators'][] = $validator;
 188      }
 189    }
 190  
 191    /**
 192     * Validates a file or parameter.
 193     *
 194     * @param string A file or parameter name
 195     * @param array  Data associated with the file or parameter
 196     * @param string A parent name
 197     *
 198     * @return bool true, if validation completes successfully, otherwise false
 199     */
 200    protected function validate(&$name, &$data, $parent)
 201    {
 202      // get defaults
 203      $error     = null;
 204      $errorName = null;
 205      $force     = null !== $data['group'] ? $data['group']['_force'] : false;
 206      $retval    = true;
 207      $value     = null;
 208  
 209      // get our parameter value
 210      if ($parent == null)
 211      {
 212        // normal file/parameter
 213        $errorName = $name;
 214  
 215        if ($data['is_file'])
 216        {
 217          // file
 218          $value = $this->request->getFile($name);
 219        }
 220        else
 221        {
 222          // parameter
 223          $value = $this->request->getParameterHolder()->get($name);
 224        }
 225      }
 226      else
 227      {
 228        // we have a parent
 229        $errorName = $parent.'{'.$name.'}';
 230  
 231        if ($data['is_file'])
 232        {
 233          // file
 234          $parent = $this->request->getFile($parent.'['.$name.']');
 235  
 236          if ($parent != null)
 237          {
 238            $value = $parent;
 239          }
 240        }
 241        else
 242        {
 243          // parameter
 244          $parent = $this->request->getParameterHolder()->get($parent);
 245  
 246          if ($parent != null && isset($parent[$name]))
 247          {
 248            $value = $parent[$name];
 249          }
 250        }
 251      }
 252  
 253      // now for the dirty work
 254      if (
 255        ($data['is_file'] && !$value['name'])
 256        ||
 257        (!$data['is_file'] && (is_array($value) ? sfToolkit::isArrayValuesEmpty($value) : ($value == null || strlen($value) == 0)))
 258      )
 259      {
 260        if ($data['required'] || $force)
 261        {
 262          // it's empty!
 263          $error  = $data['required_msg'];
 264          $retval = false;
 265        }
 266        else
 267        {
 268          // we don't have to validate it
 269          $retval = true;
 270        }
 271      }
 272      else
 273      {
 274        // time for the fun
 275        $error = null;
 276  
 277        // get group force status
 278        if ($data['group'] != null)
 279        {
 280          // we set this because we do have a value for a parameter in this group
 281          $data['group']['_force'] = true;
 282        }
 283  
 284        if (count($data['validators']) > 0)
 285        {
 286          // loop through our validators
 287          foreach ($data['validators'] as $validator)
 288          {
 289            if (!$validator->execute($value, $error))
 290            {
 291              $retval = false;
 292  
 293              break;
 294            }
 295          }
 296        }
 297      }
 298  
 299      if (!$retval)
 300      {
 301        // set validation status
 302        $data['validation_status'] = false;
 303  
 304        // set the request error
 305        $this->request->setError($errorName, $error);
 306      }
 307  
 308      return $retval;
 309    }
 310  }


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