00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 class PrimitiveDate extends ComplexPrimitive
00017 {
00018 const DAY = 'day';
00019 const MONTH = 'month';
00020 const YEAR = 'year';
00021
00026 public function setValue( $object)
00027 {
00028 $this->checkType($object);
00029
00030 $this->value = $object;
00031
00032 return $this;
00033 }
00034
00039 public function setMin( $object)
00040 {
00041 $this->checkType($object);
00042
00043 $this->min = $object;
00044
00045 return $this;
00046 }
00047
00052 public function setMax( $object)
00053 {
00054 $this->checkType($object);
00055
00056 $this->max = $object;
00057
00058 return $this;
00059 }
00060
00065 public function setDefault( $object)
00066 {
00067 $this->checkType($object);
00068
00069 $this->default = $object;
00070
00071 return $this;
00072 }
00073
00074 public function importSingle($scope)
00075 {
00076 if (
00077 BasePrimitive::import($scope)
00078 && (
00079 is_string($scope[$this->name])
00080 || is_numeric($scope[$this->name])
00081 )
00082 ) {
00083 try {
00084 $class = $this->getObjectName();
00085 $ts = new $class($scope[$this->name]);
00086 } catch (WrongArgumentException $e) {
00087 return false;
00088 }
00089
00090 if ($this->checkRanges($ts)) {
00091 $this->value = $ts;
00092 return true;
00093 }
00094 }
00095
00096 return false;
00097 }
00098
00099 public function isEmpty($scope)
00100 {
00101 if ($this->getState()->isFalse()) {
00102 return empty($scope[$this->name][self::DAY])
00103 && empty($scope[$this->name][self::MONTH])
00104 && empty($scope[$this->name][self::YEAR]);
00105 } else
00106 return empty($scope[$this->name]);
00107 }
00108
00109 public function importMarried($scope)
00110 {
00111 if (
00112 BasePrimitive::import($scope)
00113 && isset(
00114 $scope[$this->name][self::DAY],
00115 $scope[$this->name][self::MONTH],
00116 $scope[$this->name][self::YEAR]
00117 )
00118 && is_array($scope[$this->name])
00119 ) {
00120 if ($this->isEmpty($scope))
00121 return !$this->isRequired();
00122
00123 $year = (int) $scope[$this->name][self::YEAR];
00124 $month = (int) $scope[$this->name][self::MONTH];
00125 $day = (int) $scope[$this->name][self::DAY];
00126
00127 if (!checkdate($month, $day, $year))
00128 return false;
00129
00130 try {
00131 $date = new Date(
00132 $year.'-'.$month.'-'.$day
00133 );
00134 } catch (WrongArgumentException $e) {
00135
00136 return false;
00137 }
00138
00139 if ($this->checkRanges($date)) {
00140 $this->value = $date;
00141 return true;
00142 }
00143 }
00144
00145 return false;
00146 }
00147
00148 public function importValue($value)
00149 {
00150 if ($value)
00151 $this->checkType($value);
00152 else
00153 return parent::importValue(null);
00154
00155 $singleScope = array($this->getName() => $value->toString());
00156 $marriedRaw =
00157 array (
00158 self::DAY => $value->getDay(),
00159 self::MONTH => $value->getMonth(),
00160 self::YEAR => $value->getYear(),
00161 );
00162
00163 if ($value instanceof Timestamp) {
00164 $marriedRaw[PrimitiveTimestamp::HOURS] = $value->getHour();
00165 $marriedRaw[PrimitiveTimestamp::MINUTES] = $value->getMinute();
00166 $marriedRaw[PrimitiveTimestamp::SECONDS] = $value->getSecond();
00167 }
00168
00169 $marriedScope = array($this->getName() => $marriedRaw);
00170
00171 if ($this->getState()->isTrue())
00172 return $this->importSingle($singleScope);
00173 elseif ($this->getState()->isFalse())
00174 return $this->importMarried($marriedScope);
00175 else {
00176 if (!$this->importMarried($marriedScope))
00177 return $this->importSingle($singleScope);
00178
00179 return $this->imported = true;
00180 }
00181 }
00182
00183 protected function checkRanges(Date $date)
00184 {
00185 return
00186 (!$this->min || ($this->min->toStamp() <= $date->toStamp()))
00187 && (!$this->max || ($this->max->toStamp() >= $date->toStamp()));
00188 }
00189
00190 protected function getObjectName()
00191 {
00192 return 'Date';
00193 }
00194
00195 protected function checkType($object)
00196 {
00197 Assert::isTrue(
00198 ClassUtils::isInstanceOf($object, $this->getObjectName())
00199 );
00200 }
00201 }
00202 ?>