Callback.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_Callback extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Invalid callback
  35. */
  36. const INVALID_CALLBACK = 'callbackInvalid';
  37. /**
  38. * Invalid value
  39. */
  40. const INVALID_VALUE = 'callbackValue';
  41. /**
  42. * Validation failure message template definitions
  43. *
  44. * @var array
  45. */
  46. protected $_messageTemplates = array(
  47. self::INVALID_VALUE => "'%value%' is not valid",
  48. self::INVALID_CALLBACK => "An exception has been raised within the callback",
  49. );
  50. /**
  51. * Callback in a call_user_func format
  52. *
  53. * @var string|array
  54. */
  55. protected $_callback = null;
  56. /**
  57. * Default options to set for the filter
  58. *
  59. * @var mixed
  60. */
  61. protected $_options = array();
  62. /**
  63. * Sets validator options
  64. *
  65. * @param mixed $callback
  66. * @throws Zend_Validate_Exception
  67. */
  68. public function __construct($callback = null)
  69. {
  70. if (is_callable($callback)) {
  71. $this->setCallback($callback);
  72. } elseif (is_array($callback)) {
  73. if (isset($callback['callback'])) {
  74. $this->setCallback($callback['callback']);
  75. }
  76. if (isset($callback['options'])) {
  77. $this->setOptions($callback['options']);
  78. }
  79. }
  80. if (null === ($initializedCallack = $this->getCallback())) {
  81. #require_once 'Zend/Validate/Exception.php';
  82. throw new Zend_Validate_Exception('No callback registered');
  83. }
  84. }
  85. /**
  86. * Returns the set callback
  87. *
  88. * @return mixed
  89. */
  90. public function getCallback()
  91. {
  92. return $this->_callback;
  93. }
  94. /**
  95. * Sets the callback
  96. *
  97. * @param string|array $callback
  98. * @throws Zend_Validate_Exception
  99. * @return Zend_Validate_Callback Provides a fluent interface
  100. */
  101. public function setCallback($callback)
  102. {
  103. if (!is_callable($callback)) {
  104. #require_once 'Zend/Validate/Exception.php';
  105. throw new Zend_Validate_Exception('Invalid callback given');
  106. }
  107. $this->_callback = $callback;
  108. return $this;
  109. }
  110. /**
  111. * Returns the set options for the callback
  112. *
  113. * @return mixed
  114. */
  115. public function getOptions()
  116. {
  117. return $this->_options;
  118. }
  119. /**
  120. * Sets options for the callback
  121. *
  122. * @param mixed $options
  123. * @return Zend_Validate_Callback Provides a fluent interface
  124. */
  125. public function setOptions($options)
  126. {
  127. $this->_options = (array) $options;
  128. return $this;
  129. }
  130. /**
  131. * Defined by Zend_Validate_Interface
  132. *
  133. * Returns true if and only if the set callback returns
  134. * for the provided $value
  135. *
  136. * @param mixed $value
  137. * @return boolean
  138. */
  139. public function isValid($value)
  140. {
  141. $this->_setValue($value);
  142. $options = $this->getOptions();
  143. $callback = $this->getCallback();
  144. $args = func_get_args();
  145. $options = array_merge($args, $options);
  146. try {
  147. if (!call_user_func_array($callback, $options)) {
  148. $this->_error(self::INVALID_VALUE);
  149. return false;
  150. }
  151. } catch (Exception $e) {
  152. $this->_error(self::INVALID_CALLBACK);
  153. return false;
  154. }
  155. return true;
  156. }
  157. }