PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/pear/DB/DataObject/Cast.php

https://github.com/orchestra-io/sample-openx
PHP | 546 lines | 189 code | 81 blank | 276 comment | 29 complexity | ce45b217d139e008c948da1f8dd72977 MD5 | raw file
  1. <?php
  2. /**
  3. * Prototype Castable Object.. for DataObject queries
  4. *
  5. * Storage for Data that may be cast into a variety of formats.
  6. *
  7. * PHP versions 4 and 5
  8. *
  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.
  14. *
  15. * @category Database
  16. * @package DB_DataObject
  17. * @author Alan Knowles <alan@akbkhome.com>
  18. * @copyright 1997-2006 The PHP Group
  19. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  20. * @version CVS: $Id: Cast.php 4533 2007-02-19 16:52:09Z andrew.hill@openads.org $
  21. * @link http://pear.php.net/package/DB_DataObject
  22. */
  23. /**
  24. *
  25. * Common usages:
  26. * // blobs
  27. * $data = DB_DataObject_Cast::blob($somefile);
  28. * $data = DB_DataObject_Cast::string($somefile);
  29. * $dataObject->someblobfield = $data
  30. *
  31. * // dates?
  32. * $d1 = new DB_DataObject_Cast::date('12/12/2000');
  33. * $d2 = new DB_DataObject_Cast::date(2000,12,30);
  34. * $d3 = new DB_DataObject_Cast::date($d1->year, $d1->month+30, $d1->day+30);
  35. *
  36. * // time, datetime.. ?????????
  37. *
  38. * // raw sql????
  39. * $data = DB_DataObject_Cast::sql('cast("123123",datetime)');
  40. * $data = DB_DataObject_Cast::sql('NULL');
  41. *
  42. * // int's/string etc. are proably pretty pointless..!!!!
  43. *
  44. *
  45. * inside DB_DataObject,
  46. * if (is_a($v,'db_dataobject_class')) {
  47. * $value .= $v->toString(DB_DATAOBJECT_INT,'mysql');
  48. * }
  49. *
  50. *
  51. *
  52. *
  53. */
  54. class DB_DataObject_Cast {
  55. /**
  56. * Type of data Stored in the object..
  57. *
  58. * @var string (date|blob|.....?)
  59. * @access public
  60. */
  61. var $type;
  62. /**
  63. * Data For date representation
  64. *
  65. * @var int day/month/year
  66. * @access public
  67. */
  68. var $day;
  69. var $month;
  70. var $year;
  71. /**
  72. * Generic Data..
  73. *
  74. * @var string
  75. * @access public
  76. */
  77. var $value;
  78. /**
  79. * Blob consructor
  80. *
  81. * create a Cast object from some raw data.. (binary)
  82. *
  83. *
  84. * @param string (with binary data!)
  85. *
  86. * @return object DB_DataObject_Cast
  87. * @access public
  88. */
  89. function blob($value) {
  90. $r = new DB_DataObject_Cast;
  91. $r->type = 'blob';
  92. $r->value = $value;
  93. return $r;
  94. }
  95. /**
  96. * String consructor (actually use if for ints and everything else!!!
  97. *
  98. * create a Cast object from some string (not binary)
  99. *
  100. *
  101. * @param string (with binary data!)
  102. *
  103. * @return object DB_DataObject_Cast
  104. * @access public
  105. */
  106. function string($value) {
  107. $r = new DB_DataObject_Cast;
  108. $r->type = 'string';
  109. $r->value = $value;
  110. return $r;
  111. }
  112. /**
  113. * SQL constructor (for raw SQL insert)
  114. *
  115. * create a Cast object from some sql
  116. *
  117. * @param string (with binary data!)
  118. *
  119. * @return object DB_DataObject_Cast
  120. * @access public
  121. */
  122. function sql($value)
  123. {
  124. $r = new DB_DataObject_Cast;
  125. $r->type = 'sql';
  126. $r->value = $value;
  127. return $r;
  128. }
  129. /**
  130. * Date Constructor
  131. *
  132. * create a Cast object from some string (not binary)
  133. * NO VALIDATION DONE, although some crappy re-calcing done!
  134. *
  135. * @param vargs... accepts
  136. * dd/mm
  137. * dd/mm/yyyy
  138. * yyyy-mm
  139. * yyyy-mm-dd
  140. * array(yyyy,dd)
  141. * array(yyyy,dd,mm)
  142. *
  143. *
  144. *
  145. * @return object DB_DataObject_Cast
  146. * @access public
  147. */
  148. function date()
  149. {
  150. $args = func_get_args();
  151. switch(count($args)) {
  152. case 0: // no args = today!
  153. $bits = explode('-',date('Y-m-d'));
  154. break;
  155. case 1: // one arg = a string
  156. if (strpos($args[0],'/') !== false) {
  157. $bits = array_reverse(explode('/',$args[0]));
  158. } else {
  159. $bits = explode('-',$args[0]);
  160. }
  161. break;
  162. default: // 2 or more..
  163. $bits = $args;
  164. }
  165. if (count($bits) == 1) { // if YYYY set day = 1st..
  166. $bits[] = 1;
  167. }
  168. if (count($bits) == 2) { // if YYYY-DD set day = 1st..
  169. $bits[] = 1;
  170. }
  171. // if year < 1970 we cant use system tools to check it...
  172. // so we make a few best gueses....
  173. // basically do date calculations for the year 2000!!!
  174. // fix me if anyone has more time...
  175. if (($bits[0] < 1975) || ($bits[0] > 2030)) {
  176. $oldyear = $bits[0];
  177. $bits = explode('-',date('Y-m-d',mktime(1,1,1,$bits[1],$bits[2],2000)));
  178. $bits[0] = ($bits[0] - 2000) + $oldyear;
  179. } else {
  180. // now mktime
  181. $bits = explode('-',date('Y-m-d',mktime(1,1,1,$bits[1],$bits[2],$bits[0])));
  182. }
  183. $r = new DB_DataObject_Cast;
  184. $r->type = 'date';
  185. list($r->year,$r->month,$r->day) = $bits;
  186. return $r;
  187. }
  188. /**
  189. * Data For time representation ** does not handle timezones!!
  190. *
  191. * @var int hour/minute/second
  192. * @access public
  193. */
  194. var $hour;
  195. var $minute;
  196. var $second;
  197. /**
  198. * DateTime Constructor
  199. *
  200. * create a Cast object from a Date/Time
  201. * Maybe should accept a Date object.!
  202. * NO VALIDATION DONE, although some crappy re-calcing done!
  203. *
  204. * @param vargs... accepts
  205. * noargs (now)
  206. * yyyy-mm-dd HH:MM:SS (Iso)
  207. * array(yyyy,mm,dd,HH,MM,SS)
  208. *
  209. *
  210. * @return object DB_DataObject_Cast
  211. * @access public
  212. * @author therion 5 at hotmail
  213. */
  214. function dateTime()
  215. {
  216. $args = func_get_args();
  217. switch(count($args)) {
  218. case 0: // no args = now!
  219. $datetime = date('Y-m-d G:i:s', mktime());
  220. case 1:
  221. // continue on from 0 args.
  222. if (!isset($datetime)) {
  223. $datetime = $args[0];
  224. }
  225. $parts = explode(' ', $datetime);
  226. $bits = explode('-', $parts[0]);
  227. $bits = array_merge($bits, explode(':', $parts[1]));
  228. break;
  229. default: // 2 or more..
  230. $bits = $args;
  231. }
  232. if (count($bits) != 6) {
  233. // PEAR ERROR?
  234. return false;
  235. }
  236. $r = DB_DataObject_Cast::date($bits[0], $bits[1], $bits[2]);
  237. if (!$r) {
  238. return $r; // pass thru error (False) - doesnt happen at present!
  239. }
  240. // change the type!
  241. $r->type = 'datetime';
  242. // should we mathematically sort this out..
  243. // (or just assume that no-one's dumb enough to enter 26:90:90 as a time!
  244. $r->hour = $bits[3];
  245. $r->minute = $bits[4];
  246. $r->second = $bits[5];
  247. return $r;
  248. }
  249. /**
  250. * time Constructor
  251. *
  252. * create a Cast object from a Date/Time
  253. * Maybe should accept a Date object.!
  254. * NO VALIDATION DONE, and no-recalcing done!
  255. *
  256. * @param vargs... accepts
  257. * noargs (now)
  258. * HH:MM:SS (Iso)
  259. * array(HH,MM,SS)
  260. *
  261. *
  262. * @return object DB_DataObject_Cast
  263. * @access public
  264. * @author therion 5 at hotmail
  265. */
  266. function time()
  267. {
  268. $args = func_get_args();
  269. switch (count($args)) {
  270. case 0: // no args = now!
  271. $time = date('G:i:s', mktime());
  272. case 1:
  273. // continue on from 0 args.
  274. if (!isset($time)) {
  275. $time = $args[0];
  276. }
  277. $bits = explode(':', $time);
  278. break;
  279. default: // 2 or more..
  280. $bits = $args;
  281. }
  282. if (count($bits) != 3) {
  283. return false;
  284. }
  285. // now take data from bits into object fields
  286. $r = new DB_DataObject_Cast;
  287. $r->type = 'time';
  288. $r->hour = $bits[0];
  289. $r->minute = $bits[1];
  290. $r->second = $bits[2];
  291. return $r;
  292. }
  293. /**
  294. * get the string to use in the SQL statement for this...
  295. *
  296. *
  297. * @param int $to Type (DB_DATAOBJECT_*
  298. * @param object $db DB Connection Object
  299. *
  300. *
  301. * @return string
  302. * @access public
  303. */
  304. function toString($to=false,$db)
  305. {
  306. // if $this->type is not set, we are in serious trouble!!!!
  307. // values for to:
  308. $method = 'toStringFrom'.$this->type;
  309. return $this->$method($to,$db);
  310. }
  311. /**
  312. * get the string to use in the SQL statement from a blob of binary data
  313. * ** Suppots only blob->postgres::bytea
  314. *
  315. * @param int $to Type (DB_DATAOBJECT_*
  316. * @param object $db DB Connection Object
  317. *
  318. *
  319. * @return string
  320. * @access public
  321. */
  322. function toStringFromBlob($to,$db)
  323. {
  324. // first weed out invalid casts..
  325. // in blobs can only be cast to blobs.!
  326. // perhaps we should support TEXT fields???
  327. if (!($to & DB_DATAOBJECT_BLOB)) {
  328. return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::blob to something other than a blob!');
  329. }
  330. switch ($db->dsn["phptype"]) {
  331. case 'pgsql':
  332. return "'".pg_escape_bytea($this->value)."'::bytea";
  333. case 'mysql':
  334. return "'".mysql_real_escape_string($this->value,$db->connection)."'";
  335. case 'mysqli':
  336. // this is funny - the parameter order is reversed ;)
  337. return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
  338. default:
  339. return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
  340. }
  341. }
  342. /**
  343. * get the string to use in the SQL statement for a blob from a string!
  344. * ** Suppots only string->postgres::bytea
  345. *
  346. *
  347. * @param int $to Type (DB_DATAOBJECT_*
  348. * @param object $db DB Connection Object
  349. *
  350. *
  351. * @return string
  352. * @access public
  353. */
  354. function toStringFromString($to,$db)
  355. {
  356. // first weed out invalid casts..
  357. // in blobs can only be cast to blobs.!
  358. // perhaps we should support TEXT fields???
  359. //
  360. if (!($to & DB_DATAOBJECT_BLOB)) {
  361. return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a blob!'.
  362. ' (why not just use native features)');
  363. }
  364. switch ($db->dsn['phptype']) {
  365. case 'pgsql':
  366. return "'".pg_escape_string($this->value)."'::bytea";
  367. case 'mysql':
  368. return "'".mysql_real_escape_string($this->value,$db->connection)."'";
  369. case 'mysqli':
  370. return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
  371. default:
  372. return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
  373. }
  374. }
  375. /**
  376. * get the string to use in the SQL statement for a date
  377. *
  378. *
  379. *
  380. * @param int $to Type (DB_DATAOBJECT_*
  381. * @param object $db DB Connection Object
  382. *
  383. *
  384. * @return string
  385. * @access public
  386. */
  387. function toStringFromDate($to,$db)
  388. {
  389. // first weed out invalid casts..
  390. // in blobs can only be cast to blobs.!
  391. // perhaps we should support TEXT fields???
  392. //
  393. if (($to !== false) && !($to & DB_DATAOBJECT_DATE)) {
  394. return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!'.
  395. ' (why not just use native features)');
  396. }
  397. return "'{$this->year}-{$this->month}-{$this->day}'";
  398. }
  399. /**
  400. * get the string to use in the SQL statement for a datetime
  401. *
  402. *
  403. *
  404. * @param int $to Type (DB_DATAOBJECT_*
  405. * @param object $db DB Connection Object
  406. *
  407. *
  408. * @return string
  409. * @access public
  410. * @author therion 5 at hotmail
  411. */
  412. function toStringFromDateTime($to,$db)
  413. {
  414. // first weed out invalid casts..
  415. // in blobs can only be cast to blobs.!
  416. // perhaps we should support TEXT fields???
  417. if (($to !== false) &&
  418. !($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) {
  419. return PEAR::raiseError('Invalid Cast from a ' .
  420. ' DB_DataObject_Cast::dateTime to something other than a datetime!' .
  421. ' (try using native features)');
  422. }
  423. return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'";
  424. }
  425. /**
  426. * get the string to use in the SQL statement for a time
  427. *
  428. *
  429. *
  430. * @param int $to Type (DB_DATAOBJECT_*
  431. * @param object $db DB Connection Object
  432. *
  433. *
  434. * @return string
  435. * @access public
  436. * @author therion 5 at hotmail
  437. */
  438. function toStringFromTime($to,$db)
  439. {
  440. // first weed out invalid casts..
  441. // in blobs can only be cast to blobs.!
  442. // perhaps we should support TEXT fields???
  443. if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
  444. return PEAR::raiseError('Invalid Cast from a' .
  445. ' DB_DataObject_Cast::time to something other than a time!'.
  446. ' (try using native features)');
  447. }
  448. return "'{$this->hour}:{$this->minute}:{$this->second}'";
  449. }
  450. /**
  451. * get the string to use in the SQL statement for a raw sql statement.
  452. *
  453. * @param int $to Type (DB_DATAOBJECT_*
  454. * @param object $db DB Connection Object
  455. *
  456. *
  457. * @return string
  458. * @access public
  459. */
  460. function toStringFromSql($to,$db)
  461. {
  462. return $this->value;
  463. }
  464. }