[Pkg-owncloud-commits] [php-sabre-vobject] 02/43: Added BirthdayCalendarGenerator
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 caadb361a14582befe4e453d7b9af6b20dcd9599
Author: Dominik Tobschall <dominik at fruux.com>
Date: Wed Jul 8 09:51:29 2015 +0200
Added BirthdayCalendarGenerator
---
CHANGELOG.md | 2 +-
lib/BirthdayCalendarGenerator.php | 185 ++++++++++++++
lib/Component/VCard.php | 40 ---
tests/VObject/BirthdayCalendarGeneratorTest.php | 320 ++++++++++++++++++++++++
tests/VObject/VCardBirthdayTest.php | 120 ---------
5 files changed, 506 insertions(+), 161 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2a1d115..f8571f4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,7 +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)
+* #239: Added a `BirthdayCalendarGenerator`. (@DominikTo)
3.4.5 (2015-06-02)
diff --git a/lib/BirthdayCalendarGenerator.php b/lib/BirthdayCalendarGenerator.php
new file mode 100644
index 0000000..02b1a1f
--- /dev/null
+++ b/lib/BirthdayCalendarGenerator.php
@@ -0,0 +1,185 @@
+<?php
+
+namespace Sabre\VObject;
+
+use DateTimeInterface;
+use DateTimezone;
+use Sabre\VObject\Component\VCalendar;
+
+/**
+ * This class generates birthday calendars.
+ *
+ * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
+ * @author Dominik Tobschall (http://tobschall.de/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class BirthdayCalendarGenerator {
+
+ /**
+ * Input objects.
+ *
+ * @var array
+ */
+ protected $objects = [];
+
+ /**
+ * Reference timezone.
+ *
+ * When generating events, and we come across so-called
+ * floating times (times without a timezone), we use the reference timezone
+ * instead.
+ *
+ * This is also used for all-day events.
+ *
+ * This defaults to UTC.
+ *
+ * @var DateTimeZone
+ */
+ protected $timeZone;
+
+ /**
+ * Default year.
+ * Used for dates without a year.
+ *
+ * @var string
+ */
+ protected $defaultYear = 1900;
+
+ /**
+ * Creates the generator.
+ *
+ * Check the setTimeRange and setObjects methods for details about the
+ * arguments.
+ *
+ * @param mixed $objects
+ * @param DateTimeZone $timeZone
+ */
+ function __construct($objects = null, DateTimeZone $timeZone = null) {
+
+ if ($objects) {
+ $this->setObjects($objects);
+ }
+ if (is_null($timeZone)) {
+ $timeZone = new DateTimeZone('UTC');
+ }
+ $this->setTimeZone($timeZone);
+
+ }
+
+ /**
+ * Sets the input objects.
+ *
+ * You must either supply a vCard as a string or as a Component/VCard object.
+ * It's also possible to supply an array of strings or objects.
+ *
+ * @param mixed $objects
+ *
+ * @return void
+ */
+ function setObjects($objects) {
+
+ if (!is_array($objects)) {
+ $objects = [$objects];
+ }
+
+ $this->objects = [];
+ foreach ($objects as $object) {
+
+ if (is_string($object)) {
+
+ $vObj = Reader::read($object);
+ if (!$vObj instanceof Component\VCard) {
+ throw new \InvalidArgumentException('String could not be parsed as \\Sabre\\VObject\\Component\\VCard by setObjects');
+ }
+
+ $this->objects[] = $vObj;
+
+ } elseif ($object instanceof Component\VCard) {
+
+ $this->objects[] = $object;
+
+ } else {
+
+ throw new \InvalidArgumentException('You can only pass strings or \\Sabre\\VObject\\Component\\VCard arguments to setObjects');
+
+ }
+
+ }
+
+ }
+
+ /**
+ * Sets the reference timezone for floating times.
+ *
+ * @param DateTimeZone $timeZone
+ *
+ * @return void
+ */
+ function setTimeZone(DateTimeZone $timeZone) {
+
+ $this->timeZone = $timeZone;
+
+ }
+
+ /**
+ * Parses the input data and returns a VCALENDAR.
+ *
+ * @return Component/VCalendar
+ */
+ function getResult() {
+
+ $calendar = new VCalendar();
+
+ foreach ($this->objects as $object) {
+
+ // Skip if there is no BDAY property.
+ if (!$object->select('BDAY')) {
+ continue;
+ }
+
+ // Skip if the BDAY property is not of the right type.
+ if (!$object->BDAY instanceof Property\VCard\DateAndOrTime) {
+ continue;
+ }
+
+ // Skip if we can't parse the BDAY value.
+ try {
+ $dateParts = DateTimeParser::parseVCardDateTime($object->BDAY->getValue());
+ } catch (\InvalidArgumentException $e) {
+ continue;
+ }
+
+ // Set a year if it's not set.
+ if (!$dateParts['year']) {
+ $object->BDAY=$this->defaultYear . '-' . $dateParts['month'] . '-' . $dateParts['date'];
+ }
+
+ // Set a year, if X-APPLE-OMIT-YEAR is set.
+ if (isset($object->BDAY['X-APPLE-OMIT-YEAR'])) {
+ if ($dateParts['year'] === $object->BDAY['X-APPLE-OMIT-YEAR']->getValue()) {
+ // @TODO:
+ // It probably makes sense to add an info to our event that the year is unknown.
+ // Maybe we should just keep this and also set the X-APPLE-OMIT-YEAR PARAM on our PROPERTY
+ $object->BDAY=$this->defaultYear . '-' . $dateParts['month'] . '-' . $dateParts['date'];
+ }
+
+ }
+
+ // Generate the event.
+ $calendar->add('VEVENT', [
+ 'SUMMARY' => $object->FN->getValue() .'\'s Birthday',
+ 'DTSTART' => new \DateTime($object->BDAY->getValue(), $this->timeZone),
+ 'RRULE' => 'FREQ=YEARLY',
+ 'TRANSP' => 'TRANSPARENT',
+ 'X-SABRE-BDAY' => [$object->UID->getValue(), 'BDAY', $object->FN->getValue()]
+ ]);
+
+ }
+
+ return $calendar;
+
+ }
+
+
+
+}
diff --git a/lib/Component/VCard.php b/lib/Component/VCard.php
index 2842be0..87baaa3 100644
--- a/lib/Component/VCard.php
+++ b/lib/Component/VCard.php
@@ -415,46 +415,6 @@ 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/BirthdayCalendarGeneratorTest.php b/tests/VObject/BirthdayCalendarGeneratorTest.php
new file mode 100644
index 0000000..13e6420
--- /dev/null
+++ b/tests/VObject/BirthdayCalendarGeneratorTest.php
@@ -0,0 +1,320 @@
+<?php
+
+namespace Sabre\VObject;
+
+class BirthdayCalendarGeneratorTest extends TestCase {
+
+ function setUp() {
+ $this->generator = new BirthdayCalendarGenerator();
+ }
+
+ function testVcardStringWithValidBirthday() {
+
+ $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:19850407
+UID:foo
+END:VCARD
+VCF;
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+SUMMARY:Forrest Gump's Birthday
+DTSTART:19850407T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+X-SABRE-BDAY:foo,BDAY,Forrest Gump
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+ $this->generator->setObjects($input);
+ $output = $this->generator->getResult();
+
+ $this->assertVObjEquals(
+ $expected,
+ $output
+ );
+
+ }
+
+ function testArrayOfVcardStringsWithValidBirthdays() {
+
+ $input = [];
+
+ $input[] = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:19850407
+UID:foo
+END:VCARD
+VCF;
+
+ $input[] = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Doe;John;;Mr.
+FN:John Doe
+BDAY:19820210
+UID:bar
+END:VCARD
+VCF;
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+SUMMARY:Forrest Gump's Birthday
+DTSTART:19850407T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+X-SABRE-BDAY:foo,BDAY,Forrest Gump
+END:VEVENT
+BEGIN:VEVENT
+SUMMARY:John Doe's Birthday
+DTSTART:19820210T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+X-SABRE-BDAY:bar,BDAY,John Doe
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+ $this->generator->setObjects($input);
+ $output = $this->generator->getResult();
+
+ $this->assertVObjEquals(
+ $expected,
+ $output
+ );
+
+ }
+
+ function testVcardObjectWithValidBirthday() {
+
+ $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:19850407
+UID:foo
+END:VCARD
+VCF;
+
+ $input = Reader::read($input);
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+SUMMARY:Forrest Gump's Birthday
+DTSTART:19850407T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+X-SABRE-BDAY:foo,BDAY,Forrest Gump
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+ $this->generator->setObjects($input);
+ $output = $this->generator->getResult();
+
+ $this->assertVObjEquals(
+ $expected,
+ $output
+ );
+
+ }
+
+ function testArrayOfVcardObjectsWithValidBirthdays() {
+
+ $input = [];
+
+ $input[] = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:19850407
+UID:foo
+END:VCARD
+VCF;
+
+ $input[] = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Doe;John;;Mr.
+FN:John Doe
+BDAY:19820210
+UID:bar
+END:VCARD
+VCF;
+
+ foreach ($input as $key => $value) {
+ $input[$key] = Reader::read($value);
+ }
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+SUMMARY:Forrest Gump's Birthday
+DTSTART:19850407T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+X-SABRE-BDAY:foo,BDAY,Forrest Gump
+END:VEVENT
+BEGIN:VEVENT
+SUMMARY:John Doe's Birthday
+DTSTART:19820210T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+X-SABRE-BDAY:bar,BDAY,John Doe
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+ $this->generator->setObjects($input);
+ $output = $this->generator->getResult();
+
+ $this->assertVObjEquals(
+ $expected,
+ $output
+ );
+
+ }
+
+ function testVcardStringWithValidBirthdayWithXAppleOmitYear() {
+
+ $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY;X-APPLE-OMIT-YEAR=1604:1604-04-07
+UID:foo
+END:VCARD
+VCF;
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+SUMMARY:Forrest Gump's Birthday
+DTSTART:19000407T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+X-SABRE-BDAY:foo,BDAY,Forrest Gump
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+ $this->generator->setObjects($input);
+ $output = $this->generator->getResult();
+
+ $this->assertVObjEquals(
+ $expected,
+ $output
+ );
+
+ }
+
+ function testVcardStringWithValidBirthdayWithoutYear() {
+
+ $input = <<<VCF
+BEGIN:VCARD
+VERSION:4.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:--04-07
+UID:foo
+END:VCARD
+VCF;
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+SUMMARY:Forrest Gump's Birthday
+DTSTART:19000407T000000Z
+RRULE:FREQ=YEARLY
+TRANSP:TRANSPARENT
+X-SABRE-BDAY:foo,BDAY,Forrest Gump
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+ $this->generator->setObjects($input);
+ $output = $this->generator->getResult();
+
+ $this->assertVObjEquals(
+ $expected,
+ $output
+ );
+
+ }
+
+ function testVcardStringWithInvalidBirthday() {
+
+ $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+BDAY:foo
+UID:foo
+END:VCARD
+VCF;
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+END:VCALENDAR
+ICS;
+
+ $this->generator->setObjects($input);
+ $output = $this->generator->getResult();
+
+ $this->assertVObjEquals(
+ $expected,
+ $output
+ );
+
+ }
+
+ function testVcardStringWithNoBirthday() {
+
+ $input = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+N:Gump;Forrest;;Mr.
+FN:Forrest Gump
+UID:foo
+END:VCARD
+VCF;
+
+ $expected = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+END:VCALENDAR
+ICS;
+
+ $this->generator->setObjects($input);
+ $output = $this->generator->getResult();
+
+ $this->assertVObjEquals(
+ $expected,
+ $output
+ );
+
+ }
+
+}
diff --git a/tests/VObject/VCardBirthdayTest.php b/tests/VObject/VCardBirthdayTest.php
deleted file mode 100644
index 021a94a..0000000
--- a/tests/VObject/VCardBirthdayTest.php
+++ /dev/null
@@ -1,120 +0,0 @@
-<?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