web/lib/Zend/Validate/Regex.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Validate
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Regex.php 22668 2010-07-25 14:50:46Z thomas $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Validate_Abstract
       
    24  */
       
    25 require_once 'Zend/Validate/Abstract.php';
       
    26 
       
    27 /**
       
    28  * @category   Zend
       
    29  * @package    Zend_Validate
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Validate_Regex extends Zend_Validate_Abstract
       
    34 {
       
    35     const INVALID   = 'regexInvalid';
       
    36     const NOT_MATCH = 'regexNotMatch';
       
    37     const ERROROUS  = 'regexErrorous';
       
    38 
       
    39     /**
       
    40      * @var array
       
    41      */
       
    42     protected $_messageTemplates = array(
       
    43         self::INVALID   => "Invalid type given. String, integer or float expected",
       
    44         self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'",
       
    45         self::ERROROUS  => "There was an internal error while using the pattern '%pattern%'",
       
    46     );
       
    47 
       
    48     /**
       
    49      * @var array
       
    50      */
       
    51     protected $_messageVariables = array(
       
    52         'pattern' => '_pattern'
       
    53     );
       
    54 
       
    55     /**
       
    56      * Regular expression pattern
       
    57      *
       
    58      * @var string
       
    59      */
       
    60     protected $_pattern;
       
    61 
       
    62     /**
       
    63      * Sets validator options
       
    64      *
       
    65      * @param  string|Zend_Config $pattern
       
    66      * @throws Zend_Validate_Exception On missing 'pattern' parameter
       
    67      * @return void
       
    68      */
       
    69     public function __construct($pattern)
       
    70     {
       
    71         if ($pattern instanceof Zend_Config) {
       
    72             $pattern = $pattern->toArray();
       
    73         }
       
    74 
       
    75         if (is_array($pattern)) {
       
    76             if (array_key_exists('pattern', $pattern)) {
       
    77                 $pattern = $pattern['pattern'];
       
    78             } else {
       
    79                 require_once 'Zend/Validate/Exception.php';
       
    80                 throw new Zend_Validate_Exception("Missing option 'pattern'");
       
    81             }
       
    82         }
       
    83 
       
    84         $this->setPattern($pattern);
       
    85     }
       
    86 
       
    87     /**
       
    88      * Returns the pattern option
       
    89      *
       
    90      * @return string
       
    91      */
       
    92     public function getPattern()
       
    93     {
       
    94         return $this->_pattern;
       
    95     }
       
    96 
       
    97     /**
       
    98      * Sets the pattern option
       
    99      *
       
   100      * @param  string $pattern
       
   101      * @throws Zend_Validate_Exception if there is a fatal error in pattern matching
       
   102      * @return Zend_Validate_Regex Provides a fluent interface
       
   103      */
       
   104     public function setPattern($pattern)
       
   105     {
       
   106         $this->_pattern = (string) $pattern;
       
   107         $status         = @preg_match($this->_pattern, "Test");
       
   108 
       
   109         if (false === $status) {
       
   110             require_once 'Zend/Validate/Exception.php';
       
   111             throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'");
       
   112         }
       
   113 
       
   114         return $this;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Defined by Zend_Validate_Interface
       
   119      *
       
   120      * Returns true if and only if $value matches against the pattern option
       
   121      *
       
   122      * @param  string $value
       
   123      * @return boolean
       
   124      */
       
   125     public function isValid($value)
       
   126     {
       
   127         if (!is_string($value) && !is_int($value) && !is_float($value)) {
       
   128             $this->_error(self::INVALID);
       
   129             return false;
       
   130         }
       
   131 
       
   132         $this->_setValue($value);
       
   133 
       
   134         $status = @preg_match($this->_pattern, $value);
       
   135         if (false === $status) {
       
   136             $this->_error(self::ERROROUS);
       
   137             return false;
       
   138         }
       
   139 
       
   140         if (!$status) {
       
   141             $this->_error(self::NOT_MATCH);
       
   142             return false;
       
   143         }
       
   144 
       
   145         return true;
       
   146     }
       
   147 }