1 <?php
2 /**
3 * Neevo - Tiny database layer for PHP. (http://neevo.smasty.net)
4 *
5 * This source file is subject to the MIT license that is bundled
6 * with this package in the file license.txt.
7 *
8 * Copyright (c) 2011 Martin Srank (http://smasty.net)
9 *
10 */
11
12
13 /**
14 * The map of observers - instances of INeevoObserver.
15 * @author Martin Srank
16 * @package Neevo
17 */
18 class NeevoObserverMap implements Iterator, Countable {
19
20
21 /** @var array */
22 private $storage = array();
23
24 /** @var int */
25 private $pointer = 0;
26
27
28 /**
29 * Add given observer to map for given event.
30 * @param INeevoObserver $observer
31 * @param int $event Event bitmap
32 * @return void
33 */
34 public function attach(INeevoObserver $observer, $event){
35 $this->storage[spl_object_hash($observer)] = array(
36 'observer' => $observer,
37 'event' => $event
38 );
39 }
40
41
42 /**
43 * Remove given observer from map.
44 * @param INeevoObserver $observer
45 * @return void
46 */
47 public function detach(INeevoObserver $observer){
48 unset($this->storage[spl_object_hash($observer)]);
49 }
50
51
52 /**
53 * Check if given observer is in the map.
54 * @param INeevoObserver $observer
55 * @return bool
56 */
57 public function contains(INeevoObserver $observer){
58 return isset($this->storage[spl_object_hash($observer)]);
59 }
60
61
62 /**
63 * Get the event associated with current observer in iteration.
64 * @return int
65 */
66 public function getEvent(){
67 $c = current($this->storage);
68 return $c['event'];
69 }
70
71
72 /**
73 * Get number of observers in map.
74 * @return int
75 */
76 public function count(){
77 return count($this->storage);
78 }
79
80
81 /**
82 * Rewind internal pointer.
83 * @return void
84 */
85 public function rewind(){
86 reset($this->storage);
87 $this->pointer = 0;
88 }
89
90
91 /**
92 * Move to next observer.
93 * @return void
94 */
95 public function next(){
96 next($this->storage);
97 $this->pointer++;
98 }
99
100
101 /**
102 * Check for valid current observer.
103 * @return bool
104 */
105 public function valid(){
106 return key($this->storage) !== null;
107 }
108
109
110 /**
111 * Return the current observer.
112 * @return INeevoObserver
113 */
114 public function current(){
115 $current = current($this->storage);
116 return $current['observer'];
117 }
118
119
120 /**
121 * Return the key of current observer.
122 * @return int
123 */
124 public function key(){
125 return $this->pointer;
126 }
127
128
129 }
130