00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class DateRangeList extends BasePrimitive implements Stringable
00017 {
00018 public function import($scope)
00019 {
00020 if (
00021 empty($scope[$this->name])
00022 || !is_array($scope[$this->name])
00023 || (
00024 count($scope[$this->name]) == 1
00025 && !current($scope[$this->name])
00026 )
00027 )
00028 return null;
00029
00030 $this->raw = $scope[$this->name];
00031 $this->imported = true;
00032 $list = array();
00033
00034 foreach ($this->raw as $string) {
00035 $rangeList = self::stringToDateRangeList($string);
00036
00037 if ($rangeList)
00038 foreach ($rangeList as $range)
00039 $list[] = $range;
00040 }
00041
00042 $this->value = $list;
00043
00044 return ($this->value !== array());
00045 }
00046
00047 public function toString()
00048 {
00049 if ($this->value) {
00050 $out = array();
00051
00052 foreach ($this->value as $range)
00053 $out[] = $range->toDateString();
00054
00055 return implode(', ', $out);
00056 }
00057
00058 return null;
00059 }
00060
00061 public static function stringToDateRangeList($string)
00062 {
00063 $list = array();
00064
00065 if ($string) {
00066 if (strpos($string, ',') !== false)
00067 $dates = explode(',', $string);
00068 else
00069 $dates = array($string);
00070
00071 foreach ($dates as $date) {
00072 try {
00073 $list[] = self::makeRange($date);
00074 } catch (WrongArgumentException $e) {
00075
00076 }
00077 }
00078 }
00079
00080 return $list;
00081 }
00082
00087 public static function makeRange($string)
00088 {
00089 if (
00090 (substr_count($string, ' - ') === 1)
00091 || (substr_count($string, '-') === 1)
00092 ) {
00093 $delimiter = ' - ';
00094
00095 if (substr_count($string, '-') === 1)
00096 $delimiter = '-';
00097
00098 list($start, $finish) = explode($delimiter, $string, 2);
00099
00100 $start = self::toDate(trim($start));
00101 $finish = self::toDate(trim($finish));
00102
00103 if ($start || $finish) {
00104
00105 $range = new DateRange();
00106
00107 $range =
00108 DateRange::create()->
00109 lazySet($start, $finish);
00110
00111 return $range;
00112
00113 } elseif (trim($string) == '-')
00114 return DateRange::create();
00115 } elseif ($single = self::toDate(trim($string)))
00116 return
00117 DateRange::create()->
00118 setStart($single)->
00119 setEnd($single);
00120
00121 throw new WrongArgumentException(
00122 "unknown string format '{$string}'"
00123 );
00124 }
00125
00130 private static function toDate($date)
00131 {
00132 if (strpos($date, '.') !== false) {
00133
00134 $fieldCount = substr_count($date, '.') + 1;
00135
00136 $year = null;
00137
00138 if ($fieldCount == 3) {
00139 list($day, $month, $year) = explode('.', $date, $fieldCount);
00140
00141 if (strlen($day) > 2) {
00142 $tmp = $year;
00143 $year = $day;
00144 $day = $tmp;
00145 }
00146 } else
00147 list($day, $month) = explode('.', $date, $fieldCount);
00148
00149 if (strlen($day) == 1)
00150 $day = "0{$day}";
00151
00152 if ($month === null)
00153 $month = date('m');
00154 elseif (strlen($month) == 1)
00155 $month = "0{$month}";
00156
00157 $currentYear = date('Y');
00158 if ($year === null)
00159 $year = $currentYear;
00160 elseif (strlen($year) === 2)
00161 $year = substr_replace($currentYear, $year, -2, 2);
00162
00163 $date = $year.$month.$day;
00164 }
00165
00166 $lenght = strlen($date);
00167
00168 if ($lenght > 4) {
00169 return new Date(strtotime($date));
00170 } elseif ($lenght === 4) {
00171 return new Date(
00172 strtotime(
00173 date('Y-').substr($date, 2).'-'.substr($date, 0, 2)
00174 )
00175 );
00176 } elseif (($lenght == 2) || ($lenght == 1)) {
00177 return new Date(strtotime(date('Y-m-').$date));
00178 }
00179
00180 return null;
00181 }
00182 }
00183 ?>