00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2007 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 /* $Id: PrimitiveDateRange.class.php 4687 2007-12-09 18:57:18Z voxus $ */ 00012 00016 final class PrimitiveDateRange extends FiltrablePrimitive 00017 { 00018 private $className = null; 00019 00023 public static function create($name) 00024 { 00025 return new self($name); 00026 } 00027 00032 public function of($class) 00033 { 00034 Assert::isTrue( 00035 ClassUtils::isInstanceOf($class, $this->getObjectName()) 00036 ); 00037 00038 $this->className = $class; 00039 00040 return $this; 00041 } 00042 00047 public function setDefault(/* DateRange */ $object) 00048 { 00049 $this->checkType($object); 00050 00051 $this->default = $object; 00052 00053 return $this; 00054 } 00055 00056 public function importValue($value) 00057 { 00058 try { 00059 if ($value) { 00060 $this->checkType($value); 00061 00062 if ($this->checkRanges($value)) { 00063 $this->value = $value; 00064 return true; 00065 } else { 00066 return false; 00067 } 00068 } else { 00069 return parent::importValue(null); 00070 } 00071 } catch (WrongArgumentException $e) { 00072 return false; 00073 } 00074 } 00075 00076 public function import($scope) 00077 { 00078 if (parent::import($scope)) { 00079 try { 00080 $range = DateRangeList::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 /* void */ private function checkType($object) 00123 { 00124 if ($this->className) 00125 Assert::isTrue( 00126 ClassUtils::isInstanceOf($object, $this->className) 00127 ); 00128 else 00129 Assert::isTrue( 00130 ClassUtils::isInstanceOf($object, $this->getObjectName()) 00131 ); 00132 } 00133 } 00134 ?>