ElggPriorityList.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * Iterate over elements in a specific priority.
  4. *
  5. * $pl = new \ElggPriorityList();
  6. * $pl->add('Element 0');
  7. * $pl->add('Element 10', 10);
  8. * $pl->add('Element -10', -10);
  9. *
  10. * foreach ($pl as $priority => $element) {
  11. * var_dump("$priority => $element");
  12. * }
  13. *
  14. * Yields:
  15. * -10 => Element -10
  16. * 0 => Element 0
  17. * 10 => Element 10
  18. *
  19. * Collisions on priority are handled by inserting the element at or as close to the
  20. * requested priority as possible:
  21. *
  22. * $pl = new \ElggPriorityList();
  23. * $pl->add('Element 5', 5);
  24. * $pl->add('Colliding element 5', 5);
  25. * $pl->add('Another colliding element 5', 5);
  26. *
  27. * foreach ($pl as $priority => $element) {
  28. * var_dump("$priority => $element");
  29. * }
  30. *
  31. * Yields:
  32. * 5 => 'Element 5',
  33. * 6 => 'Colliding element 5',
  34. * 7 => 'Another colliding element 5'
  35. *
  36. * You can do priority lookups by element:
  37. *
  38. * $pl = new \ElggPriorityList();
  39. * $pl->add('Element 0');
  40. * $pl->add('Element -5', -5);
  41. * $pl->add('Element 10', 10);
  42. * $pl->add('Element -10', -10);
  43. *
  44. * $priority = $pl->getPriority('Element -5');
  45. *
  46. * Or element lookups by priority.
  47. * $element = $pl->getElement(-5);
  48. *
  49. * To remove elements, pass the element.
  50. * $pl->remove('Element -10');
  51. *
  52. * To check if an element exists:
  53. * $pl->contains('Element -5');
  54. *
  55. * To move an element:
  56. * $pl->move('Element -5', -3);
  57. *
  58. * \ElggPriorityList only tracks priority. No checking is done in \ElggPriorityList for duplicates or
  59. * updating. If you need to track this use objects and an external map:
  60. *
  61. * function elgg_register_something($id, $display_name, $location, $priority = 500) {
  62. * // $id => $element.
  63. * static $map = array();
  64. * static $list;
  65. *
  66. * if (!$list) {
  67. * $list = new \ElggPriorityList();
  68. * }
  69. *
  70. * // update if already registered.
  71. * if (isset($map[$id])) {
  72. * $element = $map[$id];
  73. * // move it first because we have to pass the original element.
  74. * if (!$list->move($element, $priority)) {
  75. * return false;
  76. * }
  77. * $element->display_name = $display_name;
  78. * $element->location = $location;
  79. * } else {
  80. * $element = new \stdClass();
  81. * $element->display_name = $display_name;
  82. * $element->location = $location;
  83. * if (!$list->add($element, $priority)) {
  84. * return false;
  85. * }
  86. * $map[$id] = $element;
  87. * }
  88. *
  89. * return true;
  90. * }
  91. *
  92. * @package Elgg.Core
  93. * @subpackage Helpers
  94. */
  95. class ElggPriorityList
  96. implements \Iterator, \Countable {
  97. /**
  98. * The list of elements
  99. *
  100. * @var array
  101. */
  102. private $elements = array();
  103. /**
  104. * Create a new priority list.
  105. *
  106. * @param array $elements An optional array of priorities => element
  107. */
  108. public function __construct(array $elements = array()) {
  109. if ($elements) {
  110. foreach ($elements as $priority => $element) {
  111. $this->add($element, $priority);
  112. }
  113. }
  114. }
  115. /**
  116. * Adds an element to the list.
  117. *
  118. * @warning This returns the priority at which the element was added, which can be 0. Use
  119. * !== false to check for success.
  120. *
  121. * @param mixed $element The element to add to the list.
  122. * @param mixed $priority Priority to add the element. In priority collisions, the original element
  123. * maintains its priority and the new element is to the next available
  124. * slot, taking into consideration all previously registered elements.
  125. * Negative elements are accepted.
  126. * @param bool $exact unused
  127. * @return int The priority of the added element.
  128. * @todo remove $exact or implement it. Note we use variable name strict below.
  129. */
  130. public function add($element, $priority = null, $exact = false) {
  131. if ($priority !== null && !is_numeric($priority)) {
  132. return false;
  133. } else {
  134. $priority = $this->getNextPriority($priority);
  135. }
  136. $this->elements[$priority] = $element;
  137. $this->sorted = false;
  138. return $priority;
  139. }
  140. /**
  141. * Removes an element from the list.
  142. *
  143. * @warning The element must have the same attributes / values. If using $strict, it must have
  144. * the same types. array(10) will fail in strict against array('10') (str vs int).
  145. *
  146. * @param mixed $element The element to remove from the list
  147. * @param bool $strict Whether to check the type of the element match
  148. * @return bool
  149. */
  150. public function remove($element, $strict = false) {
  151. $index = array_search($element, $this->elements, $strict);
  152. if ($index !== false) {
  153. unset($this->elements[$index]);
  154. return true;
  155. } else {
  156. return false;
  157. }
  158. }
  159. /**
  160. * Move an existing element to a new priority.
  161. *
  162. * @param mixed $element The element to move
  163. * @param int $new_priority The new priority for the element
  164. * @param bool $strict Whether to check the type of the element match
  165. * @return bool
  166. */
  167. public function move($element, $new_priority, $strict = false) {
  168. $new_priority = (int) $new_priority;
  169. $current_priority = $this->getPriority($element, $strict);
  170. if ($current_priority === false) {
  171. return false;
  172. }
  173. if ($current_priority == $new_priority) {
  174. return true;
  175. }
  176. // move the actual element so strict operations still work
  177. $element = $this->getElement($current_priority);
  178. unset($this->elements[$current_priority]);
  179. return $this->add($element, $new_priority);
  180. }
  181. /**
  182. * Returns the elements
  183. *
  184. * @return array
  185. */
  186. public function getElements() {
  187. $this->sortIfUnsorted();
  188. return $this->elements;
  189. }
  190. /**
  191. * Sort the elements optionally by a callback function.
  192. *
  193. * If no user function is provided the elements are sorted by priority registered.
  194. *
  195. * The callback function should accept the array of elements as the first
  196. * argument and should return a sorted array.
  197. *
  198. * This function can be called multiple times.
  199. *
  200. * @param callback $callback The callback for sorting. Numeric sorting is the default.
  201. * @return bool
  202. */
  203. public function sort($callback = null) {
  204. if (!$callback) {
  205. ksort($this->elements, SORT_NUMERIC);
  206. } else {
  207. $sorted = call_user_func($callback, $this->elements);
  208. if (!$sorted) {
  209. return false;
  210. }
  211. $this->elements = $sorted;
  212. }
  213. $this->sorted = true;
  214. return true;
  215. }
  216. /**
  217. * Sort the elements if they haven't been sorted yet.
  218. *
  219. * @return bool
  220. */
  221. private function sortIfUnsorted() {
  222. if (!$this->sorted) {
  223. return $this->sort();
  224. }
  225. }
  226. /**
  227. * Returns the next priority available.
  228. *
  229. * @param int $near Make the priority as close to $near as possible.
  230. * @return int
  231. */
  232. public function getNextPriority($near = 0) {
  233. $near = (int) $near;
  234. while (array_key_exists($near, $this->elements)) {
  235. $near++;
  236. }
  237. return $near;
  238. }
  239. /**
  240. * Returns the priority of an element if it exists in the list.
  241. *
  242. * @warning This can return 0 if the element's priority is 0.
  243. *
  244. * @param mixed $element The element to check for.
  245. * @param bool $strict Use strict checking?
  246. * @return mixed False if the element doesn't exists, the priority if it does.
  247. */
  248. public function getPriority($element, $strict = false) {
  249. return array_search($element, $this->elements, $strict);
  250. }
  251. /**
  252. * Returns the element at $priority.
  253. *
  254. * @param int $priority The priority
  255. * @return mixed The element or false on fail.
  256. */
  257. public function getElement($priority) {
  258. return (isset($this->elements[$priority])) ? $this->elements[$priority] : false;
  259. }
  260. /**
  261. * Returns if the list contains $element.
  262. *
  263. * @param mixed $element The element to check.
  264. * @param bool $strict Use strict checking?
  265. * @return bool
  266. */
  267. public function contains($element, $strict = false) {
  268. return $this->getPriority($element, $strict) !== false;
  269. }
  270. /**********************
  271. * Interface methods *
  272. **********************/
  273. /**
  274. * Iterator
  275. */
  276. /**
  277. * PHP Iterator Interface
  278. *
  279. * @see Iterator::rewind()
  280. * @return void
  281. */
  282. public function rewind() {
  283. $this->sortIfUnsorted();
  284. return reset($this->elements);
  285. }
  286. /**
  287. * PHP Iterator Interface
  288. *
  289. * @see Iterator::current()
  290. * @return mixed
  291. */
  292. public function current() {
  293. $this->sortIfUnsorted();
  294. return current($this->elements);
  295. }
  296. /**
  297. * PHP Iterator Interface
  298. *
  299. * @see Iterator::key()
  300. * @return int
  301. */
  302. public function key() {
  303. $this->sortIfUnsorted();
  304. return key($this->elements);
  305. }
  306. /**
  307. * PHP Iterator Interface
  308. *
  309. * @see Iterator::next()
  310. * @return mixed
  311. */
  312. public function next() {
  313. $this->sortIfUnsorted();
  314. return next($this->elements);
  315. }
  316. /**
  317. * PHP Iterator Interface
  318. *
  319. * @see Iterator::valid()
  320. * @return bool
  321. */
  322. public function valid() {
  323. $this->sortIfUnsorted();
  324. $key = key($this->elements);
  325. return ($key !== null && $key !== false);
  326. }
  327. /**
  328. * Countable interface
  329. *
  330. * @see Countable::count()
  331. * @return int
  332. */
  333. public function count() {
  334. return count($this->elements);
  335. }
  336. }