Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 jpm 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-2005 The PHP Group
19
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
20
 * @version    CVS: $Id: Cast.php,v 1.15 2005/07/07 05:30:53 alan_k Exp $
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
 
395
 
396
            default:
397
                return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
398
        }
399
 
400
    }
401
 
402
    /**
403
    * get the string to use in the SQL statement for a blob from a string!
404
    *   ** Suppots only string->postgres::bytea
405
    *
406
    *
407
    * @param   int      $to Type (DB_DATAOBJECT_*
408
    * @param   object   $db DB Connection Object
409
    *
410
    *
411
    * @return   string
412
    * @access   public
413
    */
414
    function toStringFromString($to,$db)
415
    {
416
        // first weed out invalid casts..
417
        // in blobs can only be cast to blobs.!
418
 
419
        // perhaps we should support TEXT fields???
420
        //
421
 
422
        if (!($to & DB_DATAOBJECT_BLOB)) {
423
            return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a blob!'.
424
                ' (why not just use native features)');
425
        }
426
 
427
        switch ($db->dsn['phptype']) {
428
            case 'pgsql':
429
                return "'".pg_escape_string($this->value)."'::bytea";
430
 
431
            case 'mysql':
432
                return "'".mysql_real_escape_string($this->value,$db->connection)."'";
433
 
434
 
435
            case 'mysqli':
436
                return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
437
 
438
 
439
            default:
440
                return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
441
        }
442
 
443
    }
444
 
445
 
446
    /**
447
    * get the string to use in the SQL statement for a date
448
    *
449
    *
450
    *
451
    * @param   int      $to Type (DB_DATAOBJECT_*
452
    * @param   object   $db DB Connection Object
453
    *
454
    *
455
    * @return   string
456
    * @access   public
457
    */
458
    function toStringFromDate($to,$db)
459
    {
460
        // first weed out invalid casts..
461
        // in blobs can only be cast to blobs.!
462
         // perhaps we should support TEXT fields???
463
        //
464
 
465
        if (($to !== false) && !($to & DB_DATAOBJECT_DATE)) {
466
            return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!'.
467
                ' (why not just use native features)');
468
        }
469
        return "'{$this->year}-{$this->month}-{$this->day}'";
470
    }
471
 
472
    /**
473
    * get the string to use in the SQL statement for a datetime
474
    *
475
    *
476
    *
477
    * @param   int     $to Type (DB_DATAOBJECT_*
478
    * @param   object   $db DB Connection Object
479
    *
480
    *
481
    * @return   string
482
    * @access   public
483
    * @author   therion 5 at hotmail
484
    */
485
 
486
    function toStringFromDateTime($to,$db)
487
    {
488
        // first weed out invalid casts..
489
        // in blobs can only be cast to blobs.!
490
        // perhaps we should support TEXT fields???
491
        if (($to !== false) &&
492
            !($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) {
493
            return PEAR::raiseError('Invalid Cast from a ' .
494
                ' DB_DataObject_Cast::dateTime to something other than a datetime!' .
495
                ' (try using native features)');
496
        }
497
        return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'";
498
    }
499
 
500
    /**
501
    * get the string to use in the SQL statement for a time
502
    *
503
    *
504
    *
505
    * @param   int     $to Type (DB_DATAOBJECT_*
506
    * @param   object   $db DB Connection Object
507
    *
508
    *
509
    * @return   string
510
    * @access   public
511
    * @author   therion 5 at hotmail
512
    */
513
 
514
    function toStringFromTime($to,$db)
515
    {
516
        // first weed out invalid casts..
517
        // in blobs can only be cast to blobs.!
518
        // perhaps we should support TEXT fields???
519
        if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
520
            return PEAR::raiseError('Invalid Cast from a' .
521
                ' DB_DataObject_Cast::time to something other than a time!'.
522
                ' (try using native features)');
523
        }
524
        return "'{$this->hour}:{$this->minute}:{$this->second}'";
525
    }
526
 
527
    /**
528
    * get the string to use in the SQL statement for a raw sql statement.
529
    *
530
    * @param   int      $to Type (DB_DATAOBJECT_*
531
    * @param   object   $db DB Connection Object
532
    *
533
    *
534
    * @return   string
535
    * @access   public
536
    */
537
    function toStringFromSql($to,$db)
538
    {
539
        return $this->value;
540
    }
541
 
542
 
543
 
544
 
545
}
546