Alpha.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_Alpha extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Validation failure message key for when the value contains non-alphabetic characters
  35. */
  36. const NOT_ALPHA = 'notAlpha';
  37. /**
  38. * Validation failure message key for when the value is an empty string
  39. */
  40. const STRING_EMPTY = 'stringEmpty';
  41. /**
  42. * Whether to allow white space characters; off by default
  43. *
  44. * @var boolean
  45. * @depreciated
  46. */
  47. public $allowWhiteSpace;
  48. /**
  49. * Alphabetic filter used for validation
  50. *
  51. * @var Zend_Filter_Alpha
  52. */
  53. protected static $_filter = null;
  54. /**
  55. * Validation failure message template definitions
  56. *
  57. * @var array
  58. */
  59. protected $_messageTemplates = array(
  60. self::NOT_ALPHA => "'%value%' has not only alphabetic characters",
  61. self::STRING_EMPTY => "'%value%' is an empty string"
  62. );
  63. /**
  64. * Sets default option values for this instance
  65. *
  66. * @param boolean $allowWhiteSpace
  67. * @return void
  68. */
  69. public function __construct($allowWhiteSpace = false)
  70. {
  71. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  72. }
  73. /**
  74. * Returns the allowWhiteSpace option
  75. *
  76. * @return boolean
  77. */
  78. public function getAllowWhiteSpace()
  79. {
  80. return $this->allowWhiteSpace;
  81. }
  82. /**
  83. * Sets the allowWhiteSpace option
  84. *
  85. * @param boolean $allowWhiteSpace
  86. * @return Zend_Filter_Alpha Provides a fluent interface
  87. */
  88. public function setAllowWhiteSpace($allowWhiteSpace)
  89. {
  90. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  91. return $this;
  92. }
  93. /**
  94. * Defined by Zend_Validate_Interface
  95. *
  96. * Returns true if and only if $value contains only alphabetic characters
  97. *
  98. * @param string $value
  99. * @return boolean
  100. */
  101. public function isValid($value)
  102. {
  103. $valueString = (string) $value;
  104. $this->_setValue($valueString);
  105. if ('' === $valueString) {
  106. $this->_error(self::STRING_EMPTY);
  107. return false;
  108. }
  109. if (null === self::$_filter) {
  110. /**
  111. * @see Zend_Filter_Alpha
  112. */
  113. require_once 'Zend/Filter/Alpha.php';
  114. self::$_filter = new Zend_Filter_Alpha();
  115. }
  116. self::$_filter->allowWhiteSpace = $this->allowWhiteSpace;
  117. if ($valueString !== self::$_filter->filter($valueString)) {
  118. $this->_error(self::NOT_ALPHA);
  119. return false;
  120. }
  121. return true;
  122. }
  123. }