/vendor/magento/zendframework1/library/Zend/EventManager/ResponseCollection.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 424 lines · 180 code · 36 blank · 208 comment · 11 complexity · 14500f355513b35b17a7dd7e947f5301 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_EventManager
  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. */
  20. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  21. class SplStack implements Iterator, ArrayAccess, Countable
  22. {
  23. /**
  24. * Delete items during iteration
  25. */
  26. const IT_MODE_DELETE = 1;
  27. /**
  28. * Keep items during iteration
  29. */
  30. const IT_MODE_KEEP = 0;
  31. /**
  32. * Mode used when iterating
  33. * @var int
  34. */
  35. protected $mode = self::IT_MODE_KEEP;
  36. /**
  37. * Count of elements in the stack
  38. *
  39. * @var int
  40. */
  41. protected $count = 0;
  42. /**
  43. * Data represented by this stack
  44. *
  45. * @var array
  46. */
  47. protected $data = array();
  48. /**
  49. * Sorted stack of values
  50. *
  51. * @var false|array
  52. */
  53. protected $stack = false;
  54. /**
  55. * Set the iterator mode
  56. *
  57. * Must be set to one of IT_MODE_DELETE or IT_MODE_KEEP
  58. *
  59. * @todo Currently, IteratorMode is ignored, as we use the default (keep); should this be implemented?
  60. * @param int $mode
  61. * @return void
  62. * @throws InvalidArgumentException
  63. */
  64. public function setIteratorMode($mode)
  65. {
  66. $expected = array(
  67. self::IT_MODE_DELETE => true,
  68. self::IT_MODE_KEEP => true,
  69. );
  70. if (!isset($expected[$mode])) {
  71. throw new InvalidArgumentException(sprintf('Invalid iterator mode specified ("%s")', $mode));
  72. }
  73. $this->mode = $mode;
  74. }
  75. /**
  76. * Return last element in the stack
  77. *
  78. * @return mixed
  79. */
  80. public function bottom()
  81. {
  82. $this->rewind();
  83. $value = array_pop($this->stack);
  84. array_push($this->stack, $value);
  85. return $value;
  86. }
  87. /**
  88. * Countable: return count of items in the stack
  89. *
  90. * @return int
  91. */
  92. public function count()
  93. {
  94. return $this->count;
  95. }
  96. /**
  97. * Iterator: return current item in the stack
  98. *
  99. * @return mixed
  100. */
  101. public function current()
  102. {
  103. if (!$this->stack) {
  104. $this->rewind();
  105. }
  106. return current($this->stack);
  107. }
  108. /**
  109. * Get iteration mode
  110. *
  111. * @return int
  112. */
  113. public function getIteratorMode()
  114. {
  115. return $this->mode;
  116. }
  117. /**
  118. * Is the stack empty?
  119. *
  120. * @return bool
  121. */
  122. public function isEmpty()
  123. {
  124. return ($this->count === 0);
  125. }
  126. /**
  127. * Iterator: return key of current item in the stack
  128. *
  129. * @return mixed
  130. */
  131. public function key()
  132. {
  133. if (!$this->stack) {
  134. $this->rewind();
  135. }
  136. return key($this->stack);
  137. }
  138. /**
  139. * Iterator: advance pointer to next item in the stack
  140. *
  141. * @return void
  142. */
  143. public function next()
  144. {
  145. if (!$this->stack) {
  146. $this->rewind();
  147. }
  148. return next($this->stack);
  149. }
  150. /**
  151. * ArrayAccess: does an item exist at the specified offset?
  152. *
  153. * @param mixed $index
  154. * @return bool
  155. */
  156. public function offsetExists($index)
  157. {
  158. return array_key_exists($index, $this->data);
  159. }
  160. /**
  161. * ArrayAccess: get the item at the specified offset
  162. *
  163. * @param mixed $index
  164. * @return mixed
  165. * @throws OutOfRangeException
  166. */
  167. public function offsetGet($index)
  168. {
  169. if (!$this->offsetExists($index)) {
  170. throw OutOfRangeException(sprintf('Invalid index ("%s") specified', $index));
  171. }
  172. return $this->data[$index];
  173. }
  174. /**
  175. * ArrayAccess: add an item at the specified offset
  176. *
  177. * @param mixed $index
  178. * @param mixed $newval
  179. * @return void
  180. */
  181. public function offsetSet($index, $newval)
  182. {
  183. $this->data[$index] = $newval;
  184. $this->stack = false;
  185. $this->count++;
  186. }
  187. /**
  188. * ArrayAccess: unset the item at the specified offset
  189. *
  190. * @param mixed $index
  191. * @return void
  192. * @throws OutOfRangeException
  193. */
  194. public function offsetUnset($index)
  195. {
  196. if (!$this->offsetExists($index)) {
  197. throw OutOfRangeException(sprintf('Invalid index ("%s") specified', $index));
  198. }
  199. unset($this->data[$index]);
  200. $this->stack = false;
  201. $this->count--;
  202. }
  203. /**
  204. * Pop a node from the end of the stack
  205. *
  206. * @return mixed
  207. * @throws RuntimeException
  208. */
  209. public function pop()
  210. {
  211. $val = array_pop($this->data);
  212. $this->stack = false;
  213. $this->count--;
  214. return $val;
  215. }
  216. /**
  217. * Move the iterator to the previous node
  218. *
  219. * @todo Does this need to be implemented?
  220. * @return void
  221. */
  222. public function prev()
  223. {
  224. }
  225. /**
  226. * Push an element to the list
  227. *
  228. * @param mixed $value
  229. * @return void
  230. */
  231. public function push($value)
  232. {
  233. array_push($this->data, $value);
  234. $this->count++;
  235. $this->stack = false;
  236. }
  237. /**
  238. * Iterator: rewind to beginning of stack
  239. *
  240. * @return void
  241. */
  242. public function rewind()
  243. {
  244. if (is_array($this->stack)) {
  245. return reset($this->stack);
  246. }
  247. $this->stack = array_reverse($this->data, true);
  248. }
  249. /**
  250. * Serialize the storage
  251. *
  252. * @return string
  253. */
  254. public function serialize()
  255. {
  256. return serialize($this->data);
  257. }
  258. /**
  259. * Shifts a node from the beginning of the list
  260. *
  261. * @return mixed
  262. * @throws RuntimeException
  263. */
  264. public function shift()
  265. {
  266. $val = array_shift($this->data);
  267. $this->stack = false;
  268. $this->count--;
  269. return $val;
  270. }
  271. /**
  272. * Peek at the top node of the stack
  273. *
  274. * @return mixed
  275. */
  276. public function top()
  277. {
  278. $this->rewind();
  279. $value = array_shift($this->stack);
  280. array_unshift($this->stack, $value);
  281. return $value;
  282. }
  283. /**
  284. * Unserialize the storage
  285. *
  286. * @param string
  287. * @return void
  288. */
  289. public function unserialize($serialized)
  290. {
  291. $this->data = unserialize($serialized);
  292. $this->stack = false;
  293. }
  294. /**
  295. * Unshift a node onto the beginning of the list
  296. *
  297. * @param mixed $value
  298. * @return void
  299. */
  300. public function unshift($value)
  301. {
  302. array_unshift($this->data, $value);
  303. $this->count++;
  304. $this->stack = false;
  305. }
  306. /**
  307. * Iterator: is the current pointer valid?
  308. *
  309. * @return bool
  310. */
  311. public function valid()
  312. {
  313. $key = key($this->stack);
  314. $var = ($key !== null && $key !== false);
  315. return $var;
  316. }
  317. }
  318. }
  319. /**
  320. * Collection of signal handler return values
  321. *
  322. * @category Zend
  323. * @package Zend_EventManager
  324. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  325. * @license http://framework.zend.com/license/new-bsd New BSD License
  326. */
  327. class Zend_EventManager_ResponseCollection extends SplStack
  328. {
  329. protected $stopped = false;
  330. /**
  331. * Did the last response provided trigger a short circuit of the stack?
  332. *
  333. * @return bool
  334. */
  335. public function stopped()
  336. {
  337. return $this->stopped;
  338. }
  339. /**
  340. * Mark the collection as stopped (or its opposite)
  341. *
  342. * @param bool $flag
  343. * @return Zend_EventManager_ResponseCollection
  344. */
  345. public function setStopped($flag)
  346. {
  347. $this->stopped = (bool) $flag;
  348. return $this;
  349. }
  350. /**
  351. * Convenient access to the first handler return value.
  352. *
  353. * @return mixed The first handler return value
  354. */
  355. public function first()
  356. {
  357. return parent::bottom();
  358. }
  359. /**
  360. * Convenient access to the last handler return value.
  361. *
  362. * If the collection is empty, returns null. Otherwise, returns value
  363. * returned by last handler.
  364. *
  365. * @return mixed The last handler return value
  366. */
  367. public function last()
  368. {
  369. if (count($this) === 0) {
  370. return null;
  371. }
  372. return parent::top();
  373. }
  374. /**
  375. * Check if any of the responses match the given value.
  376. *
  377. * @param mixed $value The value to look for among responses
  378. */
  379. public function contains($value)
  380. {
  381. foreach ($this as $response) {
  382. if ($response === $value) {
  383. return true;
  384. }
  385. }
  386. return false;
  387. }
  388. }