Alnum.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-2015 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-2015 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 = 'alnumStringEmpty';
  36. /**
  37. * Whether to allow white space characters; off by default
  38. *
  39. * @var boolean
  40. * @deprecated
  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. String, integer or float expected",
  56. self::NOT_ALNUM => "'%value%' contains characters which are non alphabetic and no digits",
  57. self::STRING_EMPTY => "'%value%' is an empty string",
  58. );
  59. /**
  60. * Sets default option values for this instance
  61. *
  62. * @param boolean|Zend_Config $allowWhiteSpace
  63. */
  64. public function __construct($allowWhiteSpace = false)
  65. {
  66. if ($allowWhiteSpace instanceof Zend_Config) {
  67. $allowWhiteSpace = $allowWhiteSpace->toArray();
  68. }
  69. if (is_array($allowWhiteSpace)) {
  70. if (array_key_exists('allowWhiteSpace', $allowWhiteSpace)) {
  71. $allowWhiteSpace = $allowWhiteSpace['allowWhiteSpace'];
  72. } else {
  73. $allowWhiteSpace = false;
  74. }
  75. }
  76. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  77. }
  78. /**
  79. * Returns the allowWhiteSpace option
  80. *
  81. * @return boolean
  82. */
  83. public function getAllowWhiteSpace()
  84. {
  85. return $this->allowWhiteSpace;
  86. }
  87. /**
  88. * Sets the allowWhiteSpace option
  89. *
  90. * @param boolean $allowWhiteSpace
  91. * @return Zend_Filter_Alnum Provides a fluent interface
  92. */
  93. public function setAllowWhiteSpace($allowWhiteSpace)
  94. {
  95. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  96. return $this;
  97. }
  98. /**
  99. * Defined by Zend_Validate_Interface
  100. *
  101. * Returns true if and only if $value contains only alphabetic and digit characters
  102. *
  103. * @param string $value
  104. * @return boolean
  105. */
  106. public function isValid($value)
  107. {
  108. if (!is_string($value) && !is_int($value) && !is_float($value)) {
  109. $this->_error(self::INVALID);
  110. return false;
  111. }
  112. $this->_setValue($value);
  113. if ('' === $value) {
  114. $this->_error(self::STRING_EMPTY);
  115. return false;
  116. }
  117. if (null === self::$_filter) {
  118. /**
  119. * @see Zend_Filter_Alnum
  120. */
  121. #require_once 'Zend/Filter/Alnum.php';
  122. self::$_filter = new Zend_Filter_Alnum();
  123. }
  124. self::$_filter->allowWhiteSpace = $this->allowWhiteSpace;
  125. if ($value != self::$_filter->filter($value)) {
  126. $this->_error(self::NOT_ALNUM);
  127. return false;
  128. }
  129. return true;
  130. }
  131. }