]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/DB/DataObject/Cast.php
Merge branch 'master' of gitorious.org:social/mainline into social-master
[quix0rs-gnu-social.git] / extlib / DB / DataObject / Cast.php
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.01 of the PHP license
10  * that is available through the world-wide-web at the following URI:
11  * http://www.php.net/license/3_01.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-2008 The PHP Group
19  * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
20  * @version    CVS: $Id: Cast.php 326604 2012-07-12 03:02:00Z alan_k $
21  * @link       http://pear.php.net/package/DB_DataObject
22  */
23   
24 /**
25 *  
26 * Common usages:
27 *   // blobs
28 *   $data = DB_DataObject_Cast::blob($somefile);
29 *   $data = DB_DataObject_Cast::string($somefile);
30 *   $dataObject->someblobfield = $data
31 *
32 *   // dates?
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);
36 *   
37 *   // time, datetime.. ?????????
38 *
39 *   // raw sql????
40 *    $data = DB_DataObject_Cast::sql('cast("123123",datetime)');
41 *    $data = DB_DataObject_Cast::sql('NULL');
42 *
43 *   // int's/string etc. are proably pretty pointless..!!!!
44 *
45 *   
46 *   inside DB_DataObject, 
47 *   if (is_a($v,'db_dataobject_class')) {
48 *           $value .= $v->toString(DB_DATAOBJECT_INT,'mysql');
49 *   }
50 *
51 *
52 *
53 *
54
55 */ 
56 class DB_DataObject_Cast {
57         
58     /**
59     * Type of data Stored in the object..
60     *
61     * @var string       (date|blob|.....?)
62     * @access public        
63     */
64     var $type;
65         
66     /**
67     * Data For date representation
68     *
69     * @var int  day/month/year
70     * @access public
71     */
72     var $day;
73     var $month;
74     var $year;
75
76     
77     /**
78     * Generic Data..
79     *
80     * @var string
81     * @access public
82     */
83
84     var $value;
85
86
87
88     /**
89     * Blob consructor
90     *
91     * create a Cast object from some raw data.. (binary)
92     * 
93     * 
94     * @param   string (with binary data!)
95     *
96     * @return   object DB_DataObject_Cast
97     * @access   public 
98     */
99   
100     function blob($value) {
101         $r = new DB_DataObject_Cast;
102         $r->type = 'blob';
103         $r->value = $value;
104         return $r;
105     }
106
107
108     /**
109     * String consructor (actually use if for ints and everything else!!!
110     *
111     * create a Cast object from some string (not binary)
112     * 
113     * 
114     * @param   string (with binary data!)
115     *
116     * @return   object DB_DataObject_Cast
117     * @access   public 
118     */
119   
120     function string($value) {
121         $r = new DB_DataObject_Cast;
122         $r->type = 'string';
123         $r->value = $value;
124         return $r;
125     }
126     
127     /**
128     * SQL constructor (for raw SQL insert)
129     *
130     * create a Cast object from some sql
131     * 
132     * @param   string (with binary data!)
133     *
134     * @return   object DB_DataObject_Cast
135     * @access   public 
136     */
137   
138     function sql($value) 
139     {
140         $r = new DB_DataObject_Cast;
141         $r->type = 'sql';
142         $r->value = $value;
143         return $r;
144     }
145
146
147     /**
148     * Date Constructor
149     *
150     * create a Cast object from some string (not binary)
151     * NO VALIDATION DONE, although some crappy re-calcing done!
152     * 
153     * @param   vargs... accepts
154     *       dd/mm
155     *       dd/mm/yyyy
156     *       yyyy-mm
157     *       yyyy-mm-dd
158     *       array(yyyy,dd)
159     *       array(yyyy,dd,mm)
160     *
161     *
162     *
163     * @return   object DB_DataObject_Cast
164     * @access   public 
165     */
166   
167     function date() 
168     {  
169         $args = func_get_args();
170         switch(count($args)) {
171             case 0: // no args = today!
172                $bits =  explode('-',date('Y-m-d'));
173                 break;
174             case 1: // one arg = a string 
175             
176                 if (strpos($args[0],'/') !== false) {
177                     $bits = array_reverse(explode('/',$args[0]));
178                 } else {
179                     $bits = explode('-',$args[0]);
180                 }
181                 break;
182             default: // 2 or more..
183                 $bits = $args;
184         }
185         if (count($bits) == 1) { // if YYYY set day = 1st..
186             $bits[] = 1;
187         }
188         
189         if (count($bits) == 2) { // if YYYY-DD set day = 1st..
190             $bits[] = 1;
191         }
192         
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)) {
198             $oldyear = $bits[0];
199             $bits = explode('-',date('Y-m-d',mktime(1,1,1,$bits[1],$bits[2],2000)));
200             $bits[0] = ($bits[0] - 2000) + $oldyear;
201         } else {
202             // now mktime
203             $bits = explode('-',date('Y-m-d',mktime(1,1,1,$bits[1],$bits[2],$bits[0])));
204         }
205         $r = new DB_DataObject_Cast;
206         $r->type = 'date';
207         list($r->year,$r->month,$r->day) = $bits;
208         return $r;
209     }
210     
211    
212
213     /**
214     * Data For time representation ** does not handle timezones!!
215     *
216     * @var int  hour/minute/second
217     * @access public
218     */
219     var $hour;
220     var $minute;
221     var $second;
222
223     
224     /**
225     * DateTime Constructor
226     *
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!
230     * 
231     * @param   vargs... accepts
232     *              noargs (now)
233     *              yyyy-mm-dd HH:MM:SS (Iso)
234     *              array(yyyy,mm,dd,HH,MM,SS) 
235     *
236     *
237     * @return   object DB_DataObject_Cast
238     * @access   public 
239     * @author   therion 5 at hotmail
240     */
241     
242     function dateTime()
243     {
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());
248             
249             case 1:
250                 // continue on from 0 args.
251                 if (!isset($datetime)) {
252                     $datetime = $args[0];
253                 }
254                 
255                 $parts =  explode(' ', $datetime);
256                 $bits = explode('-', $parts[0]);
257                 $bits = array_merge($bits, explode(':', $parts[1]));
258                 break;
259                 
260             default: // 2 or more..
261                 $bits = $args;
262                 
263         }
264
265         if (count($bits) != 6) {
266             // PEAR ERROR?
267             return false;
268         }
269         
270         $r = DB_DataObject_Cast::date($bits[0], $bits[1], $bits[2]);
271         if (!$r) {
272             return $r; // pass thru error (False) - doesnt happen at present!
273         }
274         // change the type!
275         $r->type = 'datetime';
276         
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!
279         $r->hour = $bits[3];
280         $r->minute = $bits[4];
281         $r->second = $bits[5];
282         return $r;
283
284     }
285
286
287
288     /**
289     * time Constructor
290     *
291     * create a Cast object from a Date/Time
292     * Maybe should accept a Date object.!
293     * NO VALIDATION DONE, and no-recalcing done!
294     *
295     * @param   vargs... accepts
296     *              noargs (now)
297     *              HH:MM:SS (Iso)
298     *              array(HH,MM,SS)    
299     *
300     *
301     * @return   object DB_DataObject_Cast
302     * @access   public 
303     * @author   therion 5 at hotmail
304     */
305     function time()
306     {
307         $args = func_get_args();
308         switch (count($args)) {
309             case 0: // no args = now!
310                 $time = date('G:i:s', mktime());
311                 
312             case 1:
313                 // continue on from 0 args.
314                 if (!isset($time)) {
315                     $time = $args[0];
316                 }
317                 $bits =  explode(':', $time);
318                 break;
319                 
320             default: // 2 or more..
321                 $bits = $args;
322                 
323         }
324         
325         if (count($bits) != 3) {
326             return false;
327         }
328         
329         // now take data from bits into object fields
330         $r = new DB_DataObject_Cast;
331         $r->type = 'time';
332         $r->hour = $bits[0];
333         $r->minute = $bits[1];
334         $r->second = $bits[2];
335         return $r;
336
337     }
338
339   
340   
341     /**
342     * get the string to use in the SQL statement for this...
343     *
344     * 
345     * @param   int      $to Type (DB_DATAOBJECT_*
346     * @param   object   $db DB Connection Object
347     * 
348     *
349     * @return   string 
350     * @access   public
351     */
352   
353     function toString($to=false,$db) 
354     {
355         // if $this->type is not set, we are in serious trouble!!!!
356         // values for to:
357         $method = 'toStringFrom'.$this->type;
358         return $this->$method($to,$db);
359     }
360     
361     /**
362     * get the string to use in the SQL statement from a blob of binary data 
363     *   ** Suppots only blob->postgres::bytea
364     *
365     * @param   int      $to Type (DB_DATAOBJECT_*
366     * @param   object   $db DB Connection Object
367     * 
368     *
369     * @return   string 
370     * @access   public
371     */
372     function toStringFromBlob($to,$db) 
373     {
374         // first weed out invalid casts..
375         // in blobs can only be cast to blobs.!
376         
377         // perhaps we should support TEXT fields???
378         
379         if (!($to & DB_DATAOBJECT_BLOB)) {
380             return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::blob to something other than a blob!');
381         }
382         
383         switch ($db->dsn["phptype"]) {
384             case 'pgsql':
385                 return "'".pg_escape_bytea($this->value)."'::bytea";
386                 
387             case 'mysql':
388                 return "'".mysql_real_escape_string($this->value,$db->connection)."'";
389             
390             case 'mysqli':
391                 // this is funny - the parameter order is reversed ;)
392                 return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
393              
394             case 'sqlite':
395                 // this is funny - the parameter order is reversed ;)
396                 return "'".sqlite_escape_string($this->value)."'";
397            
398             case 'mssql':
399                 
400                 if(is_numeric($this->value)) {
401                     return $this->value;
402                 }
403                 $unpacked = unpack('H*hex', $this->value);
404                 return '0x' . $unpacked['hex'];
405                         
406                  
407    
408             default:
409                 return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
410         }
411     
412     }
413     
414     /**
415     * get the string to use in the SQL statement for a blob from a string!
416     *   ** Suppots only string->postgres::bytea
417     * 
418     *
419     * @param   int      $to Type (DB_DATAOBJECT_*
420     * @param   object   $db DB Connection Object
421     * 
422     *
423     * @return   string 
424     * @access   public
425     */
426     function toStringFromString($to,$db) 
427     {
428         // first weed out invalid casts..
429         // in blobs can only be cast to blobs.!
430         
431         // perhaps we should support TEXT fields???
432         // 
433         
434         // $to == a string field which is the default type (0)
435         // so we do not test it here. - we assume that number fields
436         // will accept a string?? - which is stretching it a bit ...
437         // should probaly add that test as some point. 
438         
439         switch ($db->dsn['phptype']) {
440             case 'pgsql':
441                 return "'".pg_escape_string($this->value)."'::bytea";
442             
443             case 'mysql':
444                 return "'".mysql_real_escape_string($this->value,$db->connection)."'";
445             
446             
447             case 'mysqli':
448                 return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
449
450             case 'mssql':
451                 // copied from the old DB mssql code...?? not sure how safe this is.
452                 return "'" . str_replace(
453                         array("'", "\\\r\n", "\\\n"),
454                         array("''", "\\\\\r\n\r\n", "\\\\\n\n"),
455                         $this->value 
456                     ) . "'";
457                 
458
459             default:
460                 return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
461         }
462     
463     }
464     
465     
466     /**
467     * get the string to use in the SQL statement for a date
468     *   
469     * 
470     *
471     * @param   int      $to Type (DB_DATAOBJECT_*
472     * @param   object   $db DB Connection Object
473     * 
474     *
475     * @return   string 
476     * @access   public
477     */
478     function toStringFromDate($to,$db) 
479     {
480         // first weed out invalid casts..
481         // in blobs can only be cast to blobs.!
482          // perhaps we should support TEXT fields???
483         // 
484         
485         if (($to !== false) && !($to & DB_DATAOBJECT_DATE)) {
486             return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!'.
487                 ' (why not just use native features)');
488         }
489         return "'{$this->year}-{$this->month}-{$this->day}'";
490     }
491     
492     /**
493     * get the string to use in the SQL statement for a datetime
494     *   
495     * 
496     *
497     * @param   int     $to Type (DB_DATAOBJECT_*
498     * @param   object   $db DB Connection Object
499     * 
500     *
501     * @return   string 
502     * @access   public
503     * @author   therion 5 at hotmail
504     */
505     
506     function toStringFromDateTime($to,$db) 
507     {
508         // first weed out invalid casts..
509         // in blobs can only be cast to blobs.!
510         // perhaps we should support TEXT fields???
511         if (($to !== false) && 
512             !($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) {
513             return PEAR::raiseError('Invalid Cast from a ' .
514                 ' DB_DataObject_Cast::dateTime to something other than a datetime!' .
515                 ' (try using native features)');
516         }
517         return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'";
518     }
519
520     /**
521     * get the string to use in the SQL statement for a time
522     *   
523     * 
524     *
525     * @param   int     $to Type (DB_DATAOBJECT_*
526     * @param   object   $db DB Connection Object
527     * 
528     *
529     * @return   string 
530     * @access   public
531     * @author   therion 5 at hotmail
532     */
533
534     function toStringFromTime($to,$db) 
535     {
536         // first weed out invalid casts..
537         // in blobs can only be cast to blobs.!
538         // perhaps we should support TEXT fields???
539         if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
540             return PEAR::raiseError('Invalid Cast from a' . 
541                 ' DB_DataObject_Cast::time to something other than a time!'.
542                 ' (try using native features)');
543         }
544         return "'{$this->hour}:{$this->minute}:{$this->second}'";
545     }
546   
547     /**
548     * get the string to use in the SQL statement for a raw sql statement.
549     *
550     * @param   int      $to Type (DB_DATAOBJECT_*
551     * @param   object   $db DB Connection Object
552     * 
553     *
554     * @return   string 
555     * @access   public
556     */
557     function toStringFromSql($to,$db) 
558     {
559         return $this->value; 
560     }
561     
562     
563     
564 }
565