PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Validate/Alnum.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 150 lines | 60 code | 18 blank | 72 comment | 11 complexity | 4b18db28219ced633c100108451c1e29 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Alnum.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * @return void
  64. */
  65. public function __construct($allowWhiteSpace = false)
  66. {
  67. if ($allowWhiteSpace instanceof Zend_Config) {
  68. $allowWhiteSpace = $allowWhiteSpace->toArray();
  69. }
  70. if (is_array($allowWhiteSpace)) {
  71. if (array_key_exists('allowWhiteSpace', $allowWhiteSpace)) {
  72. $allowWhiteSpace = $allowWhiteSpace['allowWhiteSpace'];
  73. } else {
  74. $allowWhiteSpace = false;
  75. }
  76. }
  77. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  78. }
  79. /**
  80. * Returns the allowWhiteSpace option
  81. *
  82. * @return boolean
  83. */
  84. public function getAllowWhiteSpace()
  85. {
  86. return $this->allowWhiteSpace;
  87. }
  88. /**
  89. * Sets the allowWhiteSpace option
  90. *
  91. * @param boolean $allowWhiteSpace
  92. * @return Zend_Filter_Alnum Provides a fluent interface
  93. */
  94. public function setAllowWhiteSpace($allowWhiteSpace)
  95. {
  96. $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
  97. return $this;
  98. }
  99. /**
  100. * Defined by Zend_Validate_Interface
  101. *
  102. * Returns true if and only if $value contains only alphabetic and digit characters
  103. *
  104. * @param string $value
  105. * @return boolean
  106. */
  107. public function isValid($value)
  108. {
  109. if (!is_string($value) && !is_int($value) && !is_float($value)) {
  110. $this->_error(self::INVALID);
  111. return false;
  112. }
  113. $this->_setValue($value);
  114. if ('' === $value) {
  115. $this->_error(self::STRING_EMPTY);
  116. return false;
  117. }
  118. if (null === self::$_filter) {
  119. /**
  120. * @see Zend_Filter_Alnum
  121. */
  122. require_once 'Zend/Filter/Alnum.php';
  123. self::$_filter = new Zend_Filter_Alnum();
  124. }
  125. self::$_filter->allowWhiteSpace = $this->allowWhiteSpace;
  126. if ($value != self::$_filter->filter($value)) {
  127. $this->_error(self::NOT_ALNUM);
  128. return false;
  129. }
  130. return true;
  131. }
  132. }