update sabre/vobject

This commit is contained in:
Mario Vavti
2019-04-25 11:47:18 +02:00
parent f1c0797780
commit a60c2f38c6
91 changed files with 2828 additions and 3749 deletions

View File

@@ -16,8 +16,8 @@ use DateTimeZone;
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class DateTimeParser {
class DateTimeParser
{
/**
* Parses an iCalendar (rfc5545) formatted datetime and returns a
* DateTimeImmutable object.
@@ -26,49 +26,48 @@ class DateTimeParser {
* if the non-UTC format is used. The argument is used as a reference, the
* returned DateTimeImmutable object will still be in the UTC timezone.
*
* @param string $dt
* @param string $dt
* @param DateTimeZone $tz
*
* @return DateTimeImmutable
*/
static function parseDateTime($dt, DateTimeZone $tz = null) {
public static function parseDateTime($dt, DateTimeZone $tz = null)
{
// 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);
if (!$result) {
throw new InvalidDataException('The supplied iCalendar datetime value is incorrect: ' . $dt);
throw new InvalidDataException('The supplied iCalendar datetime value is incorrect: '.$dt);
}
if ($matches[7] === 'Z' || is_null($tz)) {
if ('Z' === $matches[7] || is_null($tz)) {
$tz = new DateTimeZone('UTC');
}
try {
$date = new DateTimeImmutable($matches[1] . '-' . $matches[2] . '-' . $matches[3] . ' ' . $matches[4] . ':' . $matches[5] . ':' . $matches[6], $tz);
$date = new DateTimeImmutable($matches[1].'-'.$matches[2].'-'.$matches[3].' '.$matches[4].':'.$matches[5].':'.$matches[6], $tz);
} catch (\Exception $e) {
throw new InvalidDataException('The supplied iCalendar datetime value is incorrect: ' . $dt);
throw new InvalidDataException('The supplied iCalendar datetime value is incorrect: '.$dt);
}
return $date;
}
/**
* Parses an iCalendar (rfc5545) formatted date and returns a DateTimeImmutable object.
*
* @param string $date
* @param string $date
* @param DateTimeZone $tz
*
* @return DateTimeImmutable
*/
static function parseDate($date, DateTimeZone $tz = null) {
public static function parseDate($date, DateTimeZone $tz = null)
{
// Format is YYYYMMDD
$result = preg_match('/^([0-9]{4})([0-1][0-9])([0-3][0-9])$/', $date, $matches);
if (!$result) {
throw new InvalidDataException('The supplied iCalendar date value is incorrect: ' . $date);
throw new InvalidDataException('The supplied iCalendar date value is incorrect: '.$date);
}
if (is_null($tz)) {
@@ -76,13 +75,12 @@ class DateTimeParser {
}
try {
$date = new DateTimeImmutable($matches[1] . '-' . $matches[2] . '-' . $matches[3], $tz);
$date = new DateTimeImmutable($matches[1].'-'.$matches[2].'-'.$matches[3], $tz);
} catch (\Exception $e) {
throw new InvalidDataException('The supplied iCalendar date value is incorrect: ' . $date);
throw new InvalidDataException('The supplied iCalendar date value is incorrect: '.$date);
}
return $date;
}
/**
@@ -92,22 +90,21 @@ class DateTimeParser {
* suitable for strtotime or DateTime::modify.
*
* @param string $duration
* @param bool $asString
* @param bool $asString
*
* @return DateInterval|string
*/
static function parseDuration($duration, $asString = false) {
public static function parseDuration($duration, $asString = false)
{
$result = preg_match('/^(?<plusminus>\+|-)?P((?<week>\d+)W)?((?<day>\d+)D)?(T((?<hour>\d+)H)?((?<minute>\d+)M)?((?<second>\d+)S)?)?$/', $duration, $matches);
if (!$result) {
throw new InvalidDataException('The supplied iCalendar duration value is incorrect: ' . $duration);
throw new InvalidDataException('The supplied iCalendar duration value is incorrect: '.$duration);
}
if (!$asString) {
$invert = false;
if ($matches['plusminus'] === '-') {
if ('-' === $matches['plusminus']) {
$invert = true;
}
@@ -120,7 +117,7 @@ class DateTimeParser {
];
foreach ($parts as $part) {
$matches[$part] = isset($matches[$part]) && $matches[$part] ? (int)$matches[$part] : 0;
$matches[$part] = isset($matches[$part]) && $matches[$part] ? (int) $matches[$part] : 0;
}
// We need to re-construct the $duration string, because weeks and
@@ -133,28 +130,26 @@ class DateTimeParser {
}
if ($days) {
$duration .= $days . 'D';
$duration .= $days.'D';
}
if ($matches['minute'] || $matches['second'] || $matches['hour']) {
$duration .= 'T';
if ($matches['hour']) {
$duration .= $matches['hour'] . 'H';
$duration .= $matches['hour'].'H';
}
if ($matches['minute']) {
$duration .= $matches['minute'] . 'M';
$duration .= $matches['minute'].'M';
}
if ($matches['second']) {
$duration .= $matches['second'] . 'S';
$duration .= $matches['second'].'S';
}
}
if ($duration === 'P') {
if ('P' === $duration) {
$duration = 'PT0S';
}
@@ -165,7 +160,6 @@ class DateTimeParser {
}
return $iv;
}
$parts = [
@@ -180,38 +174,36 @@ class DateTimeParser {
foreach ($parts as $part) {
if (isset($matches[$part]) && $matches[$part]) {
$newDur .= ' ' . $matches[$part] . ' ' . $part . 's';
$newDur .= ' '.$matches[$part].' '.$part.'s';
}
}
$newDur = ($matches['plusminus'] === '-' ? '-' : '+') . trim($newDur);
$newDur = ('-' === $matches['plusminus'] ? '-' : '+').trim($newDur);
if ($newDur === '+') {
if ('+' === $newDur) {
$newDur = '+0 seconds';
};
}
return $newDur;
}
/**
* Parses either a Date or DateTime, or Duration value.
*
* @param string $date
* @param string $date
* @param DateTimeZone|string $referenceTz
*
* @return DateTimeImmutable|DateInterval
*/
static function parse($date, $referenceTz = null) {
if ($date[0] === 'P' || ($date[0] === '-' && $date[1] === 'P')) {
public static function parse($date, $referenceTz = null)
{
if ('P' === $date[0] || ('-' === $date[0] && 'P' === $date[1])) {
return self::parseDuration($date);
} elseif (strlen($date) === 8) {
} elseif (8 === strlen($date)) {
return self::parseDate($date, $referenceTz);
} else {
return self::parseDateTime($date, $referenceTz);
}
}
/**
@@ -270,8 +262,8 @@ class DateTimeParser {
*
* @return array
*/
static function parseVCardDateTime($date) {
public static function parseVCardDateTime($date)
{
$regex = '/^
(?: # date part
(?:
@@ -296,7 +288,6 @@ class DateTimeParser {
$/x';
if (!preg_match($regex, $date, $matches)) {
// Attempting to parse the extended format.
$regex = '/^
(?: # date part
@@ -321,9 +312,8 @@ class DateTimeParser {
$/x';
if (!preg_match($regex, $date, $matches)) {
throw new InvalidDataException('Invalid vCard date-time string: ' . $date);
throw new InvalidDataException('Invalid vCard date-time string: '.$date);
}
}
$parts = [
'year',
@@ -332,24 +322,21 @@ class DateTimeParser {
'hour',
'minute',
'second',
'timezone'
'timezone',
];
$result = [];
foreach ($parts as $part) {
if (empty($matches[$part])) {
$result[$part] = null;
} elseif ($matches[$part] === '-' || $matches[$part] === '--') {
} elseif ('-' === $matches[$part] || '--' === $matches[$part]) {
$result[$part] = null;
} else {
$result[$part] = $matches[$part];
}
}
return $result;
}
/**
@@ -394,8 +381,8 @@ class DateTimeParser {
*
* @return array
*/
static function parseVCardTime($date) {
public static function parseVCardTime($date)
{
$regex = '/^
(?<hour> [0-9]{2} | -)
(?<minute> [0-9]{2} | -)?
@@ -409,9 +396,7 @@ class DateTimeParser {
)?
$/x';
if (!preg_match($regex, $date, $matches)) {
// Attempting to parse the extended format.
$regex = '/^
(?: (?<hour> [0-9]{2}) : | -)
@@ -427,32 +412,28 @@ class DateTimeParser {
$/x';
if (!preg_match($regex, $date, $matches)) {
throw new InvalidDataException('Invalid vCard time string: ' . $date);
throw new InvalidDataException('Invalid vCard time string: '.$date);
}
}
$parts = [
'hour',
'minute',
'second',
'timezone'
'timezone',
];
$result = [];
foreach ($parts as $part) {
if (empty($matches[$part])) {
$result[$part] = null;
} elseif ($matches[$part] === '-') {
} elseif ('-' === $matches[$part]) {
$result[$part] = null;
} else {
$result[$part] = $matches[$part];
}
}
return $result;
}
/**
@@ -506,32 +487,32 @@ class DateTimeParser {
*
* @return array
*/
static function parseVCardDateAndOrTime($date) {
public static function parseVCardDateAndOrTime($date)
{
// \d{8}|\d{4}-\d\d|--\d\d(\d\d)?|---\d\d
$valueDate = '/^(?J)(?:' .
'(?<year>\d{4})(?<month>\d\d)(?<date>\d\d)' .
'|(?<year>\d{4})-(?<month>\d\d)' .
'|--(?<month>\d\d)(?<date>\d\d)?' .
'|---(?<date>\d\d)' .
$valueDate = '/^(?J)(?:'.
'(?<year>\d{4})(?<month>\d\d)(?<date>\d\d)'.
'|(?<year>\d{4})-(?<month>\d\d)'.
'|--(?<month>\d\d)(?<date>\d\d)?'.
'|---(?<date>\d\d)'.
')$/';
// (\d\d(\d\d(\d\d)?)?|-\d\d(\d\d)?|--\d\d)(Z|[+\-]\d\d(\d\d)?)?
$valueTime = '/^(?J)(?:' .
'((?<hour>\d\d)((?<minute>\d\d)(?<second>\d\d)?)?' .
'|-(?<minute>\d\d)(?<second>\d\d)?' .
'|--(?<second>\d\d))' .
'(?<timezone>(Z|[+\-]\d\d(\d\d)?))?' .
$valueTime = '/^(?J)(?:'.
'((?<hour>\d\d)((?<minute>\d\d)(?<second>\d\d)?)?'.
'|-(?<minute>\d\d)(?<second>\d\d)?'.
'|--(?<second>\d\d))'.
'(?<timezone>(Z|[+\-]\d\d(\d\d)?))?'.
')$/';
// (\d{8}|--\d{4}|---\d\d)T\d\d(\d\d(\d\d)?)?(Z|[+\-]\d\d(\d\d?)?
$valueDateTime = '/^(?:' .
'((?<year0>\d{4})(?<month0>\d\d)(?<date0>\d\d)' .
'|--(?<month1>\d\d)(?<date1>\d\d)' .
'|---(?<date2>\d\d))' .
'T' .
'(?<hour>\d\d)((?<minute>\d\d)(?<second>\d\d)?)?' .
'(?<timezone>(Z|[+\-]\d\d(\d\d?)))?' .
$valueDateTime = '/^(?:'.
'((?<year0>\d{4})(?<month0>\d\d)(?<date0>\d\d)'.
'|--(?<month1>\d\d)(?<date1>\d\d)'.
'|---(?<date2>\d\d))'.
'T'.
'(?<hour>\d\d)((?<minute>\d\d)(?<second>\d\d)?)?'.
'(?<timezone>(Z|[+\-]\d\d(\d\d?)))?'.
')$/';
// date-and-or-time is date | date-time | time
@@ -540,17 +521,17 @@ class DateTimeParser {
if (0 === preg_match($valueDate, $date, $matches)
&& 0 === preg_match($valueDateTime, $date, $matches)
&& 0 === preg_match($valueTime, $date, $matches)) {
throw new InvalidDataException('Invalid vCard date-time string: ' . $date);
throw new InvalidDataException('Invalid vCard date-time string: '.$date);
}
$parts = [
'year' => null,
'month' => null,
'date' => null,
'hour' => null,
'minute' => null,
'second' => null,
'timezone' => null
'year' => null,
'month' => null,
'date' => null,
'hour' => null,
'minute' => null,
'second' => null,
'timezone' => null,
];
// The $valueDateTime expression has a bug with (?J) so we simulate it.
@@ -575,6 +556,5 @@ class DateTimeParser {
unset($parts['year0']);
return $parts;
}
}