1 <?php
2 /**
3 * @package Joomla.Legacy
4 * @subpackage Base
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Abstract observable class to implement the observer design pattern
14 *
15 * @since 1.5
16 * @deprecated 2.5
17 */
18 class JObservable extends JObject
19 {
20 /**
21 * An array of Observer objects to notify
22 *
23 * @var array
24 * @since 1.5
25 * @deprecated 2.5
26 */
27 protected $_observers = array();
28
29 /**
30 * The state of the observable object
31 *
32 * @var mixed
33 * @since 1.5
34 * @deprecated 2.5
35 */
36 protected $_state = null;
37
38 /**
39 * A multi dimensional array of [function][] = key for observers
40 *
41 * @var array
42 * @since 1.6
43 * @deprecated 2.5
44 */
45 protected $_methods = array();
46
47 /**
48 * Constructor
49 *
50 * Note: Make Sure it's not directly instantiated
51 *
52 * @since 1.5
53 * @deprecated 2.5
54 */
55 public function __construct()
56 {
57 $this->_observers = array();
58 }
59
60 /**
61 * Get the state of the JObservable object
62 *
63 * @return mixed The state of the object.
64 *
65 * @since 1.5
66 * @deprecated 2.5
67 */
68 public function getState()
69 {
70 return $this->_state;
71 }
72
73 /**
74 * Update each attached observer object and return an array of their return values
75 *
76 * @return array Array of return values from the observers
77 *
78 * @since 1.5
79 * @deprecated 2.5
80 */
81 public function notify()
82 {
83 // Iterate through the _observers array
84 foreach ($this->_observers as $observer)
85 {
86 $return[] = $observer->update();
87 }
88
89 return $return;
90 }
91
92 /**
93 * Attach an observer object
94 *
95 * @param object $observer An observer object to attach
96 *
97 * @return void
98 *
99 * @since 1.5
100 * @deprecated 2.5
101 */
102 public function attach($observer)
103 {
104 if (is_array($observer))
105 {
106 if (!isset($observer['handler']) || !isset($observer['event']) || !is_callable($observer['handler']))
107 {
108 return;
109 }
110
111 // Make sure we haven't already attached this array as an observer
112 foreach ($this->_observers as $check)
113 {
114 if (is_array($check) && $check['event'] == $observer['event'] && $check['handler'] == $observer['handler'])
115 {
116 return;
117 }
118 }
119
120 $this->_observers[] = $observer;
121 end($this->_observers);
122 $methods = array($observer['event']);
123 }
124 else
125 {
126 if (!($observer instanceof JObserver))
127 {
128 return;
129 }
130
131 // Make sure we haven't already attached this object as an observer
132 $class = get_class($observer);
133
134 foreach ($this->_observers as $check)
135 {
136 if ($check instanceof $class)
137 {
138 return;
139 }
140 }
141
142 $this->_observers[] = $observer;
143 $methods = array_diff(get_class_methods($observer), get_class_methods('JPlugin'));
144 }
145
146 $key = key($this->_observers);
147
148 foreach ($methods as $method)
149 {
150 $method = strtolower($method);
151
152 if (!isset($this->_methods[$method]))
153 {
154 $this->_methods[$method] = array();
155 }
156
157 $this->_methods[$method][] = $key;
158 }
159 }
160
161 /**
162 * Detach an observer object
163 *
164 * @param object $observer An observer object to detach.
165 *
166 * @return boolean True if the observer object was detached.
167 *
168 * @since 1.5
169 * @deprecated 2.5
170 */
171 public function detach($observer)
172 {
173 $retval = false;
174
175 $key = array_search($observer, $this->_observers);
176
177 if ($key !== false)
178 {
179 unset($this->_observers[$key]);
180 $retval = true;
181
182 foreach ($this->_methods as &$method)
183 {
184 $k = array_search($key, $method);
185
186 if ($k !== false)
187 {
188 unset($method[$k]);
189 }
190 }
191 }
192
193 return $retval;
194 }
195 }
196