3 * Prototype Castable Object.. for DataObject queries
5 * Storage for Data that may be cast into a variety of formats.
9 * LICENSE: This source file is subject to version 3.0 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
16 * @package DB_DataObject
17 * @author Alan Knowles <alan@akbkhome.com>
18 * @copyright 1997-2008 The PHP Group
19 * @license http://www.php.net/license/3_0.txt PHP License 3.0
20 * @version CVS: $Id: Cast.php 264148 2008-08-04 03:44:59Z alan_k $
21 * @link http://pear.php.net/package/DB_DataObject
28 * $data = DB_DataObject_Cast::blob($somefile);
29 * $data = DB_DataObject_Cast::string($somefile);
30 * $dataObject->someblobfield = $data
33 * $d1 = new DB_DataObject_Cast::date('12/12/2000');
34 * $d2 = new DB_DataObject_Cast::date(2000,12,30);
35 * $d3 = new DB_DataObject_Cast::date($d1->year, $d1->month+30, $d1->day+30);
37 * // time, datetime.. ?????????
40 * $data = DB_DataObject_Cast::sql('cast("123123",datetime)');
41 * $data = DB_DataObject_Cast::sql('NULL');
43 * // int's/string etc. are proably pretty pointless..!!!!
46 * inside DB_DataObject,
47 * if (is_a($v,'db_dataobject_class')) {
48 * $value .= $v->toString(DB_DATAOBJECT_INT,'mysql');
56 class DB_DataObject_Cast {
59 * Type of data Stored in the object..
61 * @var string (date|blob|.....?)
67 * Data For date representation
69 * @var int day/month/year
91 * create a Cast object from some raw data.. (binary)
94 * @param string (with binary data!)
96 * @return object DB_DataObject_Cast
100 function blob($value) {
101 $r = new DB_DataObject_Cast;
109 * String consructor (actually use if for ints and everything else!!!
111 * create a Cast object from some string (not binary)
114 * @param string (with binary data!)
116 * @return object DB_DataObject_Cast
120 function string($value) {
121 $r = new DB_DataObject_Cast;
128 * SQL constructor (for raw SQL insert)
130 * create a Cast object from some sql
132 * @param string (with binary data!)
134 * @return object DB_DataObject_Cast
140 $r = new DB_DataObject_Cast;
150 * create a Cast object from some string (not binary)
151 * NO VALIDATION DONE, although some crappy re-calcing done!
153 * @param vargs... accepts
163 * @return object DB_DataObject_Cast
169 $args = func_get_args();
170 switch(count($args)) {
171 case 0: // no args = today!
172 $bits = explode('-',date('Y-m-d'));
174 case 1: // one arg = a string
176 if (strpos($args[0],'/') !== false) {
177 $bits = array_reverse(explode('/',$args[0]));
179 $bits = explode('-',$args[0]);
182 default: // 2 or more..
185 if (count($bits) == 1) { // if YYYY set day = 1st..
189 if (count($bits) == 2) { // if YYYY-DD set day = 1st..
193 // if year < 1970 we cant use system tools to check it...
194 // so we make a few best gueses....
195 // basically do date calculations for the year 2000!!!
196 // fix me if anyone has more time...
197 if (($bits[0] < 1975) || ($bits[0] > 2030)) {
199 $bits = explode('-',date('Y-m-d',mktime(1,1,1,$bits[1],$bits[2],2000)));
200 $bits[0] = ($bits[0] - 2000) + $oldyear;
203 $bits = explode('-',date('Y-m-d',mktime(1,1,1,$bits[1],$bits[2],$bits[0])));
205 $r = new DB_DataObject_Cast;
207 list($r->year,$r->month,$r->day) = $bits;
214 * Data For time representation ** does not handle timezones!!
216 * @var int hour/minute/second
225 * DateTime Constructor
227 * create a Cast object from a Date/Time
228 * Maybe should accept a Date object.!
229 * NO VALIDATION DONE, although some crappy re-calcing done!
231 * @param vargs... accepts
233 * yyyy-mm-dd HH:MM:SS (Iso)
234 * array(yyyy,mm,dd,HH,MM,SS)
237 * @return object DB_DataObject_Cast
239 * @author therion 5 at hotmail
244 $args = func_get_args();
245 switch(count($args)) {
246 case 0: // no args = now!
247 $datetime = date('Y-m-d G:i:s', mktime());
250 // continue on from 0 args.
251 if (!isset($datetime)) {
252 $datetime = $args[0];
255 $parts = explode(' ', $datetime);
256 $bits = explode('-', $parts[0]);
257 $bits = array_merge($bits, explode(':', $parts[1]));
260 default: // 2 or more..
265 if (count($bits) != 6) {
270 $r = DB_DataObject_Cast::date($bits[0], $bits[1], $bits[2]);
272 return $r; // pass thru error (False) - doesnt happen at present!
275 $r->type = 'datetime';
277 // should we mathematically sort this out..
278 // (or just assume that no-one's dumb enough to enter 26:90:90 as a time!
280 $r->minute = $bits[4];
281 $r->second = $bits[5];
291 * create a Cast object from a Date/Time
292 * Maybe should accept a Date object.!
293 * NO VALIDATION DONE, and no-recalcing done!
295 * @param vargs... accepts
301 * @return object DB_DataObject_Cast
303 * @author therion 5 at hotmail
307 $args = func_get_args();
308 switch (count($args)) {
309 case 0: // no args = now!
310 $time = date('G:i:s', mktime());
313 // continue on from 0 args.
317 $bits = explode(':', $time);
320 default: // 2 or more..
325 if (count($bits) != 3) {
329 // now take data from bits into object fields
330 $r = new DB_DataObject_Cast;
333 $r->minute = $bits[1];
334 $r->second = $bits[2];
342 * get the string to use in the SQL statement for this...
345 * @param int $to Type (DB_DATAOBJECT_*
346 * @param object $db DB Connection Object
353 function toString($to=false,$db)
355 // if $this->type is not set, we are in serious trouble!!!!
357 $method = 'toStringFrom'.$this->type;
358 return $this->$method($to,$db);
362 * get the string to use in the SQL statement from a blob of binary data
363 * ** Suppots only blob->postgres::bytea
365 * @param int $to Type (DB_DATAOBJECT_*
366 * @param object $db DB Connection Object
372 function toStringFromBlob($to,$db)
374 // first weed out invalid casts..
375 // in blobs can only be cast to blobs.!
377 // perhaps we should support TEXT fields???
379 if (!($to & DB_DATAOBJECT_BLOB)) {
380 return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::blob to something other than a blob!');
383 switch ($db->dsn["phptype"]) {
385 return "'".pg_escape_bytea($this->value)."'::bytea";
388 return "'".mysql_real_escape_string($this->value,$db->connection)."'";
391 // this is funny - the parameter order is reversed ;)
392 return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
395 // this is funny - the parameter order is reversed ;)
396 return "'".sqlite_escape_string($this->value)."'";
400 return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
406 * get the string to use in the SQL statement for a blob from a string!
407 * ** Suppots only string->postgres::bytea
410 * @param int $to Type (DB_DATAOBJECT_*
411 * @param object $db DB Connection Object
417 function toStringFromString($to,$db)
419 // first weed out invalid casts..
420 // in blobs can only be cast to blobs.!
422 // perhaps we should support TEXT fields???
425 if (!($to & DB_DATAOBJECT_BLOB)) {
426 return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a blob!'.
427 ' (why not just use native features)');
430 switch ($db->dsn['phptype']) {
432 return "'".pg_escape_string($this->value)."'::bytea";
435 return "'".mysql_real_escape_string($this->value,$db->connection)."'";
439 return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
443 return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
450 * get the string to use in the SQL statement for a date
454 * @param int $to Type (DB_DATAOBJECT_*
455 * @param object $db DB Connection Object
461 function toStringFromDate($to,$db)
463 // first weed out invalid casts..
464 // in blobs can only be cast to blobs.!
465 // perhaps we should support TEXT fields???
468 if (($to !== false) && !($to & DB_DATAOBJECT_DATE)) {
469 return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!'.
470 ' (why not just use native features)');
472 return "'{$this->year}-{$this->month}-{$this->day}'";
476 * get the string to use in the SQL statement for a datetime
480 * @param int $to Type (DB_DATAOBJECT_*
481 * @param object $db DB Connection Object
486 * @author therion 5 at hotmail
489 function toStringFromDateTime($to,$db)
491 // first weed out invalid casts..
492 // in blobs can only be cast to blobs.!
493 // perhaps we should support TEXT fields???
494 if (($to !== false) &&
495 !($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) {
496 return PEAR::raiseError('Invalid Cast from a ' .
497 ' DB_DataObject_Cast::dateTime to something other than a datetime!' .
498 ' (try using native features)');
500 return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'";
504 * get the string to use in the SQL statement for a time
508 * @param int $to Type (DB_DATAOBJECT_*
509 * @param object $db DB Connection Object
514 * @author therion 5 at hotmail
517 function toStringFromTime($to,$db)
519 // first weed out invalid casts..
520 // in blobs can only be cast to blobs.!
521 // perhaps we should support TEXT fields???
522 if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
523 return PEAR::raiseError('Invalid Cast from a' .
524 ' DB_DataObject_Cast::time to something other than a time!'.
525 ' (try using native features)');
527 return "'{$this->hour}:{$this->minute}:{$this->second}'";
531 * get the string to use in the SQL statement for a raw sql statement.
533 * @param int $to Type (DB_DATAOBJECT_*
534 * @param object $db DB Connection Object
540 function toStringFromSql($to,$db)