TimeList.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2008 by Konstantin V. Arkhipov, 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     final class TimeList extends BasePrimitive
00016     {
00017         protected $value = array();
00018         
00022         public function clean()
00023         {
00024             parent::clean();
00025             
00026             $this->value = array();
00027             
00028             return $this;
00029         }
00030         
00031         public function import($scope)
00032         {
00033             if (
00034                 empty($scope[$this->name])
00035                 || !is_array($scope[$this->name])
00036             )
00037                 return null;
00038             
00039             $this->raw = $scope[$this->name];
00040             $this->imported = true;
00041             
00042             $array = $scope[$this->name];
00043             $list = array();
00044             
00045             foreach ($array as $string) {
00046                 $timeList = self::stringToTimeList($string);
00047                 
00048                 if ($timeList)
00049                     $list[] = $timeList;
00050             }
00051             
00052             $this->value = $list;
00053             
00054             return ($this->value !== array());
00055         }
00056         
00057         public function getActualValue()
00058         {
00059             if (is_array($this->value) && $this->value[0])
00060                 return $this->value;
00061             elseif (is_array($this->raw) && $this->raw[0])
00062                 return $this->raw;
00063             
00064             return array($this->default);
00065         }
00066         
00067         public static function stringToTimeList($string)
00068         {
00069             $list = array();
00070             
00071             $times = split("([,; \n]+)", $string);
00072             
00073             for ($i = 0, $size = count($times); $i < $size; ++$i) {
00074                 $time = mb_ereg_replace('[^0-9:]', ':', $times[$i]);
00075                 
00076                 try {
00077                     $list[] = Time::create($time);
00078                 } catch (WrongArgumentException $e) {/* ignore */}
00079             }
00080             
00081             return $list;
00082         }
00083         
00084         public function exportValue()
00085         {
00086             throw new UnimplementedFeatureException();
00087         }
00088     }
00089 ?>