- Added Validators
[activemongo.git] / lib / Validators.php
blob7849e802c952b25d2bc5ba078462b02440941cdd
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 // Valid Presence Of {{{
39 ActiveMongo_Events::addEvent("before_validate_creation", function ($class, $obj) {
40 if (isset($class::$validates_presence_of)) {
41 foreach ((Array)$class::$validates_presence_of as $property) {
42 if (!isset($obj[$property])) {
43 throw new FilterException("Missing required property {$property}");
47 });
49 ActiveMongo_Events::addEvent("before_validate_update", function ($class, $obj) {
50 if (isset($class::$validates_presence_of)) {
51 foreach ((Array)$class::$validates_presence_of as $property) {
52 if (isset($obj['$unset'][$property])) {
53 throw new FilterException("Cannot delete required property {$property}");
57 });
58 // }}}
60 // Valid Size Of / Valid Length Of {{{
61 ActiveMongo_Events::addEvent("before_validate", function ($class, $obj) {
62 $validates = array();
64 if (isset($class::$validates_size_of)) {
65 $validates = $class::$validates_size_of;
66 } else if (isset($class::$validates_length_of)) {
67 $validates = $class::$validates_length_of;
70 foreach ($validates as $property) {
71 $name = $property[0];
73 if (isset($obj[$name])) {
74 $prop = $obj[$name];
77 if (isset($obj['$set'][$name])) {
78 $prop = $obj['$set'][$name];
81 if (isset($prop)) {
82 if (isset($property['min']) && strlen($prop) < $property['min']) {
83 throw new FilterException("{$name} length is too short");
85 if (isset($property['is']) && strlen($prop) != $property['is']) {
86 throw new FilterException("{$name} length is different than expected");
88 if (isset($property['max']) && strlen($prop) > $property['max']) {
89 throw new FilterException("{$name} length is too large");
93 });
94 // }}}
97 * Local variables:
98 * tab-width: 4
99 * c-basic-offset: 4
100 * End:
101 * vim600: sw=4 ts=4 fdm=marker
102 * vim<600: sw=4 ts=4