expand description
[m6w6/hikke] / lib / hikke / Event / Storage.php
1 <?php
2
3 /**
4 * Hikke
5 *
6 * @author Michael Wallner <mike@php.net>
7 */
8 namespace hikke\Event;
9
10 use hikke\Event\Bucket;
11
12 /**
13 * Storage
14 *
15 * @package hikke\Event
16 */
17 class Storage implements \Iterator, \Countable
18 {
19 /**
20 * @var int
21 */
22 private $sequence = 1;
23
24 /**
25 * @var array
26 */
27 private $buckets = array();
28
29 /**
30 * @var array
31 */
32 private $iterator;
33
34 /**
35 * @param object $object
36 * @param int|float $priority
37 * @return \hikke\Event\Bucket
38 */
39 public function insert($object, $priority = 0) {
40 $priority += $this->sequence++ / 1000;
41 $bucket = new Bucket($object, $priority);
42 $this->buckets[$bucket->getHash()] = $bucket;
43 return $bucket;
44 }
45
46 /**
47 * @param object $object
48 * @return bool whether the storage container the object
49 */
50 public function delete($object) {
51 $hash = spl_object_hash($object);
52 if (($found = isset($this->buckets[$hash]))) {
53 unset($this->buckets[$hash]);
54 }
55 return $found;
56 }
57
58 /**
59 * @ignore
60 */
61 public function count() {
62 return count($this->buckets);
63 }
64
65 /**
66 * @ignore
67 */
68 public function rewind() {
69 $this->iterator = $this->buckets;
70 uasort($this->iterator, ["hikke\\Event\\Priority", "compare"]);
71 }
72
73 /**
74 * @ignore
75 */
76 public function valid() {
77 return NULL !== key($this->iterator);
78 }
79
80 /**
81 * @ignore
82 */
83 public function next() {
84 next($this->iterator);
85 }
86
87 /**
88 * @ignore
89 */
90 public function key() {
91 return current($this->iterator)->getPriority();
92 }
93
94 /**
95 * @ignore
96 */
97 public function current() {
98 return current($this->iterator)->getObject();
99 }
100 }