From 2f1badc7fd3e17cb734a03636c1d2fa31189c30a Mon Sep 17 00:00:00 2001 From: romanb Date: Fri, 21 Mar 2008 13:32:10 +0000 Subject: [PATCH] Merged fix for #852 from 0.10 to trunk. --- lib/Doctrine/Locator.php | 197 ------------------ lib/Doctrine/Locator/Injectable.php | 149 ------------- lib/Doctrine/Validator.php | 10 +- .../Exception.php => Validator/Time.php} | 98 +++++---- lib/Doctrine/Validator/Timestamp.php | 66 ++++++ 5 files changed, 135 insertions(+), 385 deletions(-) delete mode 100644 lib/Doctrine/Locator.php delete mode 100644 lib/Doctrine/Locator/Injectable.php rename lib/Doctrine/{Locator/Exception.php => Validator/Time.php} (50%) create mode 100644 lib/Doctrine/Validator/Timestamp.php diff --git a/lib/Doctrine/Locator.php b/lib/Doctrine/Locator.php deleted file mode 100644 index 8b2ba9822..000000000 --- a/lib/Doctrine/Locator.php +++ /dev/null @@ -1,197 +0,0 @@ -. - */ - -/** - * Doctrine_Locator - * - * @package Doctrine - * @subpackage Doctrine_Locator - * @category Locator - * @license http://www.gnu.org/licenses/lgpl.txt LGPL - * @link http://www.phpdoctrine.org - * @author Janne Vanhala - * @author Konsta Vesterinen - * @author Eevert Saukkokoski - * @version $Revision$ - * @since 1.0 - */ -class Doctrine_Locator implements Countable, IteratorAggregate -{ - /** - * @var array $_resources an array of bound resources - */ - protected $_resources = array(); - - /** - * @var string $_classPrefix the default class prefix - */ - protected $_classPrefix = 'Doctrine_'; - - /** - * @var array $_instances a pool of this object's instances - */ - protected static $_instances = array(); - - /** - * Constructor. Provide an array of resources to set initial contents. - * - * @param array - * @return void - */ - public function __construct(array $defaults = null) - { - if (null !== $defaults) { - foreach ($defaults as $name => $resource) { - if ($resource instanceof Doctrine_Locator_Injectable) { - $resource->setLocator($this); - } - $this->_resources[$name] = $resource; - } - } - self::$_instances[] = $this; - } - - /** - * instance - * - * @return Sensei_Locator - */ - public static function instance() - { - if (empty(self::$_instances)) { - $obj = new Doctrine_Locator(); - } - return current(self::$_instances); - } - - /** - * setClassPrefix - * - * @param string $prefix - */ - public function setClassPrefix($prefix) - { - $this->_classPrefix = $prefix; - } - - /** - * getClassPrefix - * - * @return string - */ - public function getClassPrefix() - { - return $this->_classPrefix; - } - - /** - * contains - * checks if a resource exists under the given name - * - * @return boolean whether or not given resource name exists - */ - public function contains($name) - { - return isset($this->_resources[$name]); - } - - /** - * bind - * binds a resource to a name - * - * @param string $name the name of the resource to bind - * @param mixed $value the value of the resource - * @return Sensei_Locator this object - */ - public function bind($name, $value) - { - $this->_resources[$name] = $value; - - return $this; - } - - /** - * locate - * locates a resource by given name and returns it - * - * @throws Doctrine_Locator_Exception if the resource could not be found - * @param string $name the name of the resource - * @return mixed the located resource - */ - public function locate($name) - { - if (isset($this->_resources[$name])) { - return $this->_resources[$name]; - } else { - $className = $name; - - if ( ! class_exists($className)) { - - $name = explode('.', $name); - $name = array_map('strtolower', $name); - $name = array_map('ucfirst', $name); - $name = implode('_', $name); - - $className = $this->_classPrefix . $name; - - if ( ! class_exists($className)) { - throw new Doctrine_Locator_Exception("Couldn't locate resource " . $className); - } - } - - $this->_resources[$name] = new $className(); - - if ($this->_resources[$name] instanceof Doctrine_Locator_Injectable) { - $this->_resources[$name]->setLocator($this); - } - - return $this->_resources[$name]; - } - - throw new Doctrine_Locator_Exception("Couldn't locate resource " . $name); - } - - /** - * count - * returns the number of bound resources associated with - * this object - * - * @see Countable interface - * @return integer the number of resources - */ - public function count() - { - return count($this->_resources); - } - - /** - * getIterator - * returns an ArrayIterator that iterates through all - * bound resources - * - * @return ArrayIterator an iterator for iterating through - * all bound resources - */ - public function getIterator() - { - return new ArrayIterator($this->_resources); - } -} diff --git a/lib/Doctrine/Locator/Injectable.php b/lib/Doctrine/Locator/Injectable.php deleted file mode 100644 index 289104baf..000000000 --- a/lib/Doctrine/Locator/Injectable.php +++ /dev/null @@ -1,149 +0,0 @@ -. - */ - -/** - * Doctrine_Locator_Injectable - * - * @package Doctrine - * @subpackage Doctrine_Locator - * @category Locator - * @license http://www.gnu.org/licenses/lgpl.txt LGPL - * @link http://www.phpdoctrine.org - * @author Janne Vanhala - * @author Konsta Vesterinen - * @author Eevert Saukkokoski - * @version $Revision$ - * @since 1.0 - */ -class Doctrine_Locator_Injectable -{ - /** - * @var Doctrine_Locator the locator object - */ - protected $_locator; - - /** - * @var array an array of bound resources - */ - protected $_resources = array(); - - /** - * @var Doctrine_Null $null Doctrine_Null object, used for extremely fast null value checking - */ - protected static $_null; - - /** - * setLocator - * this method can be used for setting the locator object locally - * - * @param Doctrine_Locator the locator object - * @return Doctrine_Locator_Injectable this instance - */ - public function setLocator(Doctrine_Locator $locator) - { - $this->_locator = $locator; - return $this; - } - - /** - * getLocator - * returns the locator associated with this object - * - * if there are no locator locally associated then - * this method tries to fetch the current global locator - * - * @return Doctrine_Locator - */ - public function getLocator() - { - if ( ! isset($this->_locator)) { - $this->_locator = Doctrine_Locator::instance(); - - } - return $this->_locator; - } - - /** - * locate - * locates a resource by given name and returns it - * - * if the resource cannot be found locally this method tries - * to use the global locator for finding the resource - * - * @see Doctrine_Locator::locate() - * @throws Doctrine_Locator_Exception if the resource could not be found - * @param string $name the name of the resource - * @return mixed the located resource - */ - public function locate($name) - { - if (isset($this->_resources[$name])) { - if (is_object($this->_resources[$name])) { - return $this->_resources[$name]; - } else { - // get the name of the concrete implementation - $concreteImpl = $this->_resources[$name]; - - return $this->getLocator()->locate($concreteImpl); - } - } else { - return $this->getLocator()->locate($name); - } - } - - /** - * bind - * binds a resource to a name - * - * @param string $name the name of the resource to bind - * @param mixed $value the value of the resource - * @return Doctrine_Locator this object - */ - public function bind($name, $resource) - { - $this->_resources[$name] = $resource; - - return $this; - } - - /** - * initNullObject - * initializes the null object - * - * @param Doctrine_Null $null - * @return void - */ - public static function initNullObject(Doctrine_Null $null) - { - self::$_null = $null; - } - - /** - * getNullObject - * returns the null object associated with this object - * - * @return Doctrine_Null - */ - public static function getNullObject() - { - return self::$_null; - } -} diff --git a/lib/Doctrine/Validator.php b/lib/Doctrine/Validator.php index da4f9bd11..c69877518 100644 --- a/lib/Doctrine/Validator.php +++ b/lib/Doctrine/Validator.php @@ -173,6 +173,7 @@ class Doctrine_Validator switch ($type) { case 'float': case 'double': + case 'decimal': return (string)$var == strval(floatval($var)); case 'integer': return (string)$var == strval(intval($var)); @@ -189,8 +190,11 @@ class Doctrine_Validator case 'boolean': return is_bool($var); case 'timestamp': - // todo: validate the timestamp is in YYYY-MM-DD HH:MM:SS format - return true; + $validator = self::getValidator('timestamp'); + return $validator->validate($var); + case 'time': + $validator = self::getValidator('time'); + return $validator->validate($var); case 'date': $validator = self::getValidator('date'); return $validator->validate($var); @@ -200,4 +204,4 @@ class Doctrine_Validator return false; } } -} +} \ No newline at end of file diff --git a/lib/Doctrine/Locator/Exception.php b/lib/Doctrine/Validator/Time.php similarity index 50% rename from lib/Doctrine/Locator/Exception.php rename to lib/Doctrine/Validator/Time.php index 344981d1d..1f9fd4115 100644 --- a/lib/Doctrine/Locator/Exception.php +++ b/lib/Doctrine/Validator/Time.php @@ -1,36 +1,62 @@ -. - */ - -/** - * Doctrine_Locator_Exception - * - * @package Doctrine - * @subpackage Doctrine_Locator - * @category Locator - * @license http://www.gnu.org/licenses/lgpl.txt LGPL - * @link http://www.phpdoctrine.org - * @author Janne Vanhala - * @author Konsta Vesterinen - * @version $Revision$ - * @since 1.0 - */ -class Doctrine_Locator_Exception extends Doctrine_Exception -{ } +. + */ + +/** + * Doctrine_Validator_Time + * + * @package Doctrine + * @subpackage Validator + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 3884 $ + * @author Mark Pearson + */ +class Doctrine_Validator_Time +{ + /** + * checks if given value is a valid time + * + * @param mixed $value + * @return boolean + */ + public function validate($value) + { + if ($value === null) { + return true; + } + $e = explode(':', $value); + + if (count($e) !== 3) { + return false; + } + + if (!preg_match('/^ *[0-9]{2}:[0-9]{2}:[0-9]{2} *$/', $value)) { + return false; + } + + $hr = intval($e[0], 10); + $min = intval($e[1], 10); + $sec = intval($e[2], 10); + + return $hr >= 0 && $hr <= 23 && $min >= 0 && $min <= 59 && $sec >= 0 && $sec <= 59; + } +} \ No newline at end of file diff --git a/lib/Doctrine/Validator/Timestamp.php b/lib/Doctrine/Validator/Timestamp.php new file mode 100644 index 000000000..a5ca739e4 --- /dev/null +++ b/lib/Doctrine/Validator/Timestamp.php @@ -0,0 +1,66 @@ +. + */ + +/** + * Doctrine_Validator_Timestamp + * + * @package Doctrine + * @subpackage Validator + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 3884 $ + * @author Mark Pearson + */ +class Doctrine_Validator_Timestamp +{ + /** + * checks if given value is a valid timestamp (YYYY-MM-DD HH:MM:SS) + * + * @param mixed $value + * @return boolean + */ + public function validate($value) + { + if ($value === null) { + return true; + } + + if (!preg_match('/^ *[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} *$/', $value)) { + return false; + } + + list($date, $time) = explode(' ', trim($value)); + + $dateValidator = Doctrine_Validator::getValidator('date'); + $timeValidator = Doctrine_Validator::getValidator('time'); + + if (!$dateValidator->validate($date)) { + return false; + } + + if (!$timeValidator->validate($time)) { + return false; + } + + return true; + } +} \ No newline at end of file