# PrimitiveDateRange.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2007-2008 by Igor V. Gulyaev                            *
    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 PrimitiveDateRange extends FiltrablePrimitive
    00016     {
    00017         private $className = null;
    00018
    00022         public static function create($name)
    00023         {
    00024             return new self($name);
    00025         }
    00026
    00031         public function of($class)
    00032         {
    00033             Assert::isTrue(
    00034                 ClassUtils::isInstanceOf($class, $this->getObjectName())
    00035             );
    00036
    00037             $this->className = $class;
    00038
    00039             return $this;
    00040         }
    00041
    00046         public function setDefault(/* DateRange */ $object)
    00047         {
    00048             $this->checkType($object);
    00049
    00050             $this->default = $object;
    00051
    00052             return $this;
    00053         }
    00054
    00055         public function importValue($value)
    00056         {
    00057             try {
    00058                 if ($value) {
    00059                     $this->checkType($value);
    00060
    00061                     if ($this->checkRanges($value)) {
    00062                         $this->value = $value;
    00063                         return true;
    00064                     } else {
    00065                         return false;
    00066                     }
    00067                 } else {
    00068                     return parent::importValue(null);
    00069                 }
    00070             } catch (WrongArgumentException $e) {
    00071                 return false;
    00072             }
    00073         }
    00074
    00075         public function import($scope)
    00076         {
    00077             if (parent::import($scope)) {
    00078                 $listName = $this->getObjectName().'List';
    00079                 try {
    00080                     $range = $this->makeRange($scope[$this->name]);
    00081                 } catch (WrongArgumentException $e) {
    00082                     return false;
    00083                 }
    00084
    00085                 if ($this->checkRanges($range)) {
    00086                     if (
    00087                         $this->className
    00088                         && ($this->className != $this->getObjectName())
    00089                     ) {
    00090                         $newRange = new $this->className;
    00091
    00092                         if ($start = $range->getStart())
    00093                             $newRange->setStart($start);
    00094
    00095                         if ($end = $range->getEnd())
    00096                             $newRange->setEnd($end);
    00097
    00098                         $this->value = $newRange;
    00099                         return true;
    00100                     }
    00101
    00102                     $this->value = $range;
    00103                     return true;
    00104                 }
    00105             }
    00106
    00107             return false;
    00108         }
    00109
    00110         protected function getObjectName()
    00111         {
    00112             return 'DateRange';
    00113         }
    00114
    00115         protected function checkRanges(DateRange $range)
    00116         {
    00117             return
    00118                 !($this->min && ($this->min->toStamp() < $range->getStartStamp()))
    00119                 && !($this->max && ($this->max->toStamp() > $range->getEndStamp()));
    00120         }
    00121
    00122         protected function makeRange($string)
    00123         {
    00124             return DateRangeList::makeRange($string);
    00125         }
    00126
    00127         /* void */ private function checkType($object)
    00128         {
    00129             if ($this->className)
    00130                 Assert::isTrue(
    00131                     ClassUtils::isInstanceOf($object, $this->className)
    00132                 );
    00133             else
    00134                 Assert::isTrue(
    00135                     ClassUtils::isInstanceOf($object, $this->getObjectName())
    00136                 );
    00137         }
    00138     }
    00139 ?>