[Pkg-owncloud-commits] [php-sabre-vobject] 01/43: Added getBirthdayEvent method for VCards

David Prévot taffit at moszumanska.debian.org
Sat Sep 5 15:23:46 UTC 2015


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

taffit pushed a commit to tag 4.0.0-alpha2
in repository php-sabre-vobject.

commit 8619f79b85f7e17ef03b69b7fbd8593877761cea
Author: Dominik Tobschall <dominik at fruux.com>
Date:   Tue Jul 7 16:11:01 2015 +0200

    Added getBirthdayEvent method for VCards
---
 CHANGELOG.md                        |   1 +
 lib/Component/VCard.php             |  40 ++++++++++++
 tests/VObject/VCardBirthdayTest.php | 120 ++++++++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8c79a28..2a1d115 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,7 @@ ChangeLog
 * #197: The `$children` property on components has been changed from `public`
   to `protected`. Use the `children()` method instead to get a flat list of
   objects.
+* #239: Added a `getBirthdayEvent()` method in Component\VCard. (@DominikTo)
 
 
 3.4.5 (2015-06-02)
diff --git a/lib/Component/VCard.php b/lib/Component/VCard.php
index 87baaa3..2842be0 100644
--- a/lib/Component/VCard.php
+++ b/lib/Component/VCard.php
@@ -415,6 +415,46 @@ class VCard extends VObject\Document {
     }
 
     /**
+     * This method returns a birthday event for the card.
+     * If the BDAY property is not present, it returns null.
+     * If the BDAY property didn't contain a valid date, it returns false.
+     *
+     * With $format, it's possible to localize or modify the SUMMARY of
+     * the generated event, e.g. "$format = Geburtstag von %s"
+     *
+     * @param string $format
+     * @return VCalendar|null|false
+     */
+    function getBirthdayEvent($format = null) {
+
+        if ($format === null) {
+            $format = '%s\'s Birthday';
+        }
+
+        if (!$this->select('BDAY')) {
+            return null;
+        }
+
+        $vCal = new VCalendar();
+
+        try {
+            $vCal->add('VEVENT', [
+                'SUMMARY' => sprintf($format, $this->FN->getValue()),
+                'DTSTART' => new \DateTime($this->BDAY->getValue()),
+                'RRULE'   => 'FREQ=YEARLY',
+                'TRANSP'  => 'TRANSPARENT',
+            ]);
+
+        // If the BDAY property didn't contain a valid date, false is returned
+        } catch (\Exception $e) {
+            return false;
+        }
+
+        return $vCal;
+
+    }
+
+    /**
      * This method returns an array, with the representation as it should be
      * encoded in json. This is used to create jCard or jCal documents.
      *
diff --git a/tests/VObject/VCardBirthdayTest.php b/tests/VObject/VCardBirthdayTest.php
new file mode 100644
index 0000000..021a94a
--- /dev/null
+++ b/tests/VObject/VCardBirthdayTest.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace Sabre\VObject;
+
+class VCardBirthdayTest extends TestCase {
+
+    function testValidBirthday() {
+
+        $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:19850407
+END:VCARD
+VCF;
+
+        $version = Version::VERSION;
+        $event = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//Sabre//Sabre VObject $version//EN
+CALSCALE:GREGORIAN
+BEGIN:VEVENT
+SUMMARY:Forrest Gump's Birthday
+DTSTART:19850407T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $vcard = Reader::read($input);
+        $expected = Reader::read($event)->serialize();
+        $output = $vcard->getBirthdayEvent()->serialize();
+
+        $this->assertEquals(
+            $expected,
+            $output
+        );
+
+    }
+
+    function testLocalizedValidBirthday() {
+
+        $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:19850407
+END:VCARD
+VCF;
+
+        $version = Version::VERSION;
+        $event = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//Sabre//Sabre VObject $version//EN
+CALSCALE:GREGORIAN
+BEGIN:VEVENT
+SUMMARY:Forrest Gump's Geburtstag
+DTSTART:19850407T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $vcard = Reader::read($input);
+        $expected = Reader::read($event)->serialize();
+        $output = $vcard->getBirthdayEvent('%s\'s Geburtstag')->serialize();
+
+        $this->assertEquals(
+            $expected,
+            $output
+        );
+
+    }
+
+    function testInvalidBirthday() {
+
+        $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:foo
+END:VCARD
+VCF;
+
+        $vcard = Reader::read($input);
+
+        $this->assertEquals(
+            false,
+            $vcard->getBirthdayEvent()
+        );
+
+    }
+
+    function testNoBirthday() {
+
+        $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+END:VCARD
+VCF;
+
+        $vcard = Reader::read($input);
+
+        $this->assertEquals(
+            false,
+            $vcard->getBirthdayEvent()
+        );
+
+    }
+
+}

-- 
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