Skip to content

Commit

Permalink
Merge pull request #103 from ProtonMail/fix/customized-tz-gusser
Browse files Browse the repository at this point in the history
Fix customized timezone guesser
  • Loading branch information
liurxliu authored May 9, 2024
2 parents a9edab8 + 8595cf9 commit 40082af
Show file tree
Hide file tree
Showing 30 changed files with 431 additions and 393 deletions.
6 changes: 3 additions & 3 deletions bin/bench_freebusygenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
$bench->parse->stop();

$repeat = 100;
$start = new \DateTime('2000-01-01');
$end = new \DateTime('2020-01-01');
$timeZone = new \DateTimeZone('America/Toronto');
$start = new DateTime('2000-01-01');
$end = new DateTime('2020-01-01');
$timeZone = new DateTimeZone('America/Toronto');

$bench->fb->start();

Expand Down
3 changes: 1 addition & 2 deletions lib/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Sabre\VObject;

use Sabre\VObject;
use Sabre\Xml;

/**
Expand All @@ -15,7 +14,7 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*
* @property VObject\Property\FlatText UID
* @property Property\FlatText UID
*/
class Component extends Node
{
Expand Down
150 changes: 74 additions & 76 deletions lib/Component/VCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*
* @property VEvent VEVENT
* @property VJournal VJOURNAL
* @property VObject\Property\Text ORG
* @property VObject\Property\FlatText METHOD
* @property Property\Text ORG
* @property Property\FlatText METHOD
*/
class VCalendar extends VObject\Document
{
Expand Down Expand Up @@ -51,100 +51,100 @@ class VCalendar extends VObject\Document
* List of value-types, and which classes they map to.
*/
public static array $valueMap = [
'BINARY' => VObject\Property\Binary::class,
'BOOLEAN' => VObject\Property\Boolean::class,
'CAL-ADDRESS' => VObject\Property\ICalendar\CalAddress::class,
'DATE' => VObject\Property\ICalendar\Date::class,
'DATE-TIME' => VObject\Property\ICalendar\DateTime::class,
'DURATION' => VObject\Property\ICalendar\Duration::class,
'FLOAT' => VObject\Property\FloatValue::class,
'INTEGER' => VObject\Property\IntegerValue::class,
'PERIOD' => VObject\Property\ICalendar\Period::class,
'RECUR' => VObject\Property\ICalendar\Recur::class,
'TEXT' => VObject\Property\Text::class,
'TIME' => VObject\Property\Time::class,
'UNKNOWN' => VObject\Property\Unknown::class, // jCard / jCal-only.
'URI' => VObject\Property\Uri::class,
'UTC-OFFSET' => VObject\Property\UtcOffset::class,
'BINARY' => Property\Binary::class,
'BOOLEAN' => Property\Boolean::class,
'CAL-ADDRESS' => Property\ICalendar\CalAddress::class,
'DATE' => Property\ICalendar\Date::class,
'DATE-TIME' => Property\ICalendar\DateTime::class,
'DURATION' => Property\ICalendar\Duration::class,
'FLOAT' => Property\FloatValue::class,
'INTEGER' => Property\IntegerValue::class,
'PERIOD' => Property\ICalendar\Period::class,
'RECUR' => Property\ICalendar\Recur::class,
'TEXT' => Property\Text::class,
'TIME' => Property\Time::class,
'UNKNOWN' => Property\Unknown::class, // jCard / jCal-only.
'URI' => Property\Uri::class,
'UTC-OFFSET' => Property\UtcOffset::class,
];

/**
* List of properties, and which classes they map to.
*/
public static array $propertyMap = [
// Calendar properties
'CALSCALE' => VObject\Property\FlatText::class,
'METHOD' => VObject\Property\FlatText::class,
'PRODID' => VObject\Property\FlatText::class,
'VERSION' => VObject\Property\FlatText::class,
'CALSCALE' => Property\FlatText::class,
'METHOD' => Property\FlatText::class,
'PRODID' => Property\FlatText::class,
'VERSION' => Property\FlatText::class,

// Component properties
'ATTACH' => VObject\Property\Uri::class,
'CATEGORIES' => VObject\Property\Text::class,
'CLASS' => VObject\Property\FlatText::class,
'COMMENT' => VObject\Property\FlatText::class,
'DESCRIPTION' => VObject\Property\FlatText::class,
'GEO' => VObject\Property\FloatValue::class,
'LOCATION' => VObject\Property\FlatText::class,
'PERCENT-COMPLETE' => VObject\Property\IntegerValue::class,
'PRIORITY' => VObject\Property\IntegerValue::class,
'RESOURCES' => VObject\Property\Text::class,
'STATUS' => VObject\Property\FlatText::class,
'SUMMARY' => VObject\Property\FlatText::class,
'ATTACH' => Property\Uri::class,
'CATEGORIES' => Property\Text::class,
'CLASS' => Property\FlatText::class,
'COMMENT' => Property\FlatText::class,
'DESCRIPTION' => Property\FlatText::class,
'GEO' => Property\FloatValue::class,
'LOCATION' => Property\FlatText::class,
'PERCENT-COMPLETE' => Property\IntegerValue::class,
'PRIORITY' => Property\IntegerValue::class,
'RESOURCES' => Property\Text::class,
'STATUS' => Property\FlatText::class,
'SUMMARY' => Property\FlatText::class,

// Date and Time Component Properties
'COMPLETED' => VObject\Property\ICalendar\DateTime::class,
'DTEND' => VObject\Property\ICalendar\DateTime::class,
'DUE' => VObject\Property\ICalendar\DateTime::class,
'DTSTART' => VObject\Property\ICalendar\DateTime::class,
'DURATION' => VObject\Property\ICalendar\Duration::class,
'FREEBUSY' => VObject\Property\ICalendar\Period::class,
'TRANSP' => VObject\Property\FlatText::class,
'COMPLETED' => Property\ICalendar\DateTime::class,
'DTEND' => Property\ICalendar\DateTime::class,
'DUE' => Property\ICalendar\DateTime::class,
'DTSTART' => Property\ICalendar\DateTime::class,
'DURATION' => Property\ICalendar\Duration::class,
'FREEBUSY' => Property\ICalendar\Period::class,
'TRANSP' => Property\FlatText::class,

// Time Zone Component Properties
'TZID' => VObject\Property\FlatText::class,
'TZNAME' => VObject\Property\FlatText::class,
'TZOFFSETFROM' => VObject\Property\UtcOffset::class,
'TZOFFSETTO' => VObject\Property\UtcOffset::class,
'TZURL' => VObject\Property\Uri::class,
'TZID' => Property\FlatText::class,
'TZNAME' => Property\FlatText::class,
'TZOFFSETFROM' => Property\UtcOffset::class,
'TZOFFSETTO' => Property\UtcOffset::class,
'TZURL' => Property\Uri::class,

// Relationship Component Properties
'ATTENDEE' => VObject\Property\ICalendar\CalAddress::class,
'CONTACT' => VObject\Property\FlatText::class,
'ORGANIZER' => VObject\Property\ICalendar\CalAddress::class,
'RECURRENCE-ID' => VObject\Property\ICalendar\DateTime::class,
'RELATED-TO' => VObject\Property\FlatText::class,
'URL' => VObject\Property\Uri::class,
'UID' => VObject\Property\FlatText::class,
'ATTENDEE' => Property\ICalendar\CalAddress::class,
'CONTACT' => Property\FlatText::class,
'ORGANIZER' => Property\ICalendar\CalAddress::class,
'RECURRENCE-ID' => Property\ICalendar\DateTime::class,
'RELATED-TO' => Property\FlatText::class,
'URL' => Property\Uri::class,
'UID' => Property\FlatText::class,

// Recurrence Component Properties
'EXDATE' => VObject\Property\ICalendar\DateTime::class,
'RDATE' => VObject\Property\ICalendar\DateTime::class,
'RRULE' => VObject\Property\ICalendar\Recur::class,
'EXRULE' => VObject\Property\ICalendar\Recur::class, // Deprecated since rfc5545
'EXDATE' => Property\ICalendar\DateTime::class,
'RDATE' => Property\ICalendar\DateTime::class,
'RRULE' => Property\ICalendar\Recur::class,
'EXRULE' => Property\ICalendar\Recur::class, // Deprecated since rfc5545

// Alarm Component Properties
'ACTION' => VObject\Property\FlatText::class,
'REPEAT' => VObject\Property\IntegerValue::class,
'TRIGGER' => VObject\Property\ICalendar\Duration::class,
'ACTION' => Property\FlatText::class,
'REPEAT' => Property\IntegerValue::class,
'TRIGGER' => Property\ICalendar\Duration::class,

// Change Management Component Properties
'CREATED' => VObject\Property\ICalendar\DateTime::class,
'DTSTAMP' => VObject\Property\ICalendar\DateTime::class,
'LAST-MODIFIED' => VObject\Property\ICalendar\DateTime::class,
'SEQUENCE' => VObject\Property\IntegerValue::class,
'CREATED' => Property\ICalendar\DateTime::class,
'DTSTAMP' => Property\ICalendar\DateTime::class,
'LAST-MODIFIED' => Property\ICalendar\DateTime::class,
'SEQUENCE' => Property\IntegerValue::class,

// Request Status
'REQUEST-STATUS' => VObject\Property\Text::class,
'REQUEST-STATUS' => Property\Text::class,

// Additions from draft-daboo-valarm-extensions-04
'ALARM-AGENT' => VObject\Property\Text::class,
'ACKNOWLEDGED' => VObject\Property\ICalendar\DateTime::class,
'PROXIMITY' => VObject\Property\Text::class,
'DEFAULT-ALARM' => VObject\Property\Boolean::class,
'ALARM-AGENT' => Property\Text::class,
'ACKNOWLEDGED' => Property\ICalendar\DateTime::class,
'PROXIMITY' => Property\Text::class,
'DEFAULT-ALARM' => Property\Boolean::class,

// Additions from draft-daboo-calendar-availability-05
'BUSYTYPE' => VObject\Property\Text::class,
'BUSYTYPE' => Property\Text::class,
];

/**
Expand All @@ -166,10 +166,10 @@ public function getDocumentType(): int
*
* @return VObject\Component[]
*/
public function getBaseComponents(string $componentName = null): array
public function getBaseComponents(?string $componentName = null): array
{
$isBaseComponent = function ($component): bool {
if (!$component instanceof VObject\Component) {
if (!$component instanceof Component) {
return false;
}
if ('VTIMEZONE' === $component->name) {
Expand Down Expand Up @@ -214,13 +214,11 @@ public function getBaseComponents(string $componentName = null): array
* If there is no such component, null will be returned.
*
* @param string|null $componentName filter by component name
*
* @return VObject\Component|null
*/
public function getBaseComponent(string $componentName = null): ?Component
public function getBaseComponent(?string $componentName = null): ?Component
{
$isBaseComponent = function ($component): bool {
if (!$component instanceof VObject\Component) {
if (!$component instanceof Component) {
return false;
}
if ('VTIMEZONE' === $component->name) {
Expand Down Expand Up @@ -275,7 +273,7 @@ public function getBaseComponent(string $componentName = null): ?Component
* @throws InvalidDataException
* @throws VObject\Recur\MaxInstancesExceededException
*/
public function expand(\DateTimeInterface $start, \DateTimeInterface $end, \DateTimeZone $timeZone = null): VCalendar
public function expand(\DateTimeInterface $start, \DateTimeInterface $end, ?\DateTimeZone $timeZone = null): VCalendar
{
$newChildren = [];
$recurringEvents = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/Component/VCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class VCard extends VObject\Document
'ROLE' => VObject\Property\FlatText::class,
'LOGO' => VObject\Property\Binary::class,
// 'AGENT' => 'Sabre\\VObject\\Property\\', // Todo: is an embedded vCard. Probably rare, so
// not supported at the moment
// not supported at the moment
'ORG' => VObject\Property\Text::class,
'NOTE' => VObject\Property\FlatText::class,
'REV' => VObject\Property\VCard\TimeStamp::class,
Expand Down
16 changes: 8 additions & 8 deletions lib/DateTimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DateTimeParser
*
* @throws InvalidDataException
*/
public static function parseDateTime(string $dt, \DateTimeZone $tz = null): \DateTimeImmutable
public static function parseDateTime(string $dt, ?\DateTimeZone $tz = null): \DateTimeImmutable
{
// Format is YYYYMMDD + "T" + hhmmss
$result = preg_match('/^([0-9]{4})([0-1][0-9])([0-3][0-9])T([0-2][0-9])([0-5][0-9])([0-5][0-9])([Z]?)$/', $dt, $matches);
Expand All @@ -54,7 +54,7 @@ public static function parseDateTime(string $dt, \DateTimeZone $tz = null): \Dat
*
* @throws InvalidDataException
*/
public static function parseDate(string $date, \DateTimeZone $tz = null): \DateTimeImmutable
public static function parseDate(string $date, ?\DateTimeZone $tz = null): \DateTimeImmutable
{
// Format is YYYYMMDD
$result = preg_match('/^([0-9]{4})([0-1][0-9])([0-3][0-9])$/', $date, $matches);
Expand Down Expand Up @@ -98,12 +98,12 @@ public static function parseDuration(string $duration): \DateInterval
}

$parts = [
'week',
'day',
'hour',
'minute',
'second',
];
'week',
'day',
'hour',
'minute',
'second',
];

foreach ($parts as $part) {
$matches[$part] = isset($matches[$part]) && $matches[$part] ? (int) $matches[$part] : 0;
Expand Down
8 changes: 3 additions & 5 deletions lib/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Sabre\VObject;

use Sabre\VObject;

/**
* Document.
*
Expand All @@ -18,7 +16,7 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*
* @property VObject\Property\FlatText VERSION
* @property Property\FlatText VERSION
*/
abstract class Document extends Component
{
Expand Down Expand Up @@ -140,7 +138,7 @@ public function create(string $name)
* an iCalendar object, this may be something like CALSCALE:GREGORIAN. To
* ensure that this does not happen, set $defaults to false.
*/
public function createComponent(string $name, array $children = null, bool $defaults = true): Component
public function createComponent(string $name, ?array $children = null, bool $defaults = true): Component
{
$name = strtoupper($name);
$class = Component::class;
Expand Down Expand Up @@ -169,7 +167,7 @@ public function createComponent(string $name, array $children = null, bool $defa
*
* @throws InvalidDataException
*/
public function createProperty(string $name, $value = null, array $parameters = null, string $valueType = null): Property
public function createProperty(string $name, $value = null, ?array $parameters = null, ?string $valueType = null): Property
{
// If there's a . in the name, it means it's prefixed by a group name.
if (false !== ($i = strpos($name, '.'))) {
Expand Down
4 changes: 2 additions & 2 deletions lib/FreeBusyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class FreeBusyGenerator
* Check the setTimeRange and setObjects methods for details about the
* arguments.
*/
public function __construct(\DateTimeInterface $start = null, \DateTimeInterface $end = null, $objects = null, \DateTimeZone $timeZone = null)
public function __construct(?\DateTimeInterface $start = null, ?\DateTimeInterface $end = null, $objects = null, ?\DateTimeZone $timeZone = null)
{
$this->setTimeRange($start, $end);

Expand Down Expand Up @@ -138,7 +138,7 @@ public function setObjects($objects): void
*
* @throws \Exception
*/
public function setTimeRange(\DateTimeInterface $start = null, \DateTimeInterface $end = null): void
public function setTimeRange(?\DateTimeInterface $start = null, ?\DateTimeInterface $end = null): void
{
if (!$start) {
$start = new \DateTimeImmutable(Settings::$minDate);
Expand Down
8 changes: 4 additions & 4 deletions lib/ITip/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Broker
* @throws MaxInstancesExceededException
* @throws NoInstancesException
*/
public function processMessage(Message $itipMessage, VCalendar $existingObject = null)
public function processMessage(Message $itipMessage, ?VCalendar $existingObject = null)
{
// We only support events at the moment.
if ('VEVENT' !== $itipMessage->component) {
Expand Down Expand Up @@ -268,7 +268,7 @@ public function parseEvent($calendar, $userHref, $oldCalendar = null): array
* This is message from an organizer, and is either a new event
* invite, or an update to an existing one.
*/
protected function processMessageRequest(Message $itipMessage, VCalendar $existingObject = null): ?VCalendar
protected function processMessageRequest(Message $itipMessage, ?VCalendar $existingObject = null): ?VCalendar
{
if (!$existingObject) {
// This is a new invite, and we're just going to copy over
Expand Down Expand Up @@ -296,7 +296,7 @@ protected function processMessageRequest(Message $itipMessage, VCalendar $existi
* attendee got removed from an event, or an event got cancelled
* altogether.
*/
protected function processMessageCancel(Message $itipMessage, VCalendar $existingObject = null): ?VCalendar
protected function processMessageCancel(Message $itipMessage, ?VCalendar $existingObject = null): ?VCalendar
{
if (!$existingObject) {
// The event didn't exist in the first place, so we're just
Expand All @@ -321,7 +321,7 @@ protected function processMessageCancel(Message $itipMessage, VCalendar $existin
* @throws MaxInstancesExceededException
* @throws NoInstancesException
*/
protected function processMessageReply(Message $itipMessage, VCalendar $existingObject = null): ?VCalendar
protected function processMessageReply(Message $itipMessage, ?VCalendar $existingObject = null): ?VCalendar
{
// A reply can only be processed based on an existing object.
// If the object is not available, the reply is ignored.
Expand Down
2 changes: 1 addition & 1 deletion lib/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract class Property extends Node
* @param array $parameters List of parameters
* @param string|null $group The vcard property group
*/
public function __construct(Component $root, ?string $name, $value = null, array $parameters = [], string $group = null)
public function __construct(Component $root, ?string $name, $value = null, array $parameters = [], ?string $group = null)
{
$this->name = $name;
$this->group = $group;
Expand Down
Loading

0 comments on commit 40082af

Please sign in to comment.