/core/Form/Primitives/DateRangeList.class.php

https://github.com/bassta/onphp-framework · PHP · 202 lines · 136 code · 41 blank · 25 comment · 28 complexity · 30739907e66c6ea83b986c4044631a7d MD5 · raw file

  1. <?php
  2. /***************************************************************************
  3. * Copyright (C) 2005-2008 by Konstantin V. Arkhipov, Igor V. Gulyaev *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU Lesser General Public License as *
  7. * published by the Free Software Foundation; either version 3 of the *
  8. * License, or (at your option) any later version. *
  9. * *
  10. ***************************************************************************/
  11. /**
  12. * @ingroup Primitives
  13. **/
  14. final class DateRangeList extends BasePrimitive implements Stringable
  15. {
  16. protected $value = array();
  17. /**
  18. * @return DateRangeList
  19. **/
  20. public function clean()
  21. {
  22. parent::clean();
  23. $this->value = array();
  24. return $this;
  25. }
  26. public function import($scope)
  27. {
  28. if (
  29. empty($scope[$this->name])
  30. || !is_array($scope[$this->name])
  31. || (
  32. count($scope[$this->name]) == 1
  33. && !current($scope[$this->name])
  34. )
  35. )
  36. return null;
  37. $this->raw = $scope[$this->name];
  38. $this->imported = true;
  39. $list = array();
  40. foreach ($this->raw as $string) {
  41. $rangeList = self::stringToDateRangeList($string);
  42. if ($rangeList)
  43. foreach ($rangeList as $range)
  44. $list[] = $range;
  45. }
  46. $this->value = $list;
  47. return ($this->value !== array());
  48. }
  49. public function toString()
  50. {
  51. if ($this->value) {
  52. $out = array();
  53. foreach ($this->value as $range)
  54. $out[] = $range->toDateString();
  55. return implode(', ', $out);
  56. }
  57. return null;
  58. }
  59. public static function stringToDateRangeList($string)
  60. {
  61. $list = array();
  62. if ($string) {
  63. if (strpos($string, ',') !== false)
  64. $dates = explode(',', $string);
  65. else
  66. $dates = array($string);
  67. foreach ($dates as $date) {
  68. try {
  69. $list[] = self::makeRange($date);
  70. } catch (WrongArgumentException $e) {
  71. // ignore?
  72. }
  73. }
  74. }
  75. return $list;
  76. }
  77. /**
  78. * @throws WrongArgumentException
  79. * @return DateRange
  80. **/
  81. public static function makeRange($string)
  82. {
  83. if (
  84. (substr_count($string, ' - ') === 1)
  85. || (substr_count($string, '-') === 1)
  86. ) {
  87. $delimiter = ' - ';
  88. if (substr_count($string, '-') === 1)
  89. $delimiter = '-';
  90. list($start, $finish) = explode($delimiter, $string, 2);
  91. $start = self::toDate(trim($start));
  92. $finish = self::toDate(trim($finish));
  93. if ($start || $finish) {
  94. $range = new DateRange();
  95. $range =
  96. DateRange::create()->
  97. lazySet($start, $finish);
  98. return $range;
  99. } elseif (trim($string) == '-')
  100. return DateRange::create();
  101. } elseif ($single = self::toDate(trim($string)))
  102. return
  103. DateRange::create()->
  104. setStart($single)->
  105. setEnd($single);
  106. throw new WrongArgumentException(
  107. "unknown string format '{$string}'"
  108. );
  109. }
  110. /**
  111. * @throws WrongArgumentException
  112. * @return Date
  113. **/
  114. private static function toDate($date)
  115. {
  116. if (strpos($date, '.') !== false) {
  117. $fieldCount = substr_count($date, '.') + 1;
  118. $year = null;
  119. if ($fieldCount == 3) {
  120. list($day, $month, $year) = explode('.', $date, $fieldCount);
  121. if (strlen($day) > 2) {
  122. $tmp = $year;
  123. $year = $day;
  124. $day = $tmp;
  125. }
  126. } else
  127. list($day, $month) = explode('.', $date, $fieldCount);
  128. if (strlen($day) == 1)
  129. $day = "0{$day}";
  130. if ($month === null)
  131. $month = date('m');
  132. elseif (strlen($month) == 1)
  133. $month = "0{$month}";
  134. $currentYear = date('Y');
  135. if ($year === null)
  136. $year = $currentYear;
  137. elseif (strlen($year) === 2)
  138. $year = substr_replace($currentYear, $year, -2, 2);
  139. $date = $year.$month.$day;
  140. }
  141. $lenght = strlen($date);
  142. if ($lenght > 4) {
  143. return new Date(strtotime($date));
  144. } elseif ($lenght === 4) {
  145. return new Date(
  146. strtotime(
  147. date('Y-').substr($date, 2).'-'.substr($date, 0, 2)
  148. )
  149. );
  150. } elseif (($lenght == 2) || ($lenght == 1)) {
  151. return new Date(strtotime(date('Y-m-').$date));
  152. }
  153. return null;
  154. }
  155. public function exportValue()
  156. {
  157. // cannot use toString() because of different delimiters
  158. throw new UnimplementedFeatureException();
  159. }
  160. }
  161. ?>