PrimitiveTime.class.php

Go to the documentation of this file.
00001 <?php
00002 /****************************************************************************
00003  *   Copyright (C) 2006-2007 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     final class PrimitiveTime extends ComplexPrimitive
00016     {
00017         const HOURS     = PrimitiveTimestamp::HOURS;
00018         const MINUTES   = PrimitiveTimestamp::MINUTES;
00019         const SECONDS   = PrimitiveTimestamp::SECONDS;
00020         
00025         public function setValue(/* Time */ $time)
00026         {
00027             Assert::isTrue($time instanceof Time);
00028 
00029             $this->value = $time;
00030             
00031             return $this;
00032         }
00033         
00038         public function setMin(/* Time */ $time)
00039         {
00040             Assert::isTrue($time instanceof Time);
00041 
00042             $this->min = $time;
00043             
00044             return $this;
00045         }
00046         
00051         public function setMax(/* Time */ $time)
00052         {
00053             Assert::isTrue($time instanceof Time);
00054             
00055             $this->max = $time;
00056             
00057             return $this;
00058         }
00059         
00064         public function setDefault(/* Time */ $time)
00065         {
00066             Assert::isTrue($time instanceof Time);
00067             
00068             $this->default = $time;
00069             
00070             return $this;
00071         }
00072         
00073         public function importSingle($scope)
00074         {
00075             if (!BasePrimitive::import($scope))
00076                 return null;
00077             
00078             try {
00079                 $time = new Time($scope[$this->name]);
00080             } catch (WrongArgumentException $e) {
00081                 return false;
00082             }
00083             
00084             if ($this->checkLimits($time)) {
00085                 $this->value = $time;
00086                 
00087                 return true;
00088             }
00089             
00090             return false;
00091         }
00092         
00093         public function isEmpty($scope)
00094         {
00095             if ($this->getState()->isFalse())
00096                 return $this->isMarriedEmpty($scope);
00097             
00098             return empty($scope[$this->name]);
00099         }
00100         
00101         public function importMarried($scope)
00102         {
00103             if (
00104                 BasePrimitive::import($scope)
00105                 && is_array($scope[$this->name])
00106                 && !$this->isMarriedEmpty($scope)
00107             ) {
00108                 $this->raw = $scope[$this->name];
00109                 $this->imported = true;
00110                 
00111                 $hours = $minutes = $seconds = 0;
00112                 
00113                 if (isset($scope[$this->name][self::HOURS]))
00114                     $hours = (int) $scope[$this->name][self::HOURS];
00115 
00116                 if (isset($scope[$this->name][self::MINUTES]))
00117                     $minutes = (int) $scope[$this->name][self::MINUTES];
00118 
00119                 if (isset($scope[$this->name][self::SECONDS]))
00120                     $seconds = (int) $scope[$this->name][self::SECONDS];
00121                 
00122                 try {
00123                     $time = new Time($hours.':'.$minutes.':'.$seconds);
00124                 } catch (WrongArgumentException $e) {
00125                     return false;
00126                 }
00127                 
00128                 if ($this->checkLimits($time)) {
00129                     $this->value = $time;
00130                     
00131                     return true;
00132                 }
00133             }
00134             
00135             return false;
00136         }
00137         
00138         public function import($scope)
00139         {
00140             if ($this->isEmpty($scope)) {
00141                 $this->value = null;
00142                 $this->raw = null;
00143                 return null;
00144             }
00145 
00146             return parent::import($scope);
00147         }
00148         
00149         public function importValue($value)
00150         {
00151             if ($value)
00152                 Assert::isTrue($value instanceof Time);
00153             else
00154                 return parent::importValue(null);
00155             
00156             return
00157                 $this->importSingle(
00158                     array($this->getName() => $value->toFullString())
00159                 );
00160         }
00161         
00162         private function isMarriedEmpty($scope)
00163         {
00164             return empty($scope[$this->name][self::HOURS])
00165                 || empty($scope[$this->name][self::MINUTES])
00166                 || empty($scope[$this->name][self::SECONDS]);
00167         }
00168 
00169         private function checkLimits(Time $time)
00170         {
00171             return
00172                 !($this->min && $this->min->toSeconds() > $time->toSeconds())
00173                 && !($this->max && $this->max->toSeconds() < $time->toSeconds());
00174         }
00175     }
00176 ?>