Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unknown value data types [4.5] #699

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions lib/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,30 @@ public function createProperty($name, $value = null, ?array $parameters = null,

$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 @@ -385,4 +386,18 @@ public function testValidateBadEncodingVCard21()
$this->assertEquals('ENCODING=B is not valid for this document type.', $result[0]['message']);
$this->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());
}
}
Loading