Alnum.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Alnum extends Zend_Validate_Abstract
  32. {
  33. const INVALID = 'alnumInvalid';
  34. const NOT_ALNUM = 'notAlnum';
  35. const STRING_EMPTY = 'stringEmpty';
  36. /**
  37. * Whether to allow white space characters; off by default
  38. *
  39. * @var boolean
  40. * @depreciated
  41. */
  42. public $allowWhiteSpace;
  43. /**
  44. * Alphanumeric filter used for validation
  45. *
  46. * @var Zend_Filter_Alnum
  47. */
  48. protected static $_filter = null;
  49. /**
  50. * Validation failure message template definitions
  51. *
  52. * @var array
  53. */
  54. protected $_messageTemplates = array(
  55. self::INVALID => "Invalid type given, value should be float, string, or integer",
  56. self::NOT_ALNUM => "'%value%' has not only alphabetic and digit characters",
  57. self::STRING_EMPTY => "'%value%' is an empty string"
  58. );
  59. /**
  60. * Sets default option values for this instance
  61. *
  62. * @param boolean $allowWhiteSpace
  63. * @return void
  64. */
  65. public function __construct($allowWhiteSpace = false)
  66. {
  67. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  68. }
  69. /**
  70. * Returns the allowWhiteSpace option
  71. *
  72. * @return boolean
  73. */
  74. public function getAllowWhiteSpace()
  75. {
  76. return $this->allowWhiteSpace;
  77. }
  78. /**
  79. * Sets the allowWhiteSpace option
  80. *
  81. * @param boolean $allowWhiteSpace
  82. * @return Zend_Filter_Alnum Provides a fluent interface
  83. */
  84. public function setAllowWhiteSpace($allowWhiteSpace)
  85. {
  86. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  87. return $this;
  88. }
  89. /**
  90. * Defined by Zend_Validate_Interface
  91. *
  92. * Returns true if and only if $value contains only alphabetic and digit characters
  93. *
  94. * @param string $value
  95. * @return boolean
  96. */
  97. public function isValid($value)
  98. {
  99. if (!is_string($value) && !is_int($value) && !is_float($value)) {
  100. $this->_error(self::INVALID);
  101. return false;
  102. }
  103. $this->_setValue($value);
  104. if ('' === $value) {
  105. $this->_error(self::STRING_EMPTY);
  106. return false;
  107. }
  108. if (null === self::$_filter) {
  109. /**
  110. * @see Zend_Filter_Alnum
  111. */
  112. require_once 'Zend/Filter/Alnum.php';
  113. self::$_filter = new Zend_Filter_Alnum();
  114. }
  115. self::$_filter->allowWhiteSpace = $this->allowWhiteSpace;
  116. if ($value !== self::$_filter->filter($value)) {
  117. $this->_error(self::NOT_ALNUM);
  118. return false;
  119. }
  120. return true;
  121. }
  122. }