Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Validate
  • ValidateAbstract
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\Validate;
 14: 
 15: /**
 16:  * Factory de validateur
 17:  * @category Pry
 18:  * @package Validate
 19:  * @version 1.1.0 
 20:  * @author Olivier ROGER <oroger.fr>
 21:  */
 22: class Validate
 23: {
 24: 
 25:     /**
 26:      * Validateurs instanciés
 27:      *
 28:      * @var array
 29:      * @access private
 30:      */
 31:     private $validators;
 32: 
 33:     /**
 34:      * Constructeur
 35:      *
 36:      */
 37:     public function __construct()
 38:     {
 39:         $this->validators = array();
 40:     }
 41: 
 42:     /**
 43:      * Ajoute un nouveau validateur
 44:      *
 45:      * @param string $nom Nom du validateur
 46:      * @param array $options Option possible pour le validateur
 47:      * @param string $message Message d'erreur personalisé
 48:      * @access public
 49:      * @return Validate_Validate
 50:      */
 51:     public function addValidator($nom, $options = null, $message = '')
 52:     {
 53:         if ($this->validatorExist($nom))
 54:         {
 55:             $class = '\Pry\Validate\Validator\\' . $nom;
 56:             if (is_null($options))
 57:                 $objet = new $class();
 58:             else
 59:                 $objet = new $class($options);
 60: 
 61:             if ($message != '')
 62:             {
 63:                 $objet->setMessage($message);
 64:             }
 65:             $this->validators[] = $objet;
 66:             unset($objet);
 67:         }
 68:         else
 69:             throw new \InvalidArgumentException('Validateur inconnu');
 70:         return $this;
 71:     }
 72: 
 73:     /**
 74:      * Validation de la valeur avec les différents validateurs
 75:      *
 76:      * @param string $value
 77:      * @access public
 78:      * @return mixed Retourne true si valide, message d'erreur sinon
 79:      */
 80:     public function isValid($value)
 81:     {
 82:         foreach ($this->validators as $validator) {
 83:             if (!$validator->isValid($value))
 84:             {
 85:                 return $validator->getError();
 86:             }
 87:         }
 88:         return true;
 89:     }
 90: 
 91:     /**
 92:      * Vérifie l'existance du validateur
 93:      *
 94:      * @param string $nom
 95:      * @return boolean
 96:      */
 97:     private function validatorExist($nom)
 98:     {
 99:         return file_exists(dirname(__FILE__) . '/Validator/' . $nom . '.class.php');
100:     }
101: 
102: }
Pry API documentation generated by ApiGen 2.8.0