VAvailability.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use DateTimeInterface;
  4. use Sabre\VObject;
  5. /**
  6. * The VAvailability component.
  7. *
  8. * This component adds functionality to a component, specific for VAVAILABILITY
  9. * components.
  10. *
  11. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  12. * @author Ivan Enderlin
  13. * @license http://sabre.io/license/ Modified BSD License
  14. */
  15. class VAvailability extends VObject\Component
  16. {
  17. /**
  18. * Returns true or false depending on if the event falls in the specified
  19. * time-range. This is used for filtering purposes.
  20. *
  21. * The rules used to determine if an event falls within the specified
  22. * time-range is based on:
  23. *
  24. * https://tools.ietf.org/html/draft-daboo-calendar-availability-05#section-3.1
  25. *
  26. * @return bool
  27. */
  28. public function isInTimeRange(DateTimeInterface $start, DateTimeInterface $end)
  29. {
  30. list($effectiveStart, $effectiveEnd) = $this->getEffectiveStartEnd();
  31. return
  32. (is_null($effectiveStart) || $start < $effectiveEnd) &&
  33. (is_null($effectiveEnd) || $end > $effectiveStart)
  34. ;
  35. }
  36. /**
  37. * Returns the 'effective start' and 'effective end' of this VAVAILABILITY
  38. * component.
  39. *
  40. * We use the DTSTART and DTEND or DURATION to determine this.
  41. *
  42. * The returned value is an array containing DateTimeImmutable instances.
  43. * If either the start or end is 'unbounded' its value will be null
  44. * instead.
  45. *
  46. * @return array
  47. */
  48. public function getEffectiveStartEnd()
  49. {
  50. $effectiveStart = null;
  51. $effectiveEnd = null;
  52. if (isset($this->DTSTART)) {
  53. $effectiveStart = $this->DTSTART->getDateTime();
  54. }
  55. if (isset($this->DTEND)) {
  56. $effectiveEnd = $this->DTEND->getDateTime();
  57. } elseif ($effectiveStart && isset($this->DURATION)) {
  58. $effectiveEnd = $effectiveStart->add(VObject\DateTimeParser::parseDuration($this->DURATION));
  59. }
  60. return [$effectiveStart, $effectiveEnd];
  61. }
  62. /**
  63. * A simple list of validation rules.
  64. *
  65. * This is simply a list of properties, and how many times they either
  66. * must or must not appear.
  67. *
  68. * Possible values per property:
  69. * * 0 - Must not appear.
  70. * * 1 - Must appear exactly once.
  71. * * + - Must appear at least once.
  72. * * * - Can appear any number of times.
  73. * * ? - May appear, but not more than once.
  74. *
  75. * @var array
  76. */
  77. public function getValidationRules()
  78. {
  79. return [
  80. 'UID' => 1,
  81. 'DTSTAMP' => 1,
  82. 'BUSYTYPE' => '?',
  83. 'CLASS' => '?',
  84. 'CREATED' => '?',
  85. 'DESCRIPTION' => '?',
  86. 'DTSTART' => '?',
  87. 'LAST-MODIFIED' => '?',
  88. 'ORGANIZER' => '?',
  89. 'PRIORITY' => '?',
  90. 'SEQUENCE' => '?',
  91. 'SUMMARY' => '?',
  92. 'URL' => '?',
  93. 'DTEND' => '?',
  94. 'DURATION' => '?',
  95. 'CATEGORIES' => '*',
  96. 'COMMENT' => '*',
  97. 'CONTACT' => '*',
  98. ];
  99. }
  100. /**
  101. * Validates the node for correctness.
  102. *
  103. * The following options are supported:
  104. * Node::REPAIR - May attempt to automatically repair the problem.
  105. * Node::PROFILE_CARDDAV - Validate the vCard for CardDAV purposes.
  106. * Node::PROFILE_CALDAV - Validate the iCalendar for CalDAV purposes.
  107. *
  108. * This method returns an array with detected problems.
  109. * Every element has the following properties:
  110. *
  111. * * level - problem level.
  112. * * message - A human-readable string describing the issue.
  113. * * node - A reference to the problematic node.
  114. *
  115. * The level means:
  116. * 1 - The issue was repaired (only happens if REPAIR was turned on).
  117. * 2 - A warning.
  118. * 3 - An error.
  119. *
  120. * @param int $options
  121. *
  122. * @return array
  123. */
  124. public function validate($options = 0)
  125. {
  126. $result = parent::validate($options);
  127. if (isset($this->DTEND) && isset($this->DURATION)) {
  128. $result[] = [
  129. 'level' => 3,
  130. 'message' => 'DTEND and DURATION cannot both be present',
  131. 'node' => $this,
  132. ];
  133. }
  134. return $result;
  135. }
  136. }