1 <?php
 2 /**
 3  * @package     FrameworkOnFramework
 4  * @subpackage  utils
 5  * @copyright   Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
 6  * @license     GNU General Public License version 2 or later; see LICENSE.txt
 7  */
 8 
 9 defined('FOF_INCLUDED') or die;
10 
11 /**
12  * Defines an observable event.
13  *
14  * This class is based on JEvent as found in Joomla! 3.2.0
15  */
16 abstract class FOFUtilsObservableEvent extends FOFUtilsObject
17 {
18     /**
19      * Event object to observe.
20      *
21      * @var    object
22      */
23     protected $_subject = null;
24 
25     /**
26      * Constructor
27      *
28      * @param   object  &$subject  The object to observe.
29      */
30     public function __construct(&$subject)
31     {
32         // Register the observer ($this) so we can be notified
33         $subject->attach($this);
34 
35         // Set the subject to observe
36         $this->_subject = &$subject;
37     }
38 
39     /**
40      * Method to trigger events.
41      * The method first generates the even from the argument array. Then it unsets the argument
42      * since the argument has no bearing on the event handler.
43      * If the method exists it is called and returns its return value. If it does not exist it
44      * returns null.
45      *
46      * @param   array  &$args  Arguments
47      *
48      * @return  mixed  Routine return value
49      */
50     public function update(&$args)
51     {
52         // First let's get the event from the argument array.  Next we will unset the
53         // event argument as it has no bearing on the method to handle the event.
54         $event = $args['event'];
55         unset($args['event']);
56 
57         /*
58          * If the method to handle an event exists, call it and return its return
59          * value.  If it does not exist, return null.
60          */
61         if (method_exists($this, $event))
62         {
63             return call_user_func_array(array($this, $event), $args);
64         }
65         else
66         {
67             return null;
68         }
69     }
70 }
71