File indexing completed on 2024-05-12 06:03:13

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_Alnum extends Zend_Validate_Abstract
0034 {
0035     const INVALID      = 'alnumInvalid';
0036     const NOT_ALNUM    = 'notAlnum';
0037     const STRING_EMPTY = 'alnumStringEmpty';
0038 
0039     /**
0040      * Whether to allow white space characters; off by default
0041      *
0042      * @var boolean
0043      * @deprecated
0044      */
0045     public $allowWhiteSpace;
0046 
0047     /**
0048      * Alphanumeric filter used for validation
0049      *
0050      * @var Zend_Filter_Alnum
0051      */
0052     protected static $_filter = null;
0053 
0054     /**
0055      * Validation failure message template definitions
0056      *
0057      * @var array
0058      */
0059     protected $_messageTemplates = array(
0060         self::INVALID      => "Invalid type given. String, integer or float expected",
0061         self::NOT_ALNUM    => "'%value%' contains characters which are non alphabetic and no digits",
0062         self::STRING_EMPTY => "'%value%' is an empty string",
0063     );
0064 
0065     /**
0066      * Sets default option values for this instance
0067      *
0068      * @param boolean|Zend_Config $allowWhiteSpace
0069      */
0070     public function __construct($allowWhiteSpace = false)
0071     {
0072         if ($allowWhiteSpace instanceof Zend_Config) {
0073             $allowWhiteSpace = $allowWhiteSpace->toArray();
0074         }
0075 
0076         if (is_array($allowWhiteSpace)) {
0077             if (array_key_exists('allowWhiteSpace', $allowWhiteSpace)) {
0078                 $allowWhiteSpace = $allowWhiteSpace['allowWhiteSpace'];
0079             } else {
0080                 $allowWhiteSpace = false;
0081             }
0082         }
0083 
0084         $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
0085     }
0086 
0087     /**
0088      * Returns the allowWhiteSpace option
0089      *
0090      * @return boolean
0091      */
0092     public function getAllowWhiteSpace()
0093     {
0094         return $this->allowWhiteSpace;
0095     }
0096 
0097     /**
0098      * Sets the allowWhiteSpace option
0099      *
0100      * @param boolean $allowWhiteSpace
0101      * @return Zend_Filter_Alnum Provides a fluent interface
0102      */
0103     public function setAllowWhiteSpace($allowWhiteSpace)
0104     {
0105         $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
0106         return $this;
0107     }
0108 
0109     /**
0110      * Defined by Zend_Validate_Interface
0111      *
0112      * Returns true if and only if $value contains only alphabetic and digit characters
0113      *
0114      * @param  string $value
0115      * @return boolean
0116      */
0117     public function isValid($value)
0118     {
0119         if (!is_string($value) && !is_int($value) && !is_float($value)) {
0120             $this->_error(self::INVALID);
0121             return false;
0122         }
0123 
0124         $this->_setValue($value);
0125 
0126         if ('' === $value) {
0127             $this->_error(self::STRING_EMPTY);
0128             return false;
0129         }
0130 
0131         if (null === self::$_filter) {
0132             /**
0133              * @see Zend_Filter_Alnum
0134              */
0135             // require_once 'Zend/Filter/Alnum.php';
0136             self::$_filter = new Zend_Filter_Alnum();
0137         }
0138 
0139         self::$_filter->allowWhiteSpace = $this->allowWhiteSpace;
0140 
0141         if ($value != self::$_filter->filter($value)) {
0142             $this->_error(self::NOT_ALNUM);
0143             return false;
0144         }
0145 
0146         return true;
0147     }
0148 
0149 }