[Pkg-owncloud-commits] [php-sabre-vobject] 308/341: The FreeBusyGenerator now uses the new FreeBusyData object.

David Prévot taffit at moszumanska.debian.org
Tue Aug 11 13:35:59 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabre-vobject.

commit b99a8bd0f8f643d23329c1e55fdcc59937b8774b
Author: Evert Pot <me at evertpot.com>
Date:   Wed Jul 8 03:46:42 2015 -0400

    The FreeBusyGenerator now uses the new FreeBusyData object.
---
 lib/FreeBusyData.php                    |  32 +++---
 lib/FreeBusyGenerator.php               |  51 ++++++----
 lib/Settings.php                        |  39 ++++++++
 tests/VObject/FreeBusyDataTest.php      | 168 ++++++++++++++++----------------
 tests/VObject/FreeBusyGeneratorTest.php |  16 +--
 5 files changed, 178 insertions(+), 128 deletions(-)

diff --git a/lib/FreeBusyData.php b/lib/FreeBusyData.php
index 0ac4e61..83cc84c 100644
--- a/lib/FreeBusyData.php
+++ b/lib/FreeBusyData.php
@@ -2,8 +2,6 @@
 
 namespace Sabre\VObject;
 
-use DateTimeInterface;
-
 /**
  * FreeBusyData is a helper class that manages freebusy information.
  *
@@ -72,17 +70,17 @@ class FreeBusyData {
         if ($end > $this->end) {
             // The item ends after our requested time range
             $end = $this->end;
-        }  
+        }
 
         // Finding out where we need to insert the new item.
         $currentIndex = 0;
-        while($start > $this->data[$currentIndex]['end']) {
+        while ($start > $this->data[$currentIndex]['end']) {
             $currentIndex++;
         }
 
         // The standard insertion point will be one _after_ the first
         // overlapping item.
-        $insertStartIndex = $currentIndex+1;
+        $insertStartIndex = $currentIndex + 1;
 
         $newItem = [
             'start' => $start,
@@ -90,8 +88,8 @@ class FreeBusyData {
             'type'  => $type,
         ];
 
-        $preceedingItem = $this->data[$insertStartIndex-1];
-        if ($this->data[$insertStartIndex-1]['start'] === $start) {
+        $preceedingItem = $this->data[$insertStartIndex - 1];
+        if ($this->data[$insertStartIndex - 1]['start'] === $start) {
             // The old item starts at the exact same point as the new item.
             $insertStartIndex--;
          }
@@ -101,12 +99,12 @@ class FreeBusyData {
         // looking one item before the insertStartIndex, because it's possible
         // that the new item 'sits inside' the previous old item.
         if ($insertStartIndex > 0) {
-            $currentIndex = $insertStartIndex-1;
+            $currentIndex = $insertStartIndex - 1;
         } else {
             $currentIndex = 0;
         }
 
-        while($end > $this->data[$currentIndex]['end']) {
+        while ($end > $this->data[$currentIndex]['end']) {
 
             $currentIndex++;
 
@@ -151,29 +149,29 @@ class FreeBusyData {
         $mergeItem = $newItem;
         $mergeDelete = 1;
 
-        if (isset($this->data[$insertStartIndex-1])) {
+        if (isset($this->data[$insertStartIndex - 1])) {
             // Updating the start time of the previous item.
-            $this->data[$insertStartIndex-1]['end'] = $start - 1;
+            $this->data[$insertStartIndex - 1]['end'] = $start;
 
             // If the previous and the current are of the same type, we can
             // merge them into one item.
-            if ($this->data[$insertStartIndex-1]['type'] === $this->data[$insertStartIndex]['type']) {
+            if ($this->data[$insertStartIndex - 1]['type'] === $this->data[$insertStartIndex]['type']) {
                 $doMerge = true;
                 $mergeOffset--;
                 $mergeDelete++;
-                $mergeItem['start'] = $this->data[$insertStartIndex-1]['start'];
+                $mergeItem['start'] = $this->data[$insertStartIndex - 1]['start'];
             }
         }
-        if (isset($this->data[$insertStartIndex+1])) {
+        if (isset($this->data[$insertStartIndex + 1])) {
             // Updating the start time of the next item.
-            $this->data[$insertStartIndex+1]['start'] = $end + 1;
+            $this->data[$insertStartIndex + 1]['start'] = $end;
 
             // If the next and the current are of the same type, we can
             // merge them into one item.
-            if ($this->data[$insertStartIndex+1]['type'] === $this->data[$insertStartIndex]['type']) {
+            if ($this->data[$insertStartIndex + 1]['type'] === $this->data[$insertStartIndex]['type']) {
                 $doMerge = true;
                 $mergeDelete++;
-                $mergeItem['end'] = $this->data[$insertStartIndex+1]['end'];
+                $mergeItem['end'] = $this->data[$insertStartIndex + 1]['end'];
             }
 
         }
diff --git a/lib/FreeBusyGenerator.php b/lib/FreeBusyGenerator.php
index d812804..fe2de15 100644
--- a/lib/FreeBusyGenerator.php
+++ b/lib/FreeBusyGenerator.php
@@ -8,7 +8,6 @@ use DateTimeZone;
 use Sabre\VObject\Component\VCalendar;
 use Sabre\VObject\Recur\EventIterator;
 use Sabre\VObject\Recur\NoInstancesException;
-use SplDoublyLinkedList;
 
 /**
  * This class helps with generating FREEBUSY reports based on existing sets of
@@ -92,9 +91,7 @@ class FreeBusyGenerator {
      */
     function __construct(DateTimeInterface $start = null, DateTimeInterface $end = null, $objects = null, DateTimeZone $timeZone = null) {
 
-        if ($start && $end) {
-            $this->setTimeRange($start, $end);
-        }
+        $this->setTimeRange($start, $end);
 
         if ($objects) {
             $this->setObjects($objects);
@@ -179,6 +176,12 @@ class FreeBusyGenerator {
      */
     function setTimeRange(DateTimeInterface $start = null, DateTimeInterface $end = null) {
 
+        if (!$start) {
+            $start = new DateTimeImmutable(Settings::$minDate);
+        }
+        if (!$end) {
+            $end = new DateTimeImmutable(Settings::$maxDate);
+        }
         $this->start = $start;
         $this->end = $end;
 
@@ -205,7 +208,10 @@ class FreeBusyGenerator {
      */
     function getResult() {
 
-        $busyTimes = [];
+        $busyTimes = new FreeBusyData(
+            $this->start->getTimeStamp(),
+            $this->end->getTimeStamp()
+        );
 
         foreach ($this->objects as $key => $object) {
 
@@ -293,11 +299,11 @@ class FreeBusyGenerator {
                             if ($this->end && $time[0] > $this->end) break;
                             if ($this->start && $time[1] < $this->start) break;
 
-                            $busyTimes[] = [
-                                $time[0],
-                                $time[1],
-                                $FBTYPE,
-                            ];
+                            $busyTimes->add(
+                                $time[0]->getTimeStamp(),
+                                $time[1]->getTimeStamp(),
+                                $FBTYPE
+                            );
                         }
                         break;
 
@@ -325,11 +331,11 @@ class FreeBusyGenerator {
 
                                 if ($this->start && $this->start > $endTime) continue;
                                 if ($this->end && $this->end < $startTime) continue;
-                                $busyTimes[] = [
-                                    $startTime,
-                                    $endTime,
+                                $busyTimes->add(
+                                    $startTime->getTimeStamp(),
+                                    $endTime->getTimeStamp(),
                                     $fbType
-                                ];
+                                );
 
                             }
 
@@ -363,20 +369,27 @@ class FreeBusyGenerator {
             $dtend->setDateTime($this->end);
             $vfreebusy->add($dtend);
         }
+
+        $tz = new \DateTimeZone('UTC');
         $dtstamp = $calendar->createProperty('DTSTAMP');
-        $dtstamp->setDateTime(new DateTimeImmutable('now', new \DateTimeZone('UTC')));
+        $dtstamp->setDateTime(new DateTimeImmutable('now', $tz));
         $vfreebusy->add($dtstamp);
 
-        foreach ($busyTimes as $busyTime) {
+        foreach ($busyTimes->getData() as $busyTime) {
+
+            // Ignoring all the FREE parts, because those are already assumed.
+            if ($busyTime['type'] === 'FREE') {
+                continue;
+            }
 
-            $busyTime[0] = $busyTime[0]->setTimeZone(new \DateTimeZone('UTC'));
-            $busyTime[1] = $busyTime[1]->setTimeZone(new \DateTimeZone('UTC'));
+            $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')
             );
-            $prop['FBTYPE'] = $busyTime[2];
+            $prop['FBTYPE'] = $busyTime['type'];
             $vfreebusy->add($prop);
 
         }
diff --git a/lib/Settings.php b/lib/Settings.php
new file mode 100644
index 0000000..53acd5e
--- /dev/null
+++ b/lib/Settings.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Sabre\VObject;
+
+/**
+ * This class provides a list of global defaults for vobject.
+ *
+ * Some of these started to appear in various classes, so it made a bit more
+ * sense to centralize them, so it's easier for user to find and change these.
+ *
+ * The global nature of them does mean that changing the settings for one
+ * instance has a global influence.
+ *
+ * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class Settings {
+
+    /**
+     * The minimum date we accept for various calculations with dates, such as
+     * recurrences.
+     *
+     * The choice of 1900 is pretty arbitrary, but it covers most common
+     * use-cases. In particular, it covers birthdates for virtually everyone
+     * alive on earth, which is less than 5 people at the time of writing.
+     */
+    static $minDate = '1900-01-01';
+
+    /**
+     * The maximum date we accept for various calculations with dates, such as
+     * recurrences.
+     *
+     * The choice of 2100 is pretty arbitrary, but should cover most
+     * appointments made for many years to come.
+     */
+    static $maxDate = '2100-01-01';
+
+}
diff --git a/tests/VObject/FreeBusyDataTest.php b/tests/VObject/FreeBusyDataTest.php
index 90ce89c..9b5f541 100644
--- a/tests/VObject/FreeBusyDataTest.php
+++ b/tests/VObject/FreeBusyDataTest.php
@@ -2,8 +2,6 @@
 
 namespace Sabre\VObject;
 
-use DateTimeImmutable as DT;
-
 class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
 
     function testGetData() {
@@ -14,8 +12,8 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
             [
                 [
                     'start' => 100,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ]
             ],
             $fb->getData()
@@ -31,39 +29,39 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
         $fb = new FreeBusyData(100, 200);
 
         // Overwriting the first half
-        $fb->add(100,150,'BUSY');
+        $fb->add(100, 150, 'BUSY');
 
 
         $this->assertEquals(
             [
                 [
                     'start' => 100,
-                    'end' => 150,
-                    'type' => 'BUSY',
+                    'end'   => 150,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 151,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'start' => 150,
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ]
             ],
             $fb->getData()
         );
 
         // Overwriting the first half again
-        $fb->add(100,150,'BUSY-TENTATIVE');
+        $fb->add(100, 150, 'BUSY-TENTATIVE');
 
         $this->assertEquals(
             [
                 [
                     'start' => 100,
-                    'end' => 150,
-                    'type' => 'BUSY-TENTATIVE',
+                    'end'   => 150,
+                    'type'  => 'BUSY-TENTATIVE',
                 ],
                 [
-                    'start' => 151,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'start' => 150,
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ]
             ],
             $fb->getData()
@@ -79,20 +77,20 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
         $fb = new FreeBusyData(100, 200);
 
         // Overwriting the first half
-        $fb->add(151,200,'BUSY');
+        $fb->add(150, 200, 'BUSY');
 
 
         $this->assertEquals(
             [
                 [
                     'start' => 100,
-                    'end' => 150,
-                    'type' => 'FREE',
+                    'end'   => 150,
+                    'type'  => 'FREE',
                 ],
                 [
-                    'start' => 151,
-                    'end' => 200,
-                    'type' => 'BUSY',
+                    'start' => 150,
+                    'end'   => 200,
+                    'type'  => 'BUSY',
                 ],
             ],
             $fb->getData()
@@ -109,25 +107,25 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
         $fb = new FreeBusyData(100, 200);
 
         // Overwriting the first half
-        $fb->add(150,160,'BUSY');
+        $fb->add(150, 160, 'BUSY');
 
 
         $this->assertEquals(
             [
                 [
                     'start' => 100,
-                    'end' => 149,
-                    'type' => 'FREE',
+                    'end'   => 150,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 150,
-                    'end' => 160,
-                    'type' => 'BUSY',
+                    'end'   => 160,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 161,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'start' => 160,
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ],
             ],
             $fb->getData()
@@ -149,28 +147,28 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
             [
                 [
                     'start' => 100,
-                    'end' => 109,
-                    'type' => 'FREE',
+                    'end'   => 110,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 110,
-                    'end' => 120,
-                    'type' => 'BUSY',
+                    'end'   => 120,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 121,
-                    'end' => 129,
-                    'type' => 'FREE',
+                    'start' => 120,
+                    'end'   => 130,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 130,
-                    'end' => 140,
-                    'type' => 'BUSY',
+                    'end'   => 140,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 141,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'start' => 140,
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ],
             ],
             $fb->getData()
@@ -192,28 +190,28 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
             [
                 [
                     'start' => 100,
-                    'end' => 109,
-                    'type' => 'FREE',
+                    'end'   => 110,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 110,
-                    'end' => 120,
-                    'type' => 'BUSY',
+                    'end'   => 120,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 121,
-                    'end' => 129,
-                    'type' => 'FREE',
+                    'start' => 120,
+                    'end'   => 130,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 130,
-                    'end' => 140,
-                    'type' => 'BUSY',
+                    'end'   => 140,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 141,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'start' => 140,
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ],
             ],
             $fb->getData()
@@ -225,28 +223,28 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
             [
                 [
                     'start' => 100,
-                    'end' => 109,
-                    'type' => 'FREE',
+                    'end'   => 110,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 110,
-                    'end' => 114,
-                    'type' => 'BUSY',
+                    'end'   => 115,
+                    'type'  => 'BUSY',
                 ],
                 [
                     'start' => 115,
-                    'end' => 135,
-                    'type' => 'BUSY-TENTATIVE',
+                    'end'   => 135,
+                    'type'  => 'BUSY-TENTATIVE',
                 ],
                 [
-                    'start' => 136,
-                    'end' => 140,
-                    'type' => 'BUSY',
+                    'start' => 135,
+                    'end'   => 140,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 141,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'start' => 140,
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ],
             ],
             $fb->getData()
@@ -267,28 +265,28 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
             [
                 [
                     'start' => 100,
-                    'end' => 109,
-                    'type' => 'FREE',
+                    'end'   => 110,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 110,
-                    'end' => 120,
-                    'type' => 'BUSY',
+                    'end'   => 120,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 121,
-                    'end' => 129,
-                    'type' => 'FREE',
+                    'start' => 120,
+                    'end'   => 130,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 130,
-                    'end' => 140,
-                    'type' => 'BUSY',
+                    'end'   => 140,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 141,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'start' => 140,
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ],
             ],
             $fb->getData()
@@ -300,18 +298,18 @@ class FreeBusyDataTest extends \PHPUnit_Framework_TestCase {
             [
                 [
                     'start' => 100,
-                    'end' => 109,
-                    'type' => 'FREE',
+                    'end'   => 110,
+                    'type'  => 'FREE',
                 ],
                 [
                     'start' => 110,
-                    'end' => 140,
-                    'type' => 'BUSY',
+                    'end'   => 140,
+                    'type'  => 'BUSY',
                 ],
                 [
-                    'start' => 141,
-                    'end' => 200,
-                    'type' => 'FREE',
+                    'start' => 140,
+                    'end'   => 200,
+                    'type'  => 'FREE',
                 ],
             ],
             $fb->getData()
diff --git a/tests/VObject/FreeBusyGeneratorTest.php b/tests/VObject/FreeBusyGeneratorTest.php
index a7977e8..fc457d3 100644
--- a/tests/VObject/FreeBusyGeneratorTest.php
+++ b/tests/VObject/FreeBusyGeneratorTest.php
@@ -203,9 +203,7 @@ ICS;
             Reader::read($blob),
             [
                 '20110103T010000Z/20110103T020000Z',
-                '20110103T030000Z/20110103T040000Z',
-                '20110103T040000Z/20110103T050000Z',
-                '20110103T050000Z/20110103T060000Z',
+                '20110103T030000Z/20110103T060000Z',
             ]
         ];
 
@@ -278,7 +276,7 @@ ICS;
             new \DateTimeZone('America/Toronto')
         ];
 
-        // All-day event
+        // All-day event, slightly outside of the VFREEBUSY range.
         $blob = <<<ICS
 BEGIN:VCALENDAR
 BEGIN:VEVENT
@@ -290,7 +288,7 @@ ICS;
 
         $tests[] = [
             $blob,
-            "20110101T000000Z/20110102T000000Z"
+            "20110101T110000Z/20110102T000000Z"
         ];
 
         // All-day event + reference timezone
@@ -305,7 +303,7 @@ ICS;
 
         $tests[] = [
             $blob,
-            "20110101T050000Z/20110102T050000Z",
+            "20110101T110000Z/20110102T050000Z",
             new \DateTimeZone('America/Toronto')
         ];
 
@@ -343,14 +341,18 @@ ICS;
         );
 
         $result = $gen->getResult();
+        //print_r($result->serialize());die();
 
         $expected = (array)$expected;
 
+        if ($input instanceof Document) $input = $input->serialize();
+        $debugInfo = "Input:\n$input\n\nExpected:\n" . print_r($expected, true) . "\nActual result:\n" . $result->serialize() . "\n";
+
         $freebusy = $result->VFREEBUSY->select('FREEBUSY');
 
         foreach ($freebusy as $fb) {
 
-            $this->assertContains((string)$fb, $expected, "$fb did not appear in our list of expected freebusy strings. This is concerning!");
+            $this->assertContains((string)$fb, $expected, "$fb did not appear in our list of expected freebusy strings. This is concerning! Debug info:\n" . $debugInfo);
 
             $k = array_search((string)$fb, $expected);
             unset($expected[$k]);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabre-vobject.git



More information about the Pkg-owncloud-commits mailing list