]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/DB/DataObject/Cast.php
Merge commit 'br3nda/0.8.x-pgfixes' into 0.8.x
[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.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-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
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                  
399             default:
400                 return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
401         }
402     
403     }
404     
405     /**
406     * get the string to use in the SQL statement for a blob from a string!
407     *   ** Suppots only string->postgres::bytea
408     * 
409     *
410     * @param   int      $to Type (DB_DATAOBJECT_*
411     * @param   object   $db DB Connection Object
412     * 
413     *
414     * @return   string 
415     * @access   public
416     */
417     function toStringFromString($to,$db) 
418     {
419         // first weed out invalid casts..
420         // in blobs can only be cast to blobs.!
421         
422         // perhaps we should support TEXT fields???
423         // 
424         
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)');
428         }
429         
430         switch ($db->dsn['phptype']) {
431             case 'pgsql':
432                 return "'".pg_escape_string($this->value)."'::bytea";
433             
434             case 'mysql':
435                 return "'".mysql_real_escape_string($this->value,$db->connection)."'";
436             
437             
438             case 'mysqli':
439                 return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
440
441             
442             default:
443                 return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
444         }
445     
446     }
447     
448     
449     /**
450     * get the string to use in the SQL statement for a date
451     *   
452     * 
453     *
454     * @param   int      $to Type (DB_DATAOBJECT_*
455     * @param   object   $db DB Connection Object
456     * 
457     *
458     * @return   string 
459     * @access   public
460     */
461     function toStringFromDate($to,$db) 
462     {
463         // first weed out invalid casts..
464         // in blobs can only be cast to blobs.!
465          // perhaps we should support TEXT fields???
466         // 
467         
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)');
471         }
472         return "'{$this->year}-{$this->month}-{$this->day}'";
473     }
474     
475     /**
476     * get the string to use in the SQL statement for a datetime
477     *   
478     * 
479     *
480     * @param   int     $to Type (DB_DATAOBJECT_*
481     * @param   object   $db DB Connection Object
482     * 
483     *
484     * @return   string 
485     * @access   public
486     * @author   therion 5 at hotmail
487     */
488     
489     function toStringFromDateTime($to,$db) 
490     {
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)');
499         }
500         return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'";
501     }
502
503     /**
504     * get the string to use in the SQL statement for a time
505     *   
506     * 
507     *
508     * @param   int     $to Type (DB_DATAOBJECT_*
509     * @param   object   $db DB Connection Object
510     * 
511     *
512     * @return   string 
513     * @access   public
514     * @author   therion 5 at hotmail
515     */
516
517     function toStringFromTime($to,$db) 
518     {
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)');
526         }
527         return "'{$this->hour}:{$this->minute}:{$this->second}'";
528     }
529   
530     /**
531     * get the string to use in the SQL statement for a raw sql statement.
532     *
533     * @param   int      $to Type (DB_DATAOBJECT_*
534     * @param   object   $db DB Connection Object
535     * 
536     *
537     * @return   string 
538     * @access   public
539     */
540     function toStringFromSql($to,$db) 
541     {
542         return $this->value; 
543     }
544     
545     
546     
547     
548 }
549