PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Tag/ItemList.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 238 lines | 102 code | 24 blank | 112 comment | 15 complexity | 7961dc8244f51ba0bb9f9ed34ef54ce1 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_Tag
  17. * @subpackage ItemList
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ItemList.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Tag_Taggable
  24. */
  25. require_once 'Zend/Tag/Taggable.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Tag
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Tag_ItemList implements Countable, SeekableIterator, ArrayAccess
  33. {
  34. /**
  35. * Items in this list
  36. *
  37. * @var array
  38. */
  39. protected $_items = array();
  40. /**
  41. * Count all items
  42. *
  43. * @return integer
  44. */
  45. public function count()
  46. {
  47. return count($this->_items);
  48. }
  49. /**
  50. * Spread values in the items relative to their weight
  51. *
  52. * @param array $values
  53. * @throws Zend_Tag_Exception When value list is empty
  54. * @return void
  55. */
  56. public function spreadWeightValues(array $values)
  57. {
  58. // Don't allow an empty value list
  59. if (count($values) === 0) {
  60. require_once 'Zend/Tag/Exception.php';
  61. throw new Zend_Tag_Exception('Value list may not be empty');
  62. }
  63. // Re-index the array
  64. $values = array_values($values);
  65. // If just a single value is supplied simply assign it to to all tags
  66. if (count($values) === 1) {
  67. foreach ($this->_items as $item) {
  68. $item->setParam('weightValue', $values[0]);
  69. }
  70. } else {
  71. // Calculate min- and max-weight
  72. $minWeight = null;
  73. $maxWeight = null;
  74. foreach ($this->_items as $item) {
  75. if ($minWeight === null && $maxWeight === null) {
  76. $minWeight = $item->getWeight();
  77. $maxWeight = $item->getWeight();
  78. } else {
  79. $minWeight = min($minWeight, $item->getWeight());
  80. $maxWeight = max($maxWeight, $item->getWeight());
  81. }
  82. }
  83. // Calculate the thresholds
  84. $steps = count($values);
  85. $delta = ($maxWeight - $minWeight) / ($steps - 1);
  86. $thresholds = array();
  87. for ($i = 0; $i < $steps; $i++) {
  88. $thresholds[$i] = floor(100 * log(($minWeight + $i * $delta) + 2));
  89. }
  90. // Then assign the weight values
  91. foreach ($this->_items as $item) {
  92. $threshold = floor(100 * log($item->getWeight() + 2));
  93. for ($i = 0; $i < $steps; $i++) {
  94. if ($threshold <= $thresholds[$i]) {
  95. $item->setParam('weightValue', $values[$i]);
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Seek to an absolute positio
  104. *
  105. * @param integer $index
  106. * @throws OutOfBoundsException When the seek position is invalid
  107. * @return void
  108. */
  109. public function seek($index)
  110. {
  111. $this->rewind();
  112. $position = 0;
  113. while ($position < $index && $this->valid()) {
  114. $this->next();
  115. $position++;
  116. }
  117. if (!$this->valid()) {
  118. throw new OutOfBoundsException('Invalid seek position');
  119. }
  120. }
  121. /**
  122. * Return the current element
  123. *
  124. * @return mixed
  125. */
  126. public function current()
  127. {
  128. return current($this->_items);
  129. }
  130. /**
  131. * Move forward to next element
  132. *
  133. * @return mixed
  134. */
  135. public function next()
  136. {
  137. return next($this->_items);
  138. }
  139. /**
  140. * Return the key of the current element
  141. *
  142. * @return mixed
  143. */
  144. public function key()
  145. {
  146. return key($this->_items);
  147. }
  148. /**
  149. * Check if there is a current element after calls to rewind() or next()
  150. *
  151. * @return boolean
  152. */
  153. public function valid()
  154. {
  155. return ($this->current() !== false);
  156. }
  157. /**
  158. * Rewind the Iterator to the first element
  159. *
  160. * @return void
  161. */
  162. public function rewind()
  163. {
  164. reset($this->_items);
  165. }
  166. /**
  167. * Check if an offset exists
  168. *
  169. * @param mixed $offset
  170. * @return boolean
  171. */
  172. public function offsetExists($offset) {
  173. return array_key_exists($offset, $this->_items);
  174. }
  175. /**
  176. * Get the value of an offset
  177. *
  178. * @param mixed $offset
  179. * @return Zend_Tag_Taggable
  180. */
  181. public function offsetGet($offset) {
  182. return $this->_items[$offset];
  183. }
  184. /**
  185. * Append a new item
  186. *
  187. * @param mixed $offset
  188. * @param Zend_Tag_Taggable $item
  189. * @throws OutOfBoundsException When item does not implement Zend_Tag_Taggable
  190. * @return void
  191. */
  192. public function offsetSet($offset, $item) {
  193. // We need to make that check here, as the method signature must be
  194. // compatible with ArrayAccess::offsetSet()
  195. if (!($item instanceof Zend_Tag_Taggable)) {
  196. require_once 'Zend/Tag/Exception.php';
  197. throw new Zend_Tag_Exception('Item must implement Zend_Tag_Taggable');
  198. }
  199. if ($offset === null) {
  200. $this->_items[] = $item;
  201. } else {
  202. $this->_items[$offset] = $item;
  203. }
  204. }
  205. /**
  206. * Unset an item
  207. *
  208. * @param mixed $offset
  209. * @return void
  210. */
  211. public function offsetUnset($offset) {
  212. unset($this->_items[$offset]);
  213. }
  214. }