6 * @author Michael Wallner <mike@php.net>
10 use hikke\Event\Priority
;
11 use hikke\Event\Storage
;
16 * @package hikke\Event
18 class Event
extends Priority
implements \SplSubject
, \IteratorAggregate
, \Countable
31 * @var hikke\Event\Storage
37 * @param string $name custom event name
38 * @param int|float $priority
40 public function __construct($name, $priority = 0) {
42 $this->priority
= $priority;
43 $this->storage
= new Storage
;
47 * Returns the event name
50 public function __toString() {
51 return (string) $this->name
;
55 * Get priority of this event
58 public function getPriority() {
59 return $this->priority
;
66 public function getName() {
71 * Update priority of this event
72 * @param float $priority
74 public function setPriority($priority) {
75 $this->priority
= $priority;
79 * Attach an event observer
80 * @param \SplObserver $observer
81 * @param float $priority
82 * @return \hikke\Event
84 public function attach(\SplObserver
$observer, $priority = 0) {
85 $this->storage
->insert($observer, $priority);
90 * Detach an already attached event observer
91 * @param \SplObserver $observer
92 * @return bool whether the observer was attached
94 public function detach(\SplObserver
$observer) {
95 return $this->storage
->delete($observer);
99 * Notify attached observers
100 * @param \SplSubject $origin
102 public function notify(\SplSubject
$origin = null) {
103 foreach ($this->storage
as $observer) {
104 $observer->update($origin ?
: $this, $this);
111 public function count() {
112 return count($this->storage
);
118 public function getIterator() {
119 return $this->storage
;