cvs: pear /DB_DataObject/DataObject Cast.php

From: Date: Mon, 28 Feb 2005 09:54:08 +0000
Subject: cvs: pear /DB_DataObject/DataObject Cast.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-29923@lists.php.net to get a copy of this message
alan_k Mon Feb 28 04:54:08 2005 EDT Modified files: /pear/DB_DataObject/DataObject Cast.php Log: add datetime and time support to cast (therion_5 at hotmail), fixes bug #3584 http://cvs.php.net/diff.php/pear/DB_DataObject/DataObject/Cast.php?r1=1.7&r2=1.8&ty=u Index: pear/DB_DataObject/DataObject/Cast.php diff -u pear/DB_DataObject/DataObject/Cast.php:1.7 pear/DB_DataObject/DataObject/Cast.php:1.8 --- pear/DB_DataObject/DataObject/Cast.php:1.7 Mon Feb 14 20:51:24 2005 +++ pear/DB_DataObject/DataObject/Cast.php Mon Feb 28 04:54:07 2005 @@ -16,7 +16,7 @@ // | Authors: Alan Knowles <alan@akbkhome.com> | // +----------------------------------------------------------------------+ // -// $Id: Cast.php,v 1.7 2005/02/15 01:51:24 alan_k Exp $ +// $Id: Cast.php,v 1.8 2005/02/28 09:54:07 alan_k Exp $ // // Prototype Castable Object.. for DataObject queries // @@ -53,7 +53,7 @@ * * * -* @version $Id: Cast.php,v 1.7 2005/02/15 01:51:24 alan_k Exp $ +* @version $Id: Cast.php,v 1.8 2005/02/28 09:54:07 alan_k Exp $ */ class DB_DataObject_Cast { @@ -149,7 +149,7 @@ * Date Constructor * * create a Cast object from some string (not binary) - * + * NO VALIDATION DONE, although some crappy re-calcing done! * * @param vargs... accepts * dd/mm @@ -208,6 +208,136 @@ return $r; } + + + /** + * Data For time representation ** does not handle timezones!! + * + * @var int hour/minute/second + * @access public + */ + var $hour; + var $minute; + var $second; + + + /** + * DateTime Constructor + * + * create a Cast object from a Date/Time + * Maybe should accept a Date object.! + * NO VALIDATION DONE, although some crappy re-calcing done! + * + * @param vargs... accepts + * noargs (now) + * yyyy-mm-dd HH:MM:SS (Iso) + * array(yyyy,mm,dd,HH,MM,SS) + * + * + * @return object DB_DataObject_Cast + * @access public + * @author therion 5 at hotmail + */ + + function dateTime() + { + $args = func_get_args(); + switch(count($args)) { + case 0: // no args = now! + $datetime = date('Y-m-d G:i:s', mktime()); + + case 1: + // continue on from 0 args. + if (!isset($datetime)) { + $datetime = $args[0]; + } + + $parts = explode(' ', $datetime); + $bits = explode('-', $parts[0]); + $bits = array_merge($bits, explode(':', $parts[1])); + break; + + default: // 2 or more.. + $bits = $args; + + } + + if (count($bits) != 6) { + // PEAR ERROR? + return false; + } + + $r = DB_DataObject_Cast($bits[0], $bits[1], $bits[2]); + if (!$r) { + return $r; // pass thru error (False) - doesnt happen at present! + } + // change the type! + $r->type = 'datetime'; + + // should we mathematically sort this out.. + // (or just assume that no-one's dumb enough to enter 26:90:90 as a time! + $r->hour = $bits[3]; + $r->minute = $bits[4]; + $r->second = $bits[5]; + return $r; + + } + + + + /** + * time Constructor + * + * create a Cast object from a Date/Time + * Maybe should accept a Date object.! + * NO VALIDATION DONE, and no-recalcing done! + * + * @param vargs... accepts + * noargs (now) + * HH:MM:SS (Iso) + * array(HH,MM,SS) + * + * + * @return object DB_DataObject_Cast + * @access public + * @author therion 5 at hotmail + */ + function time() + { + $args = func_get_args(); + switch (count($args)) { + case 0: // no args = now! + $time = date('G:i:s', mktime()); + + case 1: + // continue on from 0 args. + if (!isset($time)) { + $time = $args[0]; + } + $bits = explode(':', $time); + break; + + default: // 2 or more.. + $bits = $args; + + } + + if (count($bits) != 3) { + return false; + } + + // now take data from bits into object fields + $r = new DB_DataObject_Cast; + $r->type = 'time'; + $r->hour = $bits[0]; + $r->minute = $bits[1]; + $r->second = $bits[2]; + return $r; + + } + + + /** * get the string to use in the SQL statement for this... * @@ -318,8 +448,59 @@ return "'{$this->year}-{$this->month}-{$this->day}'"; } - + /** + * get the string to use in the SQL statement for a datetime + * + * + * + * @param int $to Type (DB_DATAOBJECT_* + * @param string $db (eg. mysql|mssql.....) + * + * + * @return string + * @access public + * @author therion 5 at hotmail + */ + function toStringFromDateTime($to,$db) { + // first weed out invalid casts.. + // in blobs can only be cast to blobs.! + // perhaps we should support TEXT fields??? + if (($to !== false) && + !($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) { + return PEAR::raiseError('Invalid Cast from a ' . + ' DB_DataObject_Cast::dateTime to something other than a datetime!' . + ' (try using native features)'); + } + return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'"; + } + + /** + * get the string to use in the SQL statement for a time + * + * + * + * @param int $to Type (DB_DATAOBJECT_* + * @param string $db (eg. mysql|mssql.....) + * + * + * @return string + * @access public + * @author therion 5 at hotmail + */ + + function toStringFromTime($to,$db) { + // first weed out invalid casts.. + // in blobs can only be cast to blobs.! + // perhaps we should support TEXT fields??? + if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) { + return PEAR::raiseError('Invalid Cast from a' . + ' DB_DataObject_Cast::time to something other than a time!'. + ' (try using native features)'); + } + return "'{$this->hour}:{$this->minute}:{$this->second}'"; + } + /** * get the string to use in the SQL statement for a raw sql statement. *

« previous php.pear.cvs (#29923) next »