2006-12-30 00:34:26 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Doctrine_Validator
|
|
|
|
* Doctrine_Validator performs validations in record properties
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
*/
|
2007-06-28 15:56:56 +04:00
|
|
|
class Doctrine_Validator extends Doctrine_Object
|
2006-12-30 00:34:26 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array $validators an array of validator objects
|
|
|
|
*/
|
|
|
|
private static $validators = array();
|
|
|
|
/**
|
|
|
|
* returns a validator object
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return Doctrine_Validator_Interface
|
|
|
|
*/
|
|
|
|
public static function getValidator($name)
|
|
|
|
{
|
|
|
|
if ( ! isset(self::$validators[$name])) {
|
2007-01-27 13:08:06 +03:00
|
|
|
$class = 'Doctrine_Validator_' . ucwords(strtolower($name));
|
2006-12-30 00:34:26 +03:00
|
|
|
if (class_exists($class)) {
|
|
|
|
self::$validators[$name] = new $class;
|
|
|
|
} else {
|
|
|
|
throw new Doctrine_Exception("Validator named '$name' not availible.");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return self::$validators[$name];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* validates a given record and saves possible errors
|
|
|
|
* in Doctrine_Validator::$stack
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function validateRecord(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
$columns = $record->getTable()->getColumns();
|
|
|
|
$component = $record->getTable()->getComponentName();
|
|
|
|
|
|
|
|
$errorStack = $record->getErrorStack();
|
|
|
|
|
|
|
|
// if record is transient all fields will be validated
|
|
|
|
// if record is persistent only the modified fields will be validated
|
|
|
|
$data = ($record->exists()) ? $record->getModified() : $record->getData();
|
|
|
|
|
|
|
|
$err = array();
|
|
|
|
foreach ($data as $key => $value) {
|
2007-06-28 15:56:56 +04:00
|
|
|
if ($value === self::$_null) {
|
2006-12-30 00:34:26 +03:00
|
|
|
$value = null;
|
2007-06-28 15:56:56 +04:00
|
|
|
} elseif ($value instanceof Doctrine_Record) {
|
2006-12-30 00:34:26 +03:00
|
|
|
$value = $value->getIncremented();
|
2007-06-28 15:56:56 +04:00
|
|
|
}
|
2006-12-30 00:34:26 +03:00
|
|
|
|
|
|
|
$column = $columns[$key];
|
|
|
|
|
2007-06-28 15:56:56 +04:00
|
|
|
if ($column['type'] == 'enum') {
|
2006-12-30 00:34:26 +03:00
|
|
|
$value = $record->getTable()->enumIndex($key, $value);
|
|
|
|
|
|
|
|
if ($value === false) {
|
|
|
|
$errorStack->add($key, 'enum');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) {
|
|
|
|
if (!$this->validateLength($column, $key, $value)) {
|
|
|
|
$errorStack->add($key, 'length');
|
2007-02-17 15:38:02 +03:00
|
|
|
|
2006-12-30 00:34:26 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-28 15:56:56 +04:00
|
|
|
foreach ($column as $name => $args) {
|
2006-12-30 00:34:26 +03:00
|
|
|
if (empty($name)
|
|
|
|
|| $name == 'primary'
|
|
|
|
|| $name == 'protected'
|
|
|
|
|| $name == 'autoincrement'
|
|
|
|
|| $name == 'default'
|
2007-01-30 00:24:18 +03:00
|
|
|
|| $name == 'values'
|
|
|
|
|| $name == 'sequence'
|
2007-06-28 15:56:56 +04:00
|
|
|
|| $name == 'zerofill') {
|
2006-12-30 00:34:26 +03:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-04 00:57:07 +04:00
|
|
|
|
|
|
|
if (strtolower($name) === 'notnull' && isset($column['autoincrement'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-12-30 00:34:26 +03:00
|
|
|
if (strtolower($name) == 'length') {
|
|
|
|
if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) {
|
|
|
|
if (!$this->validateLength($column, $key, $value)) {
|
|
|
|
$errorStack->add($key, 'length');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strtolower($name) == 'type') {
|
|
|
|
if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) {
|
2007-06-28 15:56:56 +04:00
|
|
|
if ( ! self::isValidType($value, $column['type'])) {
|
2006-12-30 00:34:26 +03:00
|
|
|
$errorStack->add($key, 'type');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = self::getValidator($name);
|
|
|
|
if ( ! $validator->validate($record, $key, $value, $args)) {
|
|
|
|
$errorStack->add($key, $name);
|
|
|
|
|
|
|
|
//$err[$key] = 'not valid';
|
|
|
|
|
|
|
|
// errors found quit validation looping for this column
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) {
|
2007-06-28 15:56:56 +04:00
|
|
|
if ( ! self::isValidType($value, $column['type'])) {
|
2006-12-30 00:34:26 +03:00
|
|
|
$errorStack->add($key, 'type');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2007-01-19 23:47:24 +03:00
|
|
|
* Validates the length of a field.
|
2006-12-30 00:34:26 +03:00
|
|
|
*/
|
|
|
|
private function validateLength($column, $key, $value)
|
|
|
|
{
|
2007-06-28 15:56:56 +04:00
|
|
|
if ($column['type'] == 'timestamp' || $column['type'] == 'integer') {
|
2006-12-30 00:34:26 +03:00
|
|
|
return true;
|
2007-06-28 15:56:56 +04:00
|
|
|
} elseif ($column['type'] == 'array' || $column['type'] == 'object') {
|
2006-12-30 00:34:26 +03:00
|
|
|
$length = strlen(serialize($value));
|
|
|
|
} else {
|
|
|
|
$length = strlen($value);
|
|
|
|
}
|
|
|
|
|
2007-06-28 15:56:56 +04:00
|
|
|
if ($length > $column['length']) {
|
2006-12-30 00:34:26 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* whether or not this validator has errors
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasErrors()
|
|
|
|
{
|
|
|
|
return (count($this->stack) > 0);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* converts a doctrine type to native php type
|
|
|
|
*
|
|
|
|
* @param $doctrineType
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function phpType($doctrineType)
|
|
|
|
{
|
|
|
|
switch ($doctrineType) {
|
|
|
|
case 'enum':
|
|
|
|
return 'integer';
|
|
|
|
case 'blob':
|
|
|
|
case 'clob':
|
|
|
|
case 'mbstring':
|
|
|
|
case 'timestamp':
|
|
|
|
case 'date':
|
|
|
|
case 'gzip':
|
|
|
|
return 'string';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return $doctrineType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns whether or not the given variable is
|
|
|
|
* valid type
|
|
|
|
*
|
|
|
|
* @param mixed $var
|
|
|
|
* @param string $type
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function isValidType($var, $type)
|
|
|
|
{
|
2007-06-28 15:56:56 +04:00
|
|
|
if ($type == 'boolean') {
|
2006-12-30 00:34:26 +03:00
|
|
|
return true;
|
2007-06-28 15:56:56 +04:00
|
|
|
}
|
2006-12-30 00:34:26 +03:00
|
|
|
|
|
|
|
$looseType = self::gettype($var);
|
|
|
|
$type = self::phpType($type);
|
|
|
|
|
|
|
|
switch ($looseType) {
|
|
|
|
case 'float':
|
|
|
|
case 'double':
|
|
|
|
case 'integer':
|
2007-06-28 15:56:56 +04:00
|
|
|
if ($type == 'string' || $type == 'float') {
|
2006-12-30 00:34:26 +03:00
|
|
|
return true;
|
2007-06-28 15:56:56 +04:00
|
|
|
}
|
2006-12-30 00:34:26 +03:00
|
|
|
case 'string':
|
|
|
|
case 'array':
|
|
|
|
case 'object':
|
|
|
|
return ($type === $looseType);
|
|
|
|
break;
|
|
|
|
case 'NULL':
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the type of loosely typed variable
|
|
|
|
*
|
|
|
|
* @param mixed $var
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function gettype($var)
|
|
|
|
{
|
|
|
|
$type = gettype($var);
|
|
|
|
switch ($type) {
|
|
|
|
case 'string':
|
|
|
|
if (preg_match("/^[0-9]+$/",$var)) {
|
|
|
|
return 'integer';
|
|
|
|
} elseif (is_numeric($var)) {
|
|
|
|
return 'float';
|
|
|
|
} else {
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return $type;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|