Regex.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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-2012 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-2012 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_Regex extends Zend_Validate_Abstract
  32. {
  33. const INVALID = 'regexInvalid';
  34. const NOT_MATCH = 'regexNotMatch';
  35. const ERROROUS = 'regexErrorous';
  36. /**
  37. * @var array
  38. */
  39. protected $_messageTemplates = array(
  40. self::INVALID => "Invalid type given. String, integer or float expected",
  41. self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'",
  42. self::ERROROUS => "There was an internal error while using the pattern '%pattern%'",
  43. );
  44. /**
  45. * @var array
  46. */
  47. protected $_messageVariables = array(
  48. 'pattern' => '_pattern'
  49. );
  50. /**
  51. * Regular expression pattern
  52. *
  53. * @var string
  54. */
  55. protected $_pattern;
  56. /**
  57. * Sets validator options
  58. *
  59. * @param string|Zend_Config $pattern
  60. * @throws Zend_Validate_Exception On missing 'pattern' parameter
  61. * @return void
  62. */
  63. public function __construct($pattern)
  64. {
  65. if ($pattern instanceof Zend_Config) {
  66. $pattern = $pattern->toArray();
  67. }
  68. if (is_array($pattern)) {
  69. if (array_key_exists('pattern', $pattern)) {
  70. $pattern = $pattern['pattern'];
  71. } else {
  72. require_once 'Zend/Validate/Exception.php';
  73. throw new Zend_Validate_Exception("Missing option 'pattern'");
  74. }
  75. }
  76. $this->setPattern($pattern);
  77. }
  78. /**
  79. * Returns the pattern option
  80. *
  81. * @return string
  82. */
  83. public function getPattern()
  84. {
  85. return $this->_pattern;
  86. }
  87. /**
  88. * Sets the pattern option
  89. *
  90. * @param string $pattern
  91. * @throws Zend_Validate_Exception if there is a fatal error in pattern matching
  92. * @return Zend_Validate_Regex Provides a fluent interface
  93. */
  94. public function setPattern($pattern)
  95. {
  96. $this->_pattern = (string) $pattern;
  97. $status = @preg_match($this->_pattern, "Test");
  98. if (false === $status) {
  99. require_once 'Zend/Validate/Exception.php';
  100. throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'");
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Defined by Zend_Validate_Interface
  106. *
  107. * Returns true if and only if $value matches against the pattern option
  108. *
  109. * @param string $value
  110. * @return boolean
  111. */
  112. public function isValid($value)
  113. {
  114. if (!is_string($value) && !is_int($value) && !is_float($value)) {
  115. $this->_error(self::INVALID);
  116. return false;
  117. }
  118. $this->_setValue($value);
  119. $status = @preg_match($this->_pattern, $value);
  120. if (false === $status) {
  121. $this->_error(self::ERROROUS);
  122. return false;
  123. }
  124. if (!$status) {
  125. $this->_error(self::NOT_MATCH);
  126. return false;
  127. }
  128. return true;
  129. }
  130. }