Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Validator.php 7490 2010-03-29 19:53:27Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * This class is responsible for performing all validations on record properties
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Validator
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: 7490 $
31
 * @author      Roman Borschel <roman@code-factory.org>
32
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
33
 */
34
class Doctrine_Validator extends Doctrine_Locator_Injectable
35
{
36
    /**
37
     * @var array $validators           an array of validator objects
38
     */
39
    private static $validators = array();
40
 
41
    /**
42
     * Get a validator instance for the passed $name
43
     *
44
     * @param  string   $name  Name of the validator or the validator class name
45
     * @return Doctrine_Validator_Interface $validator
46
     */
47
    public static function getValidator($name)
48
    {
49
        if ( ! isset(self::$validators[$name])) {
50
            $class = 'Doctrine_Validator_' . ucwords(strtolower($name));
51
            if (class_exists($class)) {
52
                self::$validators[$name] = new $class;
53
            } else if (class_exists($name)) {
54
                self::$validators[$name] = new $name;
55
            } else {
56
                throw new Doctrine_Exception("Validator named '$name' not available.");
57
            }
58
 
59
        }
60
        return self::$validators[$name];
61
    }
62
 
63
    /**
64
     * Validates a given record and saves possible errors in Doctrine_Validator::$stack
65
     *
66
     * @param  Doctrine_Record $record
67
     * @return void
68
     */
69
    public function validateRecord(Doctrine_Record $record)
70
    {
71
        $table = $record->getTable();
72
 
73
        // if record is transient all fields will be validated
74
        // if record is persistent only the modified fields will be validated
75
        $fields = $record->exists() ? $record->getModified():$record->getData();
76
        foreach ($fields as $fieldName => $value) {
77
            $table->validateField($fieldName, $value, $record);
78
        }
79
        $table->validateUniques($record);
80
    }
81
 
82
    /**
83
     * Validates the length of a field.
84
     *
85
     * @param  string  $value         Value to validate
86
     * @param  string  $type          Type of field being validated
87
     * @param  string  $maximumLength Maximum length allowed for the column
88
     * @return boolean $success       True/false for whether the value passed validation
89
     */
90
    public static function validateLength($value, $type, $maximumLength)
91
    {
92
        if ($maximumLength === null ) {
93
            return true;
94
        }
95
        if ($type == 'timestamp' || $type == 'integer' || $type == 'enum') {
96
            return true;
97
        } else if ($type == 'array' || $type == 'object') {
98
            $length = strlen(serialize($value));
99
        } else if ($type == 'decimal' || $type == 'float') {
100
            $value = abs($value);
101
 
102
            $localeInfo = localeconv();
103
            $decimalPoint = $localeInfo['mon_decimal_point'] ? $localeInfo['mon_decimal_point'] : $localeInfo['decimal_point'];
104
            $e = explode($decimalPoint, $value);
105
 
106
            $length = strlen($e[0]);
107
 
108
            if (isset($e[1])) {
109
                $length = $length + strlen($e[1]);
110
            }
111
        } else if ($type == 'blob') {
112
            $length = strlen($value);
113
        } else {
114
            $length = self::getStringLength($value);
115
        }
116
        if ($length > $maximumLength) {
117
            return false;
118
        }
119
        return true;
120
    }
121
 
122
    /**
123
     * Get length of passed string. Will use multibyte character functions if they exist
124
     *
125
     * @param string $string
126
     * @return integer $length
127
     */
128
    public static function getStringLength($string)
129
    {
130
        if (function_exists('mb_strlen')) {
131
            return mb_strlen($string, 'utf8');
132
        } else {
133
            return strlen(utf8_decode($string));
134
        }
135
    }
136
 
137
    /**
138
     * Whether or not errors exist on this validator
139
     *
140
     * @return boolean True/false for whether or not this validate instance has error
141
     */
142
    public function hasErrors()
143
    {
144
        return (count($this->stack) > 0);
145
    }
146
 
147
    /**
148
     * Validate the type of the passed variable
149
     *
150
     * @param  mixed  $var   Variable to validate
151
     * @param  string $type  Type of the variable expected
152
     * @return boolean
153
     */
154
     public static function isValidType($var, $type)
155
     {
156
         if ($var instanceof Doctrine_Expression) {
157
             return true;
158
         } else if ($var === null) {
159
             return true;
160
         } else if (is_object($var)) {
161
             return $type == 'object';
162
         }
163
 
164
         switch ($type) {
165
             case 'float':
166
             case 'double':
167
             case 'decimal':
168
                 return (string) $var == strval(floatval($var));
169
             case 'integer':
170
                 return (string) $var == strval(round(floatval($var)));
171
             case 'string':
172
                 return is_string($var) || is_numeric($var);
173
             case 'blob':
174
                 return is_string($var) || is_resource($var);
175
             case 'clob':
176
             case 'gzip':
177
                 return is_string($var);
178
             case 'array':
179
                 return is_array($var);
180
             case 'object':
181
                 return is_object($var);
182
             case 'boolean':
183
                 return is_bool($var) || (is_numeric($var) && ($var == 0 || $var == 1));
184
             case 'timestamp':
185
                 $validator = self::getValidator('timestamp');
186
                 return $validator->validate($var);
187
             case 'time':
188
                 $validator = self::getValidator('time');
189
                 return $validator->validate($var);
190
             case 'date':
191
                 $validator = self::getValidator('date');
192
                 return $validator->validate($var);
193
             case 'enum':
194
                 return is_string($var) || is_int($var);
195
             case 'set':
196
                 return is_array($var) || is_string($var);
197
             default:
198
                 return true;
199
         }
200
     }
201
}