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

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4