Component
6 months ago
ITip
6 months ago
Parser
6 months ago
Property
6 months ago
Recur
6 months ago
Splitter
6 months ago
TimezoneGuesser
6 months ago
timezonedata
1 year ago
BirthdayCalendarGenerator.php
6 months ago
Cli.php
6 months ago
Component.php
6 months ago
DateTimeParser.php
6 months ago
Document.php
6 months ago
ElementList.php
6 months ago
EofException.php
6 months ago
FreeBusyData.php
6 months ago
FreeBusyGenerator.php
6 months ago
InvalidDataException.php
6 months ago
Node.php
6 months ago
PHPUnitAssertions.php
6 months ago
Parameter.php
6 months ago
ParseException.php
6 months ago
Property.php
6 months ago
Reader.php
6 months ago
Settings.php
6 months ago
StringUtil.php
6 months ago
TimeZoneUtil.php
6 months ago
UUIDUtil.php
6 months ago
VCardConverter.php
6 months ago
Version.php
6 months ago
Writer.php
6 months ago
BirthdayCalendarGenerator.php
138 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaVendor\Sabre\VObject; |
| 4 | |
| 5 | use AmeliaVendor\Sabre\VObject\Component\VCalendar; |
| 6 | /** |
| 7 | * This class generates birthday calendars. |
| 8 | * |
| 9 | * @copyright Copyright (C) fruux GmbH (https://fruux.com/) |
| 10 | * @author Dominik Tobschall (http://tobschall.de/) |
| 11 | * @license http://sabre.io/license/ Modified BSD License |
| 12 | */ |
| 13 | class BirthdayCalendarGenerator |
| 14 | { |
| 15 | /** |
| 16 | * Input objects. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | protected $objects = []; |
| 21 | /** |
| 22 | * Default year. |
| 23 | * Used for dates without a year. |
| 24 | */ |
| 25 | const DEFAULT_YEAR = 2000; |
| 26 | /** |
| 27 | * Output format for the SUMMARY. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $format = '%1$s\'s Birthday'; |
| 32 | /** |
| 33 | * Creates the generator. |
| 34 | * |
| 35 | * Check the setTimeRange and setObjects methods for details about the |
| 36 | * arguments. |
| 37 | * |
| 38 | * @param mixed $objects |
| 39 | */ |
| 40 | public function __construct($objects = null) |
| 41 | { |
| 42 | if ($objects) { |
| 43 | $this->setObjects($objects); |
| 44 | } |
| 45 | } |
| 46 | /** |
| 47 | * Sets the input objects. |
| 48 | * |
| 49 | * You must either supply a vCard as a string or as a Component/VCard object. |
| 50 | * It's also possible to supply an array of strings or objects. |
| 51 | * |
| 52 | * @param mixed $objects |
| 53 | */ |
| 54 | public function setObjects($objects) |
| 55 | { |
| 56 | if (!is_array($objects)) { |
| 57 | $objects = [$objects]; |
| 58 | } |
| 59 | $this->objects = []; |
| 60 | foreach ($objects as $object) { |
| 61 | if (is_string($object)) { |
| 62 | $vObj = Reader::read($object); |
| 63 | if (!$vObj instanceof \AmeliaVendor\Sabre\VObject\Component\VCard) { |
| 64 | throw new \InvalidArgumentException('String could not be parsed as \Sabre\VObject\Component\VCard by setObjects'); |
| 65 | } |
| 66 | $this->objects[] = $vObj; |
| 67 | } elseif ($object instanceof \AmeliaVendor\Sabre\VObject\Component\VCard) { |
| 68 | $this->objects[] = $object; |
| 69 | } else { |
| 70 | throw new \InvalidArgumentException('You can only pass strings or \Sabre\VObject\Component\VCard arguments to setObjects'); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | /** |
| 75 | * Sets the output format for the SUMMARY. |
| 76 | * |
| 77 | * @param string $format |
| 78 | */ |
| 79 | public function setFormat($format) |
| 80 | { |
| 81 | $this->format = $format; |
| 82 | } |
| 83 | /** |
| 84 | * Parses the input data and returns a VCALENDAR. |
| 85 | * |
| 86 | * @return Component/VCalendar |
| 87 | */ |
| 88 | public function getResult() |
| 89 | { |
| 90 | $calendar = new VCalendar(); |
| 91 | foreach ($this->objects as $object) { |
| 92 | // Skip if there is no BDAY property. |
| 93 | if (!$object->select('BDAY')) { |
| 94 | continue; |
| 95 | } |
| 96 | // We've seen clients (ez-vcard) putting "BDAY:" properties |
| 97 | // without a value into vCards. If we come across those, we'll |
| 98 | // skip them. |
| 99 | if (empty($object->BDAY->getValue())) { |
| 100 | continue; |
| 101 | } |
| 102 | // We're always converting to vCard 4.0 so we can rely on the |
| 103 | // VCardConverter handling the X-APPLE-OMIT-YEAR property for us. |
| 104 | $object = $object->convert(Document::VCARD40); |
| 105 | // Skip if the card has no FN property. |
| 106 | if (!isset($object->FN)) { |
| 107 | continue; |
| 108 | } |
| 109 | // Skip if the BDAY property is not of the right type. |
| 110 | if (!$object->BDAY instanceof \AmeliaVendor\Sabre\VObject\Property\VCard\DateAndOrTime) { |
| 111 | continue; |
| 112 | } |
| 113 | // Skip if we can't parse the BDAY value. |
| 114 | try { |
| 115 | $dateParts = DateTimeParser::parseVCardDateTime($object->BDAY->getValue()); |
| 116 | } catch (InvalidDataException $e) { |
| 117 | continue; |
| 118 | } |
| 119 | // Set a year if it's not set. |
| 120 | $unknownYear = false; |
| 121 | if (!$dateParts['year']) { |
| 122 | $object->BDAY = self::DEFAULT_YEAR . '-' . $dateParts['month'] . '-' . $dateParts['date']; |
| 123 | $unknownYear = true; |
| 124 | } |
| 125 | // Create event. |
| 126 | $event = $calendar->add('VEVENT', ['SUMMARY' => sprintf($this->format, $object->FN->getValue()), 'DTSTART' => new \DateTime($object->BDAY->getValue()), 'RRULE' => 'FREQ=YEARLY', 'TRANSP' => 'TRANSPARENT']); |
| 127 | // add VALUE=date |
| 128 | $event->DTSTART['VALUE'] = 'DATE'; |
| 129 | // Add X-SABRE-BDAY property. |
| 130 | if ($unknownYear) { |
| 131 | $event->add('X-SABRE-BDAY', 'BDAY', ['X-SABRE-VCARD-UID' => $object->UID->getValue(), 'X-SABRE-VCARD-FN' => $object->FN->getValue(), 'X-SABRE-OMIT-YEAR' => self::DEFAULT_YEAR]); |
| 132 | } else { |
| 133 | $event->add('X-SABRE-BDAY', 'BDAY', ['X-SABRE-VCARD-UID' => $object->UID->getValue(), 'X-SABRE-VCARD-FN' => $object->FN->getValue()]); |
| 134 | } |
| 135 | } |
| 136 | return $calendar; |
| 137 | } |
| 138 | } |