Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / RedBeanPHP / Observable.php
1 <?php
2
3 namespace RedBeanPHP;
4
5 use RedBeanPHP\Observer as Observer;
6
7 /**
8  * Observable
9  * Base class for Observables
10  *
11  * @file            RedBeanPHP/Observable.php
12  * @author          Gabor de Mooij and the RedBeanPHP community
13  * @license         BSD/GPLv2
14  *
15  * @copyright
16  * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
17  * This source file is subject to the BSD/GPLv2 License that is bundled
18  * with this source code in the file license.txt.
19  */
20 abstract class Observable { //bracket must be here - otherwise coverage software does not understand.
21
22         /**
23          * @var array
24          */
25         private $observers = array();
26
27         /**
28          * Implementation of the Observer Pattern.
29          * Adds an event listener to the observable object.
30          * First argument should be the name of the event you wish to listen for.
31          * Second argument should be the object that wants to be notified in case
32          * the event occurs.
33          *
34          * @param string   $eventname event identifier
35          * @param Observer $observer  observer instance
36          *
37          * @return void
38          */
39         public function addEventListener( $eventname, Observer $observer )
40         {
41                 if ( !isset( $this->observers[$eventname] ) ) {
42                         $this->observers[$eventname] = array();
43                 }
44
45                 foreach ( $this->observers[$eventname] as $o ) {
46                         if ( $o == $observer ) {
47                                 return;
48                         }
49                 }
50
51                 $this->observers[$eventname][] = $observer;
52         }
53
54         /**
55          * Notifies listeners.
56          * Sends the signal $eventname, the event identifier and a message object
57          * to all observers that have been registered to receive notification for
58          * this event. Part of the observer pattern implementation in RedBeanPHP.
59          *
60          * @param string $eventname event you want signal
61          * @param mixed  $info      message object to send along
62          *
63          * @return void
64          */
65         public function signal( $eventname, $info )
66         {
67                 if ( !isset( $this->observers[$eventname] ) ) {
68                         $this->observers[$eventname] = array();
69                 }
70
71                 foreach ( $this->observers[$eventname] as $observer ) {
72                         $observer->onEvent( $eventname, $info );
73                 }
74         }
75 }