VFreeBusy.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use DateTimeInterface;
  4. use Sabre\VObject;
  5. /**
  6. * The VFreeBusy component.
  7. *
  8. * This component adds functionality to a component, specific for VFREEBUSY
  9. * components.
  10. *
  11. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://sabre.io/license/ Modified BSD License
  14. */
  15. class VFreeBusy extends VObject\Component
  16. {
  17. /**
  18. * Checks based on the contained FREEBUSY information, if a timeslot is
  19. * available.
  20. *
  21. * @return bool
  22. */
  23. public function isFree(DateTimeInterface $start, DatetimeInterface $end)
  24. {
  25. foreach ($this->select('FREEBUSY') as $freebusy) {
  26. // We are only interested in FBTYPE=BUSY (the default),
  27. // FBTYPE=BUSY-TENTATIVE or FBTYPE=BUSY-UNAVAILABLE.
  28. if (isset($freebusy['FBTYPE']) && 'BUSY' !== strtoupper(substr((string) $freebusy['FBTYPE'], 0, 4))) {
  29. continue;
  30. }
  31. // The freebusy component can hold more than 1 value, separated by
  32. // commas.
  33. $periods = explode(',', (string) $freebusy);
  34. foreach ($periods as $period) {
  35. // Every period is formatted as [start]/[end]. The start is an
  36. // absolute UTC time, the end may be an absolute UTC time, or
  37. // duration (relative) value.
  38. list($busyStart, $busyEnd) = explode('/', $period);
  39. $busyStart = VObject\DateTimeParser::parse($busyStart);
  40. $busyEnd = VObject\DateTimeParser::parse($busyEnd);
  41. if ($busyEnd instanceof \DateInterval) {
  42. $busyEnd = $busyStart->add($busyEnd);
  43. }
  44. if ($start < $busyEnd && $end > $busyStart) {
  45. return false;
  46. }
  47. }
  48. }
  49. return true;
  50. }
  51. /**
  52. * A simple list of validation rules.
  53. *
  54. * This is simply a list of properties, and how many times they either
  55. * must or must not appear.
  56. *
  57. * Possible values per property:
  58. * * 0 - Must not appear.
  59. * * 1 - Must appear exactly once.
  60. * * + - Must appear at least once.
  61. * * * - Can appear any number of times.
  62. * * ? - May appear, but not more than once.
  63. *
  64. * @var array
  65. */
  66. public function getValidationRules()
  67. {
  68. return [
  69. 'UID' => 1,
  70. 'DTSTAMP' => 1,
  71. 'CONTACT' => '?',
  72. 'DTSTART' => '?',
  73. 'DTEND' => '?',
  74. 'ORGANIZER' => '?',
  75. 'URL' => '?',
  76. 'ATTENDEE' => '*',
  77. 'COMMENT' => '*',
  78. 'FREEBUSY' => '*',
  79. 'REQUEST-STATUS' => '*',
  80. ];
  81. }
  82. }