- Added Validators
[activemongo.git] / lib / Events.php
blobb5e7613d16b1a510325a539cc74a95b25453cd4a
1 <?php
2 /*
3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 ActiveMongo |
5 +---------------------------------------------------------------------------------+
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | 1. Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
10 | |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | 3. All advertising materials mentioning features or use of this software |
16 | must display the following acknowledgement: |
17 | This product includes software developed by César D. Rodas. |
18 | |
19 | 4. Neither the name of the César D. Rodas nor the |
20 | names of its contributors may be used to endorse or promote products |
21 | derived from this software without specific prior written permission. |
22 | |
23 | THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
24 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 | DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
27 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
28 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
29 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
30 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
33 +---------------------------------------------------------------------------------+
34 | Authors: César Rodas <crodas@php.net> |
35 +---------------------------------------------------------------------------------+
38 require_once dirname(__FILE__)."/Validators.php";
40 /**
41 * This class manages the Events and Filterings
44 class ActiveMongo_Events
46 static private $_events = array();
47 static private $_super_events = array();
49 // addEvent($action, $callback) {{{
50 /**
51 * addEvent
54 final static function addEvent($action, $callback)
56 if (!is_callable($callback)) {
57 throw new Exception("Invalid callback");
60 $class = get_called_class();
61 if ($class == __CLASS__ || $class == 'ActiveMongo') {
62 $events = & self::$_super_events;
63 } else {
64 $events = & self::$_events[$class];
66 if (!isset($events[$action])) {
67 $events[$action] = array();
69 $events[$action][] = $callback;
70 return true;
72 // }}}
74 // triggerEvent(string $event, Array $events_params) {{{
75 final function triggerEvent($event, Array $events_params = array())
77 $events = & self::$_events[get_class($this)][$event];
78 $sevents = & self::$_super_events[$event];
80 if (!is_array($events_params)) {
81 return false;
84 /* Super-Events handler receives the ActiveMongo class name as first param */
85 $sevents_params = array_merge(array(get_class($this)), $events_params);
87 foreach (array('events', 'sevents') as $event_type) {
88 if (count($$event_type) > 0) {
89 $params = "{$event_type}_params";
90 foreach ($$event_type as $fnc) {
91 call_user_func_array($fnc, $$params);
96 /* Some natives events are allowed to be called
97 * as methods, if they exists
99 switch ($event) {
100 case 'before_create':
101 case 'before_update':
102 case 'before_validate':
103 case 'before_delete':
104 case 'after_create':
105 case 'after_update':
106 case 'after_validate':
107 case 'after_delete':
108 $fnc = array($this, $event);
109 $params = "events_params";
110 if (is_callable($fnc)) {
111 call_user_func_array($fnc, $$params);
113 break;
116 // }}}
118 // void runFilter(string $key, mixed &$value, mixed $past_value) {{{
120 * *Internal Method*
122 * This method check if the current document property has
123 * a filter method, if so, call it.
125 * If the filter returns false, throw an Exception.
127 * @return void
129 protected function runFilter($key, &$value, $past_value)
131 $filter = array($this, "{$key}_filter");
132 if (is_callable($filter)) {
133 $filter = call_user_func_array($filter, array(&$value, $past_value));
134 if ($filter===false) {
135 throw new FilterException("{$key} filter failed");
137 $this->$key = $value;
140 // }}}
145 * Local variables:
146 * tab-width: 4
147 * c-basic-offset: 4
148 * End:
149 * vim600: sw=4 ts=4 fdm=marker
150 * vim<600: sw=4 ts=4