diff options
Diffstat (limited to 'vendor/sabre/vobject/lib/FreeBusyGenerator.php')
-rw-r--r-- | vendor/sabre/vobject/lib/FreeBusyGenerator.php | 220 |
1 files changed, 132 insertions, 88 deletions
diff --git a/vendor/sabre/vobject/lib/FreeBusyGenerator.php b/vendor/sabre/vobject/lib/FreeBusyGenerator.php index adb214c08..e30b136c4 100644 --- a/vendor/sabre/vobject/lib/FreeBusyGenerator.php +++ b/vendor/sabre/vobject/lib/FreeBusyGenerator.php @@ -23,8 +23,8 @@ use Sabre\VObject\Recur\NoInstancesException; * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -class FreeBusyGenerator -{ +class FreeBusyGenerator { + /** * Input objects. * @@ -71,7 +71,7 @@ class FreeBusyGenerator /** * A VAVAILABILITY document. * - * If this is set, its information will be included when calculating + * If this is set, it's information will be included when calculating * freebusy time. * * @var Document @@ -86,11 +86,11 @@ class FreeBusyGenerator * * @param DateTimeInterface $start * @param DateTimeInterface $end - * @param mixed $objects - * @param DateTimeZone $timeZone + * @param mixed $objects + * @param DateTimeZone $timeZone */ - public function __construct(DateTimeInterface $start = null, DateTimeInterface $end = null, $objects = null, DateTimeZone $timeZone = null) - { + function __construct(DateTimeInterface $start = null, DateTimeInterface $end = null, $objects = null, DateTimeZone $timeZone = null) { + $this->setTimeRange($start, $end); if ($objects) { @@ -100,6 +100,7 @@ class FreeBusyGenerator $timeZone = new DateTimeZone('UTC'); } $this->setTimeZone($timeZone); + } /** @@ -111,20 +112,24 @@ class FreeBusyGenerator * The VFREEBUSY object will be automatically added though. * * @param Document $vcalendar + * @return void */ - public function setBaseObject(Document $vcalendar) - { + function setBaseObject(Document $vcalendar) { + $this->baseObject = $vcalendar; + } /** * Sets a VAVAILABILITY document. * * @param Document $vcalendar + * @return void */ - public function setVAvailability(Document $vcalendar) - { + function setVAvailability(Document $vcalendar) { + $this->vavailability = $vcalendar; + } /** @@ -135,15 +140,18 @@ class FreeBusyGenerator * It's also possible to specify multiple objects as an array. * * @param mixed $objects + * + * @return void */ - public function setObjects($objects) - { + function setObjects($objects) { + if (!is_array($objects)) { $objects = [$objects]; } $this->objects = []; foreach ($objects as $object) { + if (is_string($object) || is_resource($object)) { $this->objects[] = Reader::read($object); } elseif ($object instanceof Component) { @@ -151,7 +159,9 @@ class FreeBusyGenerator } else { throw new \InvalidArgumentException('You can only pass strings or \\Sabre\\VObject\\Component arguments to setObjects'); } + } + } /** @@ -161,9 +171,11 @@ class FreeBusyGenerator * * @param DateTimeInterface $start * @param DateTimeInterface $end + * + * @return void */ - public function setTimeRange(DateTimeInterface $start = null, DateTimeInterface $end = null) - { + function setTimeRange(DateTimeInterface $start = null, DateTimeInterface $end = null) { + if (!$start) { $start = new DateTimeImmutable(Settings::$minDate); } @@ -172,16 +184,20 @@ class FreeBusyGenerator } $this->start = $start; $this->end = $end; + } /** * Sets the reference timezone for floating times. * * @param DateTimeZone $timeZone + * + * @return void */ - public function setTimeZone(DateTimeZone $timeZone) - { + function setTimeZone(DateTimeZone $timeZone) { + $this->timeZone = $timeZone; + } /** @@ -190,19 +206,23 @@ class FreeBusyGenerator * * @return Component */ - public function getResult() - { + function getResult() { + $fbData = new FreeBusyData( $this->start->getTimeStamp(), $this->end->getTimeStamp() ); if ($this->vavailability) { + $this->calculateAvailability($fbData, $this->vavailability); + } $this->calculateBusy($fbData, $this->objects); return $this->generateFreeBusyCalendar($fbData); + + } /** @@ -210,30 +230,29 @@ class FreeBusyGenerator * available times. * * @param FreeBusyData $fbData - * @param VCalendar $vavailability + * @param VCalendar $vavailability + * @return void */ - protected function calculateAvailability(FreeBusyData $fbData, VCalendar $vavailability) - { + protected function calculateAvailability(FreeBusyData $fbData, VCalendar $vavailability) { + $vavailComps = iterator_to_array($vavailability->VAVAILABILITY); usort( $vavailComps, - function ($a, $b) { + function($a, $b) { + // We need to order the components by priority. Priority 1 // comes first, up until priority 9. Priority 0 comes after // priority 9. No priority implies priority 0. // // Yes, I'm serious. - $priorityA = isset($a->PRIORITY) ? (int) $a->PRIORITY->getValue() : 0; - $priorityB = isset($b->PRIORITY) ? (int) $b->PRIORITY->getValue() : 0; + $priorityA = isset($a->PRIORITY) ? (int)$a->PRIORITY->getValue() : 0; + $priorityB = isset($b->PRIORITY) ? (int)$b->PRIORITY->getValue() : 0; - if (0 === $priorityA) { - $priorityA = 10; - } - if (0 === $priorityB) { - $priorityB = 10; - } + if ($priorityA === 0) $priorityA = 10; + if ($priorityB === 0) $priorityB = 10; return $priorityA - $priorityB; + } ); @@ -248,6 +267,7 @@ class FreeBusyGenerator $new = []; foreach ($old as $vavail) { + list($compStart, $compEnd) = $vavail->getEffectiveStartEnd(); // We don't care about datetimes that are earlier or later than the @@ -268,19 +288,24 @@ class FreeBusyGenerator // Going through our existing list of components to see if there's // a higher priority component that already fully covers this one. foreach ($new as $higherVavail) { + list($higherStart, $higherEnd) = $higherVavail->getEffectiveStartEnd(); if ( (is_null($higherStart) || $higherStart < $compStart) && (is_null($higherEnd) || $higherEnd > $compEnd) ) { + // Component is fully covered by a higher priority // component. We can skip this component. continue 2; + } + } // We're keeping it! $new[] = $vavail; + } // Lastly, we need to traverse the remaining components and fill in the @@ -289,6 +314,7 @@ class FreeBusyGenerator // We traverse the components in reverse, because we want the higher // priority components to override the lower ones. foreach (array_reverse($new) as $vavail) { + $busyType = isset($vavail->BUSYTYPE) ? strtoupper($vavail->BUSYTYPE) : 'BUSY-UNAVAILABLE'; list($vavailStart, $vavailEnd) = $vavail->getEffectiveStartEnd(); @@ -310,53 +336,57 @@ class FreeBusyGenerator ); // Looping over the AVAILABLE components. - if (isset($vavail->AVAILABLE)) { - foreach ($vavail->AVAILABLE as $available) { - list($availStart, $availEnd) = $available->getEffectiveStartEnd(); - $fbData->add( + if (isset($vavail->AVAILABLE)) foreach ($vavail->AVAILABLE as $available) { + + list($availStart, $availEnd) = $available->getEffectiveStartEnd(); + $fbData->add( $availStart->getTimeStamp(), $availEnd->getTimeStamp(), 'FREE' ); - if ($available->RRULE) { - // Our favourite thing: recurrence!! + if ($available->RRULE) { + // Our favourite thing: recurrence!! - $rruleIterator = new Recur\RRuleIterator( + $rruleIterator = new Recur\RRuleIterator( $available->RRULE->getValue(), $availStart ); - $rruleIterator->fastForward($vavailStart); + $rruleIterator->fastForward($vavailStart); - $startEndDiff = $availStart->diff($availEnd); + $startEndDiff = $availStart->diff($availEnd); - while ($rruleIterator->valid()) { - $recurStart = $rruleIterator->current(); - $recurEnd = $recurStart->add($startEndDiff); + while ($rruleIterator->valid()) { - if ($recurStart > $vavailEnd) { - // We're beyond the legal timerange. - break; - } + $recurStart = $rruleIterator->current(); + $recurEnd = $recurStart->add($startEndDiff); - if ($recurEnd > $vavailEnd) { - // Truncating the end if it exceeds the - // VAVAILABILITY end. - $recurEnd = $vavailEnd; - } + if ($recurStart > $vavailEnd) { + // We're beyond the legal timerange. + break; + } - $fbData->add( + if ($recurEnd > $vavailEnd) { + // Truncating the end if it exceeds the + // VAVAILABILITY end. + $recurEnd = $vavailEnd; + } + + $fbData->add( $recurStart->getTimeStamp(), $recurEnd->getTimeStamp(), 'FREE' ); - $rruleIterator->next(); - } + $rruleIterator->next(); + } } + } + } + } /** @@ -364,25 +394,28 @@ class FreeBusyGenerator * times on fbData. * * @param FreeBusyData $fbData - * @param VCalendar[] $objects + * @param VCalendar[] $objects */ - protected function calculateBusy(FreeBusyData $fbData, array $objects) - { + protected function calculateBusy(FreeBusyData $fbData, array $objects) { + foreach ($objects as $key => $object) { + foreach ($object->getBaseComponents() as $component) { + switch ($component->name) { - case 'VEVENT': + + case 'VEVENT' : $FBTYPE = 'BUSY'; - if (isset($component->TRANSP) && ('TRANSPARENT' === strtoupper($component->TRANSP))) { + if (isset($component->TRANSP) && (strtoupper($component->TRANSP) === 'TRANSPARENT')) { break; } if (isset($component->STATUS)) { $status = strtoupper($component->STATUS); - if ('CANCELLED' === $status) { + if ($status === 'CANCELLED') { break; } - if ('TENTATIVE' === $status) { + if ($status === 'TENTATIVE') { $FBTYPE = 'BUSY-TENTATIVE'; } } @@ -391,13 +424,13 @@ class FreeBusyGenerator if ($component->RRULE) { try { - $iterator = new EventIterator($object, (string) $component->UID, $this->timeZone); + $iterator = new EventIterator($object, (string)$component->UID, $this->timeZone); } catch (NoInstancesException $e) { // This event is recurring, but it doesn't have a single // instance. We are skipping this event from the output // entirely. unset($this->objects[$key]); - break; + continue; } if ($this->start) { @@ -407,6 +440,7 @@ class FreeBusyGenerator $maxRecurrences = Settings::$maxRecurrences; while ($iterator->valid() && --$maxRecurrences) { + $startTime = $iterator->getDTStart(); if ($this->end && $startTime > $this->end) { break; @@ -417,8 +451,11 @@ class FreeBusyGenerator ]; $iterator->next(); + } + } else { + $startTime = $component->DTSTART->getDateTime($this->timeZone); if ($this->end && $startTime > $this->end) { break; @@ -427,7 +464,7 @@ class FreeBusyGenerator if (isset($component->DTEND)) { $endTime = $component->DTEND->getDateTime($this->timeZone); } elseif (isset($component->DURATION)) { - $duration = DateTimeParser::parseDuration((string) $component->DURATION); + $duration = DateTimeParser::parseDuration((string)$component->DURATION); $endTime = clone $startTime; $endTime = $endTime->add($duration); } elseif (!$component->DTSTART->hasTime()) { @@ -439,15 +476,13 @@ class FreeBusyGenerator } $times[] = [$startTime, $endTime]; + } foreach ($times as $time) { - if ($this->end && $time[0] > $this->end) { - break; - } - if ($this->start && $time[1] < $this->start) { - break; - } + + if ($this->end && $time[0] > $this->end) break; + if ($this->start && $time[1] < $this->start) break; $fbData->add( $time[0]->getTimeStamp(), @@ -457,21 +492,21 @@ class FreeBusyGenerator } break; - case 'VFREEBUSY': + case 'VFREEBUSY' : foreach ($component->FREEBUSY as $freebusy) { + $fbType = isset($freebusy['FBTYPE']) ? strtoupper($freebusy['FBTYPE']) : 'BUSY'; // Skipping intervals marked as 'free' - if ('FREE' === $fbType) { + if ($fbType === 'FREE') continue; - } $values = explode(',', $freebusy); foreach ($values as $value) { list($startTime, $endTime) = explode('/', $value); $startTime = DateTimeParser::parseDateTime($startTime); - if ('P' === substr($endTime, 0, 1) || '-P' === substr($endTime, 0, 2)) { + if (substr($endTime, 0, 1) === 'P' || substr($endTime, 0, 2) === '-P') { $duration = DateTimeParser::parseDuration($endTime); $endTime = clone $startTime; $endTime = $endTime->add($duration); @@ -479,23 +514,27 @@ class FreeBusyGenerator $endTime = DateTimeParser::parseDateTime($endTime); } - if ($this->start && $this->start > $endTime) { - continue; - } - if ($this->end && $this->end < $startTime) { - continue; - } + if ($this->start && $this->start > $endTime) continue; + if ($this->end && $this->end < $startTime) continue; $fbData->add( $startTime->getTimeStamp(), $endTime->getTimeStamp(), $fbType ); + } + + } break; + } + + } + } + } /** @@ -504,8 +543,8 @@ class FreeBusyGenerator * * @return VCalendar */ - protected function generateFreeBusyCalendar(FreeBusyData $fbData) - { + protected function generateFreeBusyCalendar(FreeBusyData $fbData) { + if ($this->baseObject) { $calendar = $this->baseObject; } else { @@ -532,29 +571,34 @@ class FreeBusyGenerator $vfreebusy->add($dtstamp); foreach ($fbData->getData() as $busyTime) { + $busyType = strtoupper($busyTime['type']); // Ignoring all the FREE parts, because those are already assumed. - if ('FREE' === $busyType) { + if ($busyType === 'FREE') { continue; } - $busyTime[0] = new \DateTimeImmutable('@'.$busyTime['start'], $tz); - $busyTime[1] = new \DateTimeImmutable('@'.$busyTime['end'], $tz); + $busyTime[0] = new \DateTimeImmutable('@' . $busyTime['start'], $tz); + $busyTime[1] = new \DateTimeImmutable('@' . $busyTime['end'], $tz); $prop = $calendar->createProperty( 'FREEBUSY', - $busyTime[0]->format('Ymd\\THis\\Z').'/'.$busyTime[1]->format('Ymd\\THis\\Z') + $busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z') ); // Only setting FBTYPE if it's not BUSY, because BUSY is the // default anyway. - if ('BUSY' !== $busyType) { + if ($busyType !== 'BUSY') { $prop['FBTYPE'] = $busyType; } $vfreebusy->add($prop); + } return $calendar; + + } + } |