PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/common/lib/Zend/Validate/InArray.php

https://bitbucket.org/haichau59/manga
PHP | 204 lines | 91 code | 17 blank | 96 comment | 18 complexity | 9597e1064a7a7d660511ae44949d03b7 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: InArray.php 24594 2012-01-05 21:27:01Z matthew $
  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_InArray extends Zend_Validate_Abstract
  32. {
  33. const NOT_IN_ARRAY = 'notInArray';
  34. /**
  35. * @var array
  36. */
  37. protected $_messageTemplates = array(
  38. self::NOT_IN_ARRAY => "'%value%' was not found in the haystack",
  39. );
  40. /**
  41. * Haystack of possible values
  42. *
  43. * @var array
  44. */
  45. protected $_haystack;
  46. /**
  47. * Whether a strict in_array() invocation is used
  48. *
  49. * @var boolean
  50. */
  51. protected $_strict = false;
  52. /**
  53. * Whether a recursive search should be done
  54. *
  55. * @var boolean
  56. */
  57. protected $_recursive = false;
  58. /**
  59. * Sets validator options
  60. *
  61. * @param array|Zend_Config $haystack
  62. * @return void
  63. */
  64. public function __construct($options)
  65. {
  66. if ($options instanceof Zend_Config) {
  67. $options = $options->toArray();
  68. } else if (!is_array($options)) {
  69. //require_once 'Zend/Validate/Exception.php';
  70. throw new Zend_Validate_Exception('Array expected as parameter');
  71. } else {
  72. $count = func_num_args();
  73. $temp = array();
  74. if ($count > 1) {
  75. $temp['haystack'] = func_get_arg(0);
  76. $temp['strict'] = func_get_arg(1);
  77. $options = $temp;
  78. } else {
  79. $temp = func_get_arg(0);
  80. if (!array_key_exists('haystack', $options)) {
  81. $options = array();
  82. $options['haystack'] = $temp;
  83. } else {
  84. $options = $temp;
  85. }
  86. }
  87. }
  88. $this->setHaystack($options['haystack']);
  89. if (array_key_exists('strict', $options)) {
  90. $this->setStrict($options['strict']);
  91. }
  92. if (array_key_exists('recursive', $options)) {
  93. $this->setRecursive($options['recursive']);
  94. }
  95. }
  96. /**
  97. * Returns the haystack option
  98. *
  99. * @return mixed
  100. */
  101. public function getHaystack()
  102. {
  103. return $this->_haystack;
  104. }
  105. /**
  106. * Sets the haystack option
  107. *
  108. * @param mixed $haystack
  109. * @return Zend_Validate_InArray Provides a fluent interface
  110. */
  111. public function setHaystack(array $haystack)
  112. {
  113. $this->_haystack = $haystack;
  114. return $this;
  115. }
  116. /**
  117. * Returns the strict option
  118. *
  119. * @return boolean
  120. */
  121. public function getStrict()
  122. {
  123. return $this->_strict;
  124. }
  125. /**
  126. * Sets the strict option
  127. *
  128. * @param boolean $strict
  129. * @return Zend_Validate_InArray Provides a fluent interface
  130. */
  131. public function setStrict($strict)
  132. {
  133. $this->_strict = (boolean) $strict;
  134. return $this;
  135. }
  136. /**
  137. * Returns the recursive option
  138. *
  139. * @return boolean
  140. */
  141. public function getRecursive()
  142. {
  143. return $this->_recursive;
  144. }
  145. /**
  146. * Sets the recursive option
  147. *
  148. * @param boolean $recursive
  149. * @return Zend_Validate_InArray Provides a fluent interface
  150. */
  151. public function setRecursive($recursive)
  152. {
  153. $this->_recursive = (boolean) $recursive;
  154. return $this;
  155. }
  156. /**
  157. * Defined by Zend_Validate_Interface
  158. *
  159. * Returns true if and only if $value is contained in the haystack option. If the strict
  160. * option is true, then the type of $value is also checked.
  161. *
  162. * @param mixed $value
  163. * @return boolean
  164. */
  165. public function isValid($value)
  166. {
  167. $this->_setValue($value);
  168. if ($this->getRecursive()) {
  169. $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($this->_haystack));
  170. foreach($iterator as $element) {
  171. if ($this->_strict) {
  172. if ($element === $value) {
  173. return true;
  174. }
  175. } else if ($element == $value) {
  176. return true;
  177. }
  178. }
  179. } else {
  180. if (in_array($value, $this->_haystack, $this->_strict)) {
  181. return true;
  182. }
  183. }
  184. $this->_error(self::NOT_IN_ARRAY);
  185. return false;
  186. }
  187. }