Available.php 3.3 KB

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