DB_DataObject
[ class tree: DB_DataObject ] [ index: DB_DataObject ] [ all elements ]

Source for file Cast.php

Documentation is available at Cast.php

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

Documentation generated on Mon, 11 Mar 2019 14:28:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.