File indexing completed on 2024-12-22 05:37:12

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Abstract
0024  */
0025 // require_once 'Zend/Validate/Abstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate_Regex extends Zend_Validate_Abstract
0034 {
0035     const INVALID   = 'regexInvalid';
0036     const NOT_MATCH = 'regexNotMatch';
0037     const ERROROUS  = 'regexErrorous';
0038 
0039     /**
0040      * @var array
0041      */
0042     protected $_messageTemplates = array(
0043         self::INVALID   => "Invalid type given. String, integer or float expected",
0044         self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'",
0045         self::ERROROUS  => "There was an internal error while using the pattern '%pattern%'",
0046     );
0047 
0048     /**
0049      * @var array
0050      */
0051     protected $_messageVariables = array(
0052         'pattern' => '_pattern'
0053     );
0054 
0055     /**
0056      * Regular expression pattern
0057      *
0058      * @var string
0059      */
0060     protected $_pattern;
0061 
0062     /**
0063      * Sets validator options
0064      *
0065      * @param  string|Zend_Config $pattern
0066      * @throws Zend_Validate_Exception On missing 'pattern' parameter
0067      */
0068     public function __construct($pattern)
0069     {
0070         if ($pattern instanceof Zend_Config) {
0071             $pattern = $pattern->toArray();
0072         }
0073 
0074         if (is_array($pattern)) {
0075             if (array_key_exists('pattern', $pattern)) {
0076                 $pattern = $pattern['pattern'];
0077             } else {
0078                 // require_once 'Zend/Validate/Exception.php';
0079                 throw new Zend_Validate_Exception("Missing option 'pattern'");
0080             }
0081         }
0082 
0083         $this->setPattern($pattern);
0084     }
0085 
0086     /**
0087      * Returns the pattern option
0088      *
0089      * @return string
0090      */
0091     public function getPattern()
0092     {
0093         return $this->_pattern;
0094     }
0095 
0096     /**
0097      * Sets the pattern option
0098      *
0099      * @param  string $pattern
0100      * @throws Zend_Validate_Exception if there is a fatal error in pattern matching
0101      * @return Zend_Validate_Regex Provides a fluent interface
0102      */
0103     public function setPattern($pattern)
0104     {
0105         $this->_pattern = (string) $pattern;
0106         $status         = @preg_match($this->_pattern, "Test");
0107 
0108         if (false === $status) {
0109             // require_once 'Zend/Validate/Exception.php';
0110             throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'");
0111         }
0112 
0113         return $this;
0114     }
0115 
0116     /**
0117      * Defined by Zend_Validate_Interface
0118      *
0119      * Returns true if and only if $value matches against the pattern option
0120      *
0121      * @param  string $value
0122      * @return boolean
0123      */
0124     public function isValid($value)
0125     {
0126         if (!is_string($value) && !is_int($value) && !is_float($value)) {
0127             $this->_error(self::INVALID);
0128             return false;
0129         }
0130 
0131         $this->_setValue($value);
0132 
0133         $status = @preg_match($this->_pattern, $value);
0134         if (false === $status) {
0135             $this->_error(self::ERROROUS);
0136             return false;
0137         }
0138 
0139         if (!$status) {
0140             $this->_error(self::NOT_MATCH);
0141             return false;
0142         }
0143 
0144         return true;
0145     }
0146 }