Alpha Framework alpha--model--types
[ class tree: alpha--model--types ] [ index: alpha--model--types ] [ all elements ]

Source for file Date.inc

Documentation is available at Date.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/exceptions/AlphaException.inc';
  4. require_once $config->get('sysRoot').'alpha/model/types/AlphaType.inc';
  5. require_once $config->get('sysRoot').'alpha/model/types/AlphaTypeInterface.inc';
  6.  
  7. /**
  8.  * The Date complex data type
  9.  * 
  10.  * @package alpha::model::types
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: Date.inc 1453 2011-12-04 15:12:54Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class Date extends AlphaType implements AlphaTypeInterface {
  52.     /**
  53.      * The year part
  54.      *
  55.      * @var integer 
  56.      * @since 1.0
  57.      */
  58.     private $year;
  59.     
  60.     /**
  61.      * The month part
  62.      *
  63.      * @var integer 
  64.      * @since 1.0
  65.      */
  66.     private $month;
  67.     
  68.     /**
  69.      * The day part
  70.      *
  71.      * @var integer 
  72.      * @since 1.0
  73.      */
  74.     private $day;
  75.     
  76.     /**
  77.      * The textual version of the day, e.g. Monday
  78.      *
  79.      * @var string 
  80.      * @since 1.0
  81.      */
  82.     private $weekday;
  83.     
  84.     /**
  85.      * The validation rule (reg-ex) applied to Date values
  86.      *
  87.      * @var string 
  88.      * @since 1.0
  89.      */
  90.      private $validationRule;
  91.      
  92.      /**
  93.      * The error message returned for invalid values
  94.      *
  95.      * @var string 
  96.      * @since 1.0
  97.      */
  98.     protected $helper'Not a valid date value!  A date should be in the ISO format YYYY-MM-DD.';
  99.      
  100.     /**
  101.      * Constructor.  Leave $date param empty to default to now.
  102.      *
  103.      * @param string $date Date string in the ISO format YYYY-MM-DD.
  104.      * @since 1.0
  105.      * @throws IllegalArguementException
  106.      */
  107.      public function __construct($date=''{
  108.          global $config;
  109.          
  110.          $this->validationRule AlphaValidator::ALLOW_ALL;
  111.         
  112.          if(empty($date)) {
  113.              if($config->get('sysDefaultDateTime'== 'now'{
  114.                 $this->year=date('Y');
  115.                 $this->month=date('m');
  116.                 $this->day=date('d');
  117.                 $this->weekday=date('l');
  118.              }else{
  119.                  $this->year='0000';
  120.                 $this->month='00';
  121.                 $this->day='00';
  122.              }
  123.         }else{
  124.             if (preg_match($this->validationRule$date)) {
  125.                 $this->populateFromString($date);
  126.             }else{
  127.                 throw new IllegalArguementException($this->helper);
  128.             }
  129.         }
  130.      }
  131.      
  132.      /**
  133.       * Accepts a full date string in ISO YYYY-mm-dd format and populates relevent Date attributes.
  134.       * 
  135.       * @param string $date 
  136.       * @since 1.0
  137.       * @throws IllegalArguementException
  138.       */
  139.      public function setValue($date{
  140.          $this->populateFromString($date);
  141.      }
  142.      
  143.      /**
  144.       * Set the Date attributes to match the three values provided
  145.       *
  146.       * @param integer $year 
  147.       * @param integer $month 
  148.       * @param integer $day 
  149.       * @throws IllegalArguementException
  150.       * @since 1.0
  151.       */
  152.      public function setDateValue($year$month$day{
  153.          $valid null;
  154.          
  155.          if (!preg_match('/^[0-9]{4}$/'$year))
  156.              $valid 'Error: the year value '.$year.' provided is invalid!';
  157.          if (!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$month))
  158.              $valid 'Error: the month value '.$month.' provided is invalid!';
  159.          if (!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$day))
  160.              $valid 'Error: the day value '.$day.' provided is invalid!';
  161.          if (!isset($valid&& !checkdate($month$day$year))
  162.              $valid 'Error: the day value '.$year.'-'.$month.'-'.$day.' provided is invalid!';         
  163.          
  164.          if (isset($valid)){
  165.              throw new IllegalArguementException($valid);
  166.          }else{
  167.              $this->year $year;
  168.              $this->month str_pad($month2'0'STR_PAD_LEFT);
  169.              $this->day str_pad($day2'0'STR_PAD_LEFT);
  170.              $unixTime mktime(000$this->month$this->day$this->year);             
  171.              $this->weekday=date('l'$unixTime);             
  172.          }
  173.      }
  174.      
  175.      /**
  176.       * Get the date value as a string in the format "YYYY-MM-DD"
  177.       *
  178.       * @return string 
  179.       * @since 1.0
  180.       */
  181.      public function getValue({
  182.          return $this->year.'-'.$this->month.'-'.$this->day;
  183.      }
  184.      
  185.     /**
  186.       * Return the value in UNIX timestamp format
  187.       * 
  188.       * @return integer 
  189.       * @since 1.0
  190.       */
  191.      public function getUnixValue({
  192.          return mktime(000$this->month$this->day$this->year);
  193.      }
  194.      
  195.      /**
  196.       * Get the date value as a string in the format "DD/MM/YYYY"
  197.       *
  198.       * @return string 
  199.       * @since 1.0
  200.       */
  201.      public function getEuroValue({
  202.          return $this->day.'/'.$this->month.'/'.substr($this->year22);
  203.      }
  204.      
  205.     /**
  206.       * Get the date value as a string in the format "MM/DD/YYYY"
  207.       *
  208.       * @return string 
  209.       * @since 1.0
  210.       */
  211.      public function getUSValue({
  212.          return $this->month.'/'.$this->day.'/'.substr($this->year22);
  213.      }
  214.      
  215.      /**
  216.       * Get the year part
  217.       *
  218.       * @return integer 
  219.       * @since 1.0
  220.       */
  221.      public function getYear({
  222.          return $this->year;    
  223.      }
  224.      
  225.      /**
  226.       * Get the month part
  227.       *
  228.       * @return integer 
  229.       * @since 1.0
  230.       */
  231.      public function getMonth({
  232.          return $this->month;    
  233.      }
  234.      
  235.      /**
  236.       * Get the day part
  237.       *
  238.       * @return integer 
  239.       * @since 1.0
  240.       */
  241.      public function getDay({
  242.          return $this->day;    
  243.      }
  244.      
  245.      /**
  246.       * Get the textual weekday part, e.g. Monday
  247.       *
  248.       * @return string 
  249.       * @since 1.0
  250.       */
  251.      public function getWeekday({
  252.          return $this->weekday;
  253.      }
  254.           
  255.      /**
  256.       * Accepts a full date string in YYYY-MM-DD format and populates relevent Date attributes.
  257.       * 
  258.       * @param string $date 
  259.       * @throws IllegalArguementException
  260.       * @since 1.0
  261.       */
  262.      public function populateFromString($date{         
  263.          $valid null;
  264.          
  265.          if($date == '' || $date == '0000-00-00'{
  266.              $this->year '0000';
  267.              $this->month '00';
  268.              $this->day '00';
  269.          }else{
  270.              // This is just here for legacy to ensure that any old time value from a Date object is ignored
  271.              $spilt_by_space explode(" "$date);
  272.              
  273.              if(isset($spilt_by_space[0])) {
  274.                  $date $spilt_by_space[0];
  275.              }else{
  276.                  throw new IllegalArguementException('Invalid Date value ['.$date.'] provided!');
  277.              }
  278.              
  279.              $split_by_dash explode("-"$date);
  280.              
  281.              // Parse for the date parts, seperated by "-"
  282.              if(isset($split_by_dash[0]&& isset($split_by_dash[1]&& isset($split_by_dash[2])) {
  283.                  $year $split_by_dash[0];
  284.                  $month $split_by_dash[1];
  285.                  $day $split_by_dash[2];
  286.              }else{
  287.                  throw new IllegalArguementException('Invalid Date value ['.$date.'] provided!');
  288.              }
  289.              
  290.              if (!preg_match('/^[0-9]{4}$/'$year))
  291.                  $valid 'Error: the year value '.$year.' provided is invalid!';
  292.              if (!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$month))
  293.                  $valid 'Error: the month value '.$month.' provided is invalid!';
  294.              if (!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$day))
  295.                  $valid 'Error: the day value '.$day.' provided is invalid!';
  296.              if (!isset($valid&& !checkdate($month$day$year))
  297.                  $valid 'Error: the day value '.$year.'/'.$month.'/'.$day.' provided is invalid!';
  298.                       
  299.              if (isset($valid)){
  300.                  throw new IllegalArguementException($valid);
  301.              }else{
  302.                  $this->year $year;
  303.                  $this->month str_pad($month2'0'STR_PAD_LEFT);
  304.                  $this->day str_pad($day2'0'STR_PAD_LEFT);
  305.                  $unixTime mktime(000$this->month$this->day$this->year);             
  306.                  $this->weekday=date('l'$unixTime);             
  307.              }
  308.          }
  309.      }
  310.      
  311.      /**
  312.       * Get the validation rule
  313.       *
  314.       * @return string 
  315.       * @since 1.0
  316.       */
  317.      public function getRule({
  318.         return $this->validationRule;
  319.     }
  320.     
  321.     /**
  322.      * Set the validation rule
  323.      *
  324.      * @param string $rule 
  325.      * @since 1.0
  326.      */
  327.     public function setRule($rule{
  328.         $this->validationRule $rule;
  329.     }    
  330. }
  331.  
  332. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:38 +0000 by phpDocumentor 1.4.3