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. |
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. |
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. |
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. |
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 +---------------------------------------------------------------------------------+
43 class ActiveMongo_Validators
46 final private static function _hook($action, $method)
48 ActiveMongo
::addEvent($action, array(__CLASS__
, $method));
51 final public static function init()
53 self
::_hook("before_validate_creation", "presence_of_creation");
54 self
::_hook("before_validate_update", "presence_of_update");
55 self
::_hook("before_validate", "length_of");
58 // validates_length_of {{{
59 final static function length_of($class, $obj)
63 if (isset($class::$validates_size_of)) {
64 $validates = $class::$validates_size_of;
65 } else if (isset($class::$validates_length_of)) {
66 $validates = $class::$validates_length_of;
69 foreach ($validates as $property) {
72 if (isset($obj[$name])) {
76 if (isset($obj['$set'][$name])) {
77 $prop = $obj['$set'][$name];
81 if (isset($property['min']) && strlen($prop) < $property['min']) {
82 throw new ActiveMongo_FilterException("{$name} length is too short");
84 if (isset($property['is']) && strlen($prop) != $property['is']) {
85 throw new ActiveMongo_FilterException("{$name} length is different than expected");
87 if (isset($property['max']) && strlen($prop) > $property['max']) {
88 throw new ActiveMongo_FilterException("{$name} length is too large");
95 // validates_presence_of {{{
96 final static function presence_of_creation($class, $obj)
98 if (isset($class::$validates_presence_of)) {
99 foreach ((Array)$class::$validates_presence_of as $property) {
100 if (!isset($obj[$property])) {
101 throw new ActiveMongo_FilterException("Missing required property {$property}");
107 final static function presence_of_update($class, $obj)
109 if (isset($class::$validates_presence_of)) {
110 foreach ((Array)$class::$validates_presence_of as $property) {
111 if (isset($obj['$unset'][$property])) {
112 throw new ActiveMongo_FilterException("Cannot delete required property {$property}");
121 // Register validators
122 ActiveMongo_Validators
::init();
129 * vim600: sw=4 ts=4 fdm=marker