Skip to content

Commit

Permalink
Merge pull request #674 from heiglandreas/allow-unknown-value-data-types
Browse files Browse the repository at this point in the history
Allow unknown value data types for VALUE
  • Loading branch information
phil-davis authored Oct 22, 2024
2 parents 2fcc82e + 1f22f4a commit c8ef5fd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,30 @@ public function createProperty(string $name, $value = null, ?array $parameters =

$class = null;

// If a VALUE parameter is supplied, we have to use that
// According to https://datatracker.ietf.org/doc/html/rfc5545#section-3.2.20
// If the property's value is the default value type, then this
// parameter need not be specified. However, if the property's
// default value type is overridden by some other allowable value
// type, then this parameter MUST be specified.
if (!$valueType) {
$valueType = $parameters['VALUE'] ?? null;
}

if ($valueType) {
// The valueType argument comes first to figure out the correct
// class.
$class = $this->getClassNameForPropertyValue($valueType);
}

// If the value parameter is not set or set to something we do not recognize
// we do not attempt to interpret or parse the datass value as specified in
// https://datatracker.ietf.org/doc/html/rfc5545#section-3.2.20
// So when we so far did not get a class-name, we use the default for the property
if (is_null($class)) {
// If a VALUE parameter is supplied, we should use that.
if (isset($parameters['VALUE'])) {
$class = $this->getClassNameForPropertyValue($parameters['VALUE']);
if (is_null($class)) {
throw new InvalidDataException('Unsupported VALUE parameter for '.$name.' property. You supplied "'.$parameters['VALUE'].'"');
}
} else {
$class = $this->getClassNameForPropertyName($name);
}
$class = $this->getClassNameForPropertyName($name);
}

if (is_null($parameters)) {
$parameters = [];
}
Expand Down
15 changes: 15 additions & 0 deletions tests/VObject/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VCard;
use Sabre\VObject\Property\ICalendar\DateTime;

class PropertyTest extends TestCase
{
Expand Down Expand Up @@ -391,4 +392,18 @@ public function testValidateBadEncodingVCard21(): void
self::assertEquals('ENCODING=B is not valid for this document type.', $result[0]['message']);
self::assertEquals(3, $result[0]['level']);
}

public function testUnknownValuesWillBeIgnored(): void
{
$cal = new VCalendar();
$property = $cal->createProperty('DTSTAMP', '20240101T000000Z', ['VALUE' => 'DATETIME']);

self::assertEquals("DTSTAMP;VALUE=DATETIME:20240101T000000Z\r\n", $property->serialize());

self::assertInstanceOf(DateTime::class, $property);
self::assertCount(1, $property->parameters());
self::assertInstanceOf(Parameter::class, $property->parameters['VALUE']);
self::assertEquals('VALUE', $property->parameters['VALUE']->name);
self::assertEquals('DATETIME', $property->parameters['VALUE']->getValue());
}
}

0 comments on commit c8ef5fd

Please sign in to comment.