PrimitiveDate.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2008 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     class PrimitiveDate extends ComplexPrimitive
00016     {
00017         const DAY       = 'day';
00018         const MONTH     = 'month';
00019         const YEAR      = 'year';
00020 
00025         public function setValue(/* Date */ $object)
00026         {
00027             $this->checkType($object);
00028 
00029             $this->value = $object;
00030             
00031             return $this;
00032         }
00033         
00038         public function setMin(/* Date */ $object)
00039         {
00040             $this->checkType($object);
00041 
00042             $this->min = $object;
00043             
00044             return $this;
00045         }
00046         
00051         public function setMax(/* Date */ $object)
00052         {
00053             $this->checkType($object);
00054             
00055             $this->max = $object;
00056             
00057             return $this;
00058         }
00059         
00064         public function setDefault(/* Date */ $object)
00065         {
00066             $this->checkType($object);
00067             
00068             $this->default = $object;
00069             
00070             return $this;
00071         }
00072         
00073         public function importSingle($scope)
00074         {
00075             if (
00076                 BasePrimitive::import($scope)
00077                 && (
00078                     is_string($scope[$this->name])
00079                     || is_numeric($scope[$this->name])
00080                 )
00081             ) {
00082                 try {
00083                     $class = $this->getObjectName();
00084                     $ts = new $class($scope[$this->name]);
00085                 } catch (WrongArgumentException $e) {
00086                     return false;
00087                 }
00088                 
00089                 if ($this->checkRanges($ts)) {
00090                     $this->value = $ts;
00091                     return true;
00092                 }
00093             } elseif ($this->isEmpty($scope)) {
00094                 return null;
00095             }
00096             
00097             return false;
00098         }
00099 
00100         public function isEmpty($scope)
00101         {
00102             if (
00103                 $this->getState()->isFalse()
00104                 || $this->getState()->isNull()
00105             ) {
00106                 return empty($scope[$this->name][self::DAY])
00107                     && empty($scope[$this->name][self::MONTH])
00108                     && empty($scope[$this->name][self::YEAR]);
00109             } else
00110                 return empty($scope[$this->name]);
00111         }
00112         
00113         public function importMarried($scope)
00114         {
00115             if (
00116                 BasePrimitive::import($scope)
00117                 && isset(
00118                     $scope[$this->name][self::DAY],
00119                     $scope[$this->name][self::MONTH],
00120                     $scope[$this->name][self::YEAR]
00121                 )
00122                 && is_array($scope[$this->name])
00123             ) {
00124                 if ($this->isEmpty($scope))
00125                     return !$this->isRequired();
00126 
00127                 $year = (int) $scope[$this->name][self::YEAR];
00128                 $month = (int) $scope[$this->name][self::MONTH];
00129                 $day = (int) $scope[$this->name][self::DAY];
00130                 
00131                 if (!checkdate($month, $day, $year))
00132                     return false;
00133                 
00134                 try {
00135                     $date = new Date(
00136                         $year.'-'.$month.'-'.$day
00137                     );
00138                 } catch (WrongArgumentException $e) {
00139                     // fsck wrong dates
00140                     return false;
00141                 }
00142                 
00143                 if ($this->checkRanges($date)) {
00144                     $this->value = $date;
00145                     return true;
00146                 }
00147             }
00148 
00149             return false;
00150         }
00151         
00152         public function importValue($value)
00153         {
00154             if ($value)
00155                 $this->checkType($value);
00156             else
00157                 return parent::importValue(null);
00158             
00159             $singleScope = array($this->getName() => $value->toString());
00160             $marriedRaw =
00161                 array (
00162                     self::DAY => $value->getDay(),
00163                     self::MONTH => $value->getMonth(),
00164                     self::YEAR => $value->getYear(),
00165                 );
00166             
00167             if ($value instanceof Timestamp) {
00168                 $marriedRaw[PrimitiveTimestamp::HOURS] = $value->getHour();
00169                 $marriedRaw[PrimitiveTimestamp::MINUTES] = $value->getMinute();
00170                 $marriedRaw[PrimitiveTimestamp::SECONDS] = $value->getSecond();
00171             }
00172             
00173             $marriedScope = array($this->getName() => $marriedRaw);
00174             
00175             if ($this->getState()->isTrue())
00176                 return $this->importSingle($singleScope);
00177             elseif ($this->getState()->isFalse())
00178                 return $this->importMarried($marriedScope);
00179             else {
00180                 if (!$this->importMarried($marriedScope))
00181                     return $this->importSingle($singleScope);
00182                 
00183                 return $this->imported = true;
00184             }
00185         }
00186         
00187         public function exportValue()
00188         {
00189             if ($this->value === null) {
00190                 if ($this->getState()->isTrue())
00191                     return null;
00192                 else
00193                     return array(
00194                         self::DAY => null,
00195                         self::MONTH => null,
00196                         self::YEAR => null,
00197                     );
00198             }
00199             
00200             if ($this->getState()->isTrue())
00201                 return $this->value->toString();
00202             else
00203                 return array(
00204                     self::DAY => $this->value->getDay(),
00205                     self::MONTH => $this->value->getMonth(),
00206                     self::YEAR => $this->value->getYear(),
00207                 );
00208         }
00209         
00210         protected function checkRanges(Date $date)
00211         {
00212             return
00213                 (!$this->min || ($this->min->toStamp() <= $date->toStamp()))
00214                 && (!$this->max || ($this->max->toStamp() >= $date->toStamp()));
00215         }
00216         
00217         protected function getObjectName()
00218         {
00219             return 'Date';
00220         }
00221         
00222         /* void */ protected function checkType($object)
00223         {
00224             Assert::isTrue(
00225                 ClassUtils::isInstanceOf($object, $this->getObjectName())
00226             );
00227         }
00228     }
00229 ?>