DateRangeList.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 DateRangeList extends BasePrimitive implements Stringable
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                     count($scope[$this->name]) == 1
00038                     && !current($scope[$this->name])
00039                 )
00040             )
00041                 return null;
00042             
00043             $this->raw = $scope[$this->name];
00044             $this->imported = true;
00045             $list = array();
00046             
00047             foreach ($this->raw as $string) {
00048                 $rangeList = self::stringToDateRangeList($string);
00049                 
00050                 if ($rangeList)
00051                     foreach ($rangeList as $range)
00052                         $list[] = $range;
00053             }
00054             
00055             $this->value = $list;
00056             
00057             return ($this->value !== array());
00058         }
00059         
00060         public function toString()
00061         {
00062             if ($this->value) {
00063                 $out = array();
00064                 
00065                 foreach ($this->value as $range)
00066                     $out[] = $range->toDateString();
00067                     
00068                 return implode(', ', $out);
00069             }
00070             
00071             return null;
00072         }
00073         
00074         public static function stringToDateRangeList($string)
00075         {
00076             $list = array();
00077             
00078             if ($string) {
00079                 if (strpos($string, ',') !== false)
00080                     $dates = explode(',', $string);
00081                 else
00082                     $dates = array($string);
00083                 
00084                 foreach ($dates as $date) {
00085                     try {
00086                         $list[] = self::makeRange($date);
00087                     } catch (WrongArgumentException $e) {
00088                         // ignore?
00089                     }
00090                 }
00091             }
00092             
00093             return $list;
00094         }
00095         
00100         public static function makeRange($string)
00101         {
00102             if (
00103                 (substr_count($string, ' - ') === 1)
00104                 || (substr_count($string, '-') === 1)
00105             ) {
00106                 $delimiter = ' - ';
00107                 
00108                 if (substr_count($string, '-') === 1)
00109                     $delimiter = '-';
00110                 
00111                 list($start, $finish) = explode($delimiter, $string, 2);
00112                 
00113                 $start = self::toDate(trim($start));
00114                 $finish = self::toDate(trim($finish));
00115                 
00116                 if ($start || $finish) {
00117                     
00118                     $range = new DateRange();
00119                     
00120                     $range =
00121                         DateRange::create()->
00122                         lazySet($start, $finish);
00123                     
00124                     return $range;
00125                     
00126                 } elseif (trim($string) == '-')
00127                     return DateRange::create();
00128             } elseif ($single = self::toDate(trim($string)))
00129                 return
00130                     DateRange::create()->
00131                     setStart($single)->
00132                     setEnd($single);
00133             
00134             throw new WrongArgumentException(
00135                 "unknown string format '{$string}'"
00136             );
00137         }
00138         
00143         private static function toDate($date)
00144         {
00145             if (strpos($date, '.') !== false) {
00146                 
00147                 $fieldCount = substr_count($date, '.') + 1;
00148                 
00149                 $year = null;
00150                 
00151                 if ($fieldCount == 3) {
00152                     list($day, $month, $year) = explode('.', $date, $fieldCount);
00153                     
00154                     if (strlen($day) > 2) {
00155                         $tmp = $year;
00156                         $year = $day;
00157                         $day = $tmp;
00158                     }
00159                 } else
00160                     list($day, $month) = explode('.', $date, $fieldCount);
00161                 
00162                 if (strlen($day) == 1)
00163                     $day = "0{$day}";
00164                 
00165                 if ($month === null)
00166                     $month = date('m');
00167                 elseif (strlen($month) == 1)
00168                     $month = "0{$month}";
00169                 
00170                 $currentYear = date('Y');
00171                 if ($year === null)
00172                     $year = $currentYear;
00173                 elseif (strlen($year) === 2)
00174                     $year = substr_replace($currentYear, $year, -2, 2);
00175                 
00176                 $date = $year.$month.$day;
00177             }
00178             
00179             $lenght = strlen($date);
00180             
00181             if ($lenght > 4) {
00182                 return new Date(strtotime($date));
00183             } elseif ($lenght === 4) {
00184                 return new Date(
00185                     strtotime(
00186                         date('Y-').substr($date, 2).'-'.substr($date, 0, 2)
00187                     )
00188                 );
00189             } elseif (($lenght == 2) || ($lenght == 1)) {
00190                 return new Date(strtotime(date('Y-m-').$date));
00191             }
00192             
00193             return null;
00194         }
00195         
00196         public function exportValue()
00197         {
00198             // cannot use toString() because of different delimiters
00199             throw new UnimplementedFeatureException();
00200         }
00201     }
00202 ?>