Schedule.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. namespace Schedule;
  14. class Schedule {
  15. // Tests whether the given ISO8601 string has a time-of-day or not
  16. const ALL_DAY_REGEX = '/^\d{4}-\d\d-\d\d$/'; // matches strings like "2013-12-29"
  17. public $title;
  18. public $allDay; // a boolean
  19. public $start; // a DateTime
  20. public $end; // a DateTime, or null
  21. public $properties = array(); // an array of other misc properties
  22. // Constructs an Event object from the given array of key=>values.
  23. // You can optionally force the timeZone of the parsed dates.
  24. public function __construct($array, $timeZone=null) {
  25. $this->title = $array['title'];
  26. if (isset($array['allDay'])) {
  27. // allDay has been explicitly specified
  28. $this->allDay = (bool)$array['allDay'];
  29. }
  30. else {
  31. // Guess allDay based off of ISO8601 date strings
  32. $this->allDay = preg_match(self::ALL_DAY_REGEX, $array['start']) &&
  33. (!isset($array['end']) || preg_match(self::ALL_DAY_REGEX, $array['end']));
  34. }
  35. if ($this->allDay) {
  36. // If dates are allDay, we want to parse them in UTC to avoid DST issues.
  37. $timeZone = null;
  38. }
  39. // Parse dates
  40. $this->start = parseDateTime($array['start'], $timeZone);
  41. $this->end = isset($array['end']) ? parseDateTime($array['end'], $timeZone) : null;
  42. // Record misc properties
  43. foreach ($array as $name => $value) {
  44. if (!in_array($name, array('title', 'allDay', 'start', 'end'))) {
  45. $this->properties[$name] = $value;
  46. }
  47. }
  48. }
  49. // Returns whether the date range of our event intersects with the given all-day range.
  50. // $rangeStart and $rangeEnd are assumed to be dates in UTC with 00:00:00 time.
  51. public function isWithinDayRange($rangeStart, $rangeEnd) {
  52. // Normalize our event's dates for comparison with the all-day range.
  53. $eventStart = stripTime($this->start);
  54. if (isset($this->end)) {
  55. $eventEnd = stripTime($this->end); // normalize
  56. }
  57. else {
  58. $eventEnd = $eventStart; // consider this a zero-duration event
  59. }
  60. // Check if the two whole-day ranges intersect.
  61. return $eventStart < $rangeEnd && $eventEnd >= $rangeStart;
  62. }
  63. // Converts this Event object back to a plain data array, to be used for generating JSON
  64. public function toArray() {
  65. // Start with the misc properties (don't worry, PHP won't affect the original array)
  66. $array = $this->properties;
  67. $array['title'] = $this->title;
  68. // Figure out the date format. This essentially encodes allDay into the date string.
  69. if ($this->allDay) {
  70. $format = 'Y-m-d'; // output like "2013-12-29"
  71. }
  72. else {
  73. $format = 'Y-m-d H:i:s';
  74. // $format = 'c'; // full ISO8601 output, like "2013-12-29T09:00:00+08:00"
  75. }
  76. // Serialize dates into strings
  77. $array['start'] = $this->start->format($format);
  78. if (isset($this->end)) {
  79. $array['end'] = $this->end->format($format);
  80. }
  81. return $array;
  82. }
  83. }