[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Validate / Alnum.php
blob1585e5f115f4127039dce4aa5a98530a9ac7f9ad
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
22 /**
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
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
33 class Zend_Validate_Alnum extends Zend_Validate_Abstract
35 const INVALID = 'alnumInvalid';
36 const NOT_ALNUM = 'notAlnum';
37 const STRING_EMPTY = 'alnumStringEmpty';
39 /**
40 * Whether to allow white space characters; off by default
42 * @var boolean
43 * @deprecated
45 public $allowWhiteSpace;
47 /**
48 * Alphanumeric filter used for validation
50 * @var Zend_Filter_Alnum
52 protected static $_filter = null;
54 /**
55 * Validation failure message template definitions
57 * @var array
59 protected $_messageTemplates = array(
60 self::INVALID => "Invalid type given, value should be float, string, or integer",
61 self::NOT_ALNUM => "'%value%' contains characters which are non alphabetic and no digits",
62 self::STRING_EMPTY => "'%value%' is an empty string",
65 /**
66 * Sets default option values for this instance
68 * @param boolean|Zend_Config $allowWhiteSpace
69 * @return void
71 public function __construct($allowWhiteSpace = false)
73 if ($allowWhiteSpace instanceof Zend_Config) {
74 $allowWhiteSpace = $allowWhiteSpace->toArray();
77 if (is_array($allowWhiteSpace)) {
78 if (array_key_exists('allowWhiteSpace', $allowWhiteSpace)) {
79 $allowWhiteSpace = $allowWhiteSpace['allowWhiteSpace'];
80 } else {
81 $allowWhiteSpace = false;
85 $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
88 /**
89 * Returns the allowWhiteSpace option
91 * @return boolean
93 public function getAllowWhiteSpace()
95 return $this->allowWhiteSpace;
98 /**
99 * Sets the allowWhiteSpace option
101 * @param boolean $allowWhiteSpace
102 * @return Zend_Filter_Alnum Provides a fluent interface
104 public function setAllowWhiteSpace($allowWhiteSpace)
106 $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
107 return $this;
111 * Defined by Zend_Validate_Interface
113 * Returns true if and only if $value contains only alphabetic and digit characters
115 * @param string $value
116 * @return boolean
118 public function isValid($value)
120 if (!is_string($value) && !is_int($value) && !is_float($value)) {
121 $this->_error(self::INVALID);
122 return false;
125 $this->_setValue($value);
127 if ('' === $value) {
128 $this->_error(self::STRING_EMPTY);
129 return false;
132 if (null === self::$_filter) {
134 * @see Zend_Filter_Alnum
136 require_once 'Zend/Filter/Alnum.php';
137 self::$_filter = new Zend_Filter_Alnum();
140 self::$_filter->allowWhiteSpace = $this->allowWhiteSpace;
142 if ($value != self::$_filter->filter($value)) {
143 $this->_error(self::NOT_ALNUM);
144 return false;
147 return true;