1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace PEIP\Event;
|
4
|
|
|
|
5
|
|
|
/*
|
6
|
|
|
* This file is part of the PEIP package.
|
7
|
|
|
* (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
|
8
|
|
|
*
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE
|
10
|
|
|
* file that was distributed with this source code.
|
11
|
|
|
*/
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* Observable
|
15
|
|
|
*
|
16
|
|
|
* @author Timo Michna <timomichna/yahoo.de>
|
17
|
|
|
* @package PEIP
|
18
|
|
|
* @subpackage event
|
19
|
|
|
* @implements \PEIP\INF\Event\Observable
|
20
|
|
|
*/
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class Observable implements \PEIP\INF\Event\Observable {
|
26
|
|
|
|
27
|
|
|
protected $observedObject;
|
28
|
|
|
|
29
|
|
|
protected $observers = array();
|
30
|
|
|
|
31
|
|
|
protected $hasChanged = false;
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @access public
|
36
|
|
|
* @param $observedObject
|
37
|
|
|
* @return
|
38
|
|
|
*/
|
39
|
|
|
public function __construct($observedObject){
|
40
|
|
|
$this->setObserved($observedObject);
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
protected function setObserved($observedObject){
|
44
|
|
|
if(!is_object($observedObject)){
|
45
|
|
|
throw new \InvalidArgumentException("$observedObject must be an object. ".gettype($var)." given.");
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
$this->observedObject = $observedObject;
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* @access public
|
53
|
|
|
* @param $observer
|
54
|
|
|
* @return
|
55
|
|
|
*/
|
56
|
|
|
public function addObserver(\PEIP\INF\Event\Observer $observer){
|
57
|
|
|
$this->observers[] = $observer;
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/**
|
62
|
|
|
* @access public
|
63
|
|
|
* @param $observer
|
64
|
|
|
* @return
|
65
|
|
|
*/
|
66
|
|
|
public function deleteObserver(\PEIP\INF\Event\Observer $observer){
|
67
|
|
|
foreach($this->observers as $key=>$obs){
|
68
|
|
|
if($obs == $observer){
|
69
|
|
|
unset($this->observers[$key]);
|
70
|
|
|
return true;
|
71
|
|
|
}
|
72
|
|
|
}
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* @access public
|
78
|
|
|
* @param $arguments
|
79
|
|
|
* @return
|
80
|
|
|
*/
|
81
|
|
|
public function notifyObservers(array $arguments = array()){
|
82
|
|
|
if($this->hasChanged()){
|
83
|
|
|
foreach($this->observers as $observer){
|
84
|
|
|
$observer->update($this->observedObject);
|
85
|
|
|
}
|
86
|
|
|
}
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* @access public
|
92
|
|
|
* @return
|
93
|
|
|
*/
|
94
|
|
|
public function countObservers(){
|
95
|
|
|
return count($this->obeservers);
|
|
|
|
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/**
|
100
|
|
|
* @access public
|
101
|
|
|
* @return
|
102
|
|
|
*/
|
103
|
|
|
public function hasChanged(){
|
104
|
|
|
return $this->hasChanged();
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* @access public
|
110
|
|
|
* @return
|
111
|
|
|
*/
|
112
|
|
|
public function setChanged(){
|
113
|
|
|
$this->hasChanged = true;
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/**
|
118
|
|
|
* @access public
|
119
|
|
|
* @return
|
120
|
|
|
*/
|
121
|
|
|
public function clearChanged(){
|
122
|
|
|
$this->hasChanged = true;
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
} |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.