123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- <?php
- class DB_DataObject_Cast
- {
-
- public $type;
-
- public $day;
- public $month;
- public $year;
-
- public $value;
-
- public $hour;
- public $minute;
- public $second;
-
- public static function blob($value)
- {
- $r = new DB_DataObject_Cast;
- $r->type = 'blob';
- $r->value = $value;
- return $r;
- }
-
- public static function string($value)
- {
- $r = new DB_DataObject_Cast;
- $r->type = 'string';
- $r->value = $value;
- return $r;
- }
-
- public static function sql($value)
- {
- $r = new DB_DataObject_Cast;
- $r->type = 'sql';
- $r->value = $value;
- return $r;
- }
-
- public static function dateTime()
- {
- $args = func_get_args();
- switch (count($args)) {
- case 0:
- $datetime = date('Y-m-d G:i:s', time());
-
- case 1:
-
- if (!isset($datetime)) {
- $datetime = $args[0];
- }
- $parts = explode(' ', $datetime);
- $bits = explode('-', $parts[0]);
- $bits = array_merge($bits, explode(':', $parts[1]));
- break;
- default:
- $bits = $args;
- }
- if (count($bits) != 6) {
-
- return false;
- }
- $r = DB_DataObject_Cast::date($bits[0], $bits[1], $bits[2]);
- if (!$r) {
- return $r;
- }
-
- $r->type = 'datetime';
-
-
- $r->hour = $bits[3];
- $r->minute = $bits[4];
- $r->second = $bits[5];
- return $r;
- }
-
- public static function date()
- {
- $args = func_get_args();
- switch (count($args)) {
- case 0:
- $bits = explode('-', date('Y-m-d'));
- break;
- case 1:
- if (strpos($args[0], '/') !== false) {
- $bits = array_reverse(explode('/', $args[0]));
- } else {
- $bits = explode('-', $args[0]);
- }
- break;
- default:
- $bits = $args;
- }
- if (count($bits) == 1) {
- $bits[] = 1;
- }
- if (count($bits) == 2) {
- $bits[] = 1;
- }
-
-
-
-
- if (($bits[0] < 1975) || ($bits[0] > 2030)) {
- $oldyear = $bits[0];
- $bits = explode('-', date('Y-m-d', mktime(1, 1, 1, $bits[1], $bits[2], 2000)));
- $bits[0] = ($bits[0] - 2000) + $oldyear;
- } else {
-
- $bits = explode('-', date('Y-m-d', mktime(1, 1, 1, $bits[1], $bits[2], $bits[0])));
- }
- $r = new DB_DataObject_Cast;
- $r->type = 'date';
- list($r->year, $r->month, $r->day) = $bits;
- return $r;
- }
-
- public static function time()
- {
- $args = func_get_args();
- switch (count($args)) {
- case 0:
- $time = date('G:i:s', time());
-
- case 1:
-
- if (!isset($time)) {
- $time = $args[0];
- }
- $bits = explode(':', $time);
- break;
- default:
- $bits = $args;
- }
- if (count($bits) != 3) {
- return false;
- }
-
- $r = new DB_DataObject_Cast;
- $r->type = 'time';
- $r->hour = $bits[0];
- $r->minute = $bits[1];
- $r->second = $bits[2];
- return $r;
- }
-
- public function toString($to = false, $db)
- {
-
-
- $method = 'toStringFrom' . $this->type;
- return $this->$method($to, $db);
- }
-
- public function toStringFromBlob($to, $db)
- {
-
-
-
- if (!($to & DB_DATAOBJECT_BLOB)) {
- return (new PEAR)->raiseError('Invalid Cast from a DB_DataObject_Cast::blob to something other than a blob!');
- }
- switch ($db->dsn["phptype"]) {
- case 'pgsql':
- return "'" . pg_escape_bytea($this->value) . "'::bytea";
- case 'mysql':
- return "'" . mysql_real_escape_string($this->value, $db->connection) . "'";
- case 'mysqli':
-
- return "'" . mysqli_real_escape_string($db->connection, $this->value) . "'";
- case 'sqlite':
-
- return "'" . sqlite_escape_string($this->value) . "'";
- case 'mssql':
- if (is_numeric($this->value)) {
- return $this->value;
- }
- $unpacked = unpack('H*hex', $this->value);
- return '0x' . $unpacked['hex'];
- default:
- return (new PEAR)->raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
- }
- }
-
- public function toStringFromString($to, $db)
- {
-
-
-
-
-
-
-
-
- switch ($db->dsn['phptype']) {
- case 'pgsql':
- return "'" . pg_escape_string($this->value) . "'::bytea";
- case 'mysql':
- return "'" . mysql_real_escape_string($this->value, $db->connection) . "'";
- case 'mysqli':
- return "'" . mysqli_real_escape_string($db->connection, $this->value) . "'";
- case 'mssql':
-
- return "'" . str_replace(
- array("'", "\\\r\n", "\\\n"),
- array("''", "\\\\\r\n\r\n", "\\\\\n\n"),
- $this->value
- ) . "'";
- default:
- return (new PEAR)->raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
- }
- }
-
- public function toStringFromDate($to, $db)
- {
-
-
-
-
- if (($to !== false) && !($to & DB_DATAOBJECT_DATE)) {
- return (new PEAR)->raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!' .
- ' (why not just use native features)');
- }
- return "'{$this->year}-{$this->month}-{$this->day}'";
- }
-
- public function toStringFromDateTime($to, $db)
- {
-
-
-
- if (($to !== false) &&
- !($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) {
- return (new 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}'";
- }
-
- public function toStringFromTime($to, $db)
- {
-
-
-
- if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
- return (new 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}'";
- }
-
- public function toStringFromSql($to, $db)
- {
- return $this->value;
- }
- }
|