Skip to content

Commit

Permalink
Add PHONE-NUMBER value type (used for TEL in vCard 3.0) (sabre-io#486)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kraus <hanzi@hanzi.cc>
  • Loading branch information
hanzi authored and bcaller committed Jul 27, 2020
1 parent 9d6f955 commit 411317b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Component/VCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class VCard extends VObject\Document
'FLOAT' => 'Sabre\\VObject\\Property\\FloatValue',
'INTEGER' => 'Sabre\\VObject\\Property\\IntegerValue',
'LANGUAGE-TAG' => 'Sabre\\VObject\\Property\\VCard\\LanguageTag',
'PHONE-NUMBER' => 'Sabre\\VObject\\Property\\VCard\\PhoneNumber', // vCard 3.0 only
'TIMESTAMP' => 'Sabre\\VObject\\Property\\VCard\\TimeStamp',
'TEXT' => 'Sabre\\VObject\\Property\\Text',
'TIME' => 'Sabre\\VObject\\Property\\Time',
Expand Down
30 changes: 30 additions & 0 deletions lib/Property/VCard/PhoneNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Sabre\VObject\Property\VCard;

use Sabre\VObject\Property;

/**
* PhoneNumber property.
*
* This object encodes PHONE-NUMBER values.
*
* @author Christian Kraus <christian@kraus.work>
*/
class PhoneNumber extends Property\Text
{
protected $structuredValues = [];

/**
* Returns the type of value.
*
* This corresponds to the VALUE= parameter. Every property also has a
* 'default' valueType.
*
* @return string
*/
public function getValueType()
{
return 'PHONE-NUMBER';
}
}
3 changes: 3 additions & 0 deletions lib/VCardConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ protected function convertProperty(Component\VCard $input, Component\VCard $outp
if (!$valueType) {
$valueType = $property->getValueType();
}
if (Document::VCARD30 !== $targetVersion && 'PHONE-NUMBER' === $valueType) {
$valueType = null;
}
$newProperty = $output->createProperty(
$property->name,
$property->getParts(),
Expand Down
19 changes: 19 additions & 0 deletions tests/VObject/Property/VCard/PhoneNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Sabre\VObject\Property\VCard;

use PHPUnit\Framework\TestCase;
use Sabre\VObject;

class PhoneNumberTest extends TestCase
{
public function testParser()
{
$input = "BEGIN:VCARD\r\nVERSION:3.0\r\nTEL;TYPE=HOME;VALUE=PHONE-NUMBER:+1234\r\nEND:VCARD\r\n";

$vCard = VObject\Reader::read($input);
$this->assertInstanceOf('Sabre\VObject\Property\VCard\PhoneNumber', $vCard->TEL);
$this->assertEquals('PHONE-NUMBER', $vCard->TEL->getValueType());
$this->assertEquals($input, $vCard->serialize());
}
}
30 changes: 30 additions & 0 deletions tests/VObject/VCardConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,34 @@ public function testNoLabel()

$this->assertEquals($expected, str_replace("\r", '', $vcard));
}

public function testPhoneNumberValueTypeGetsRemoved()
{
$input = <<<VCF
BEGIN:VCARD
VERSION:3.0
UID:foo
FN:John Doe
TEL;TYPE=HOME;VALUE=PHONE-NUMBER:+1234
END:VCARD
VCF;

$output = <<<VCF
BEGIN:VCARD
VERSION:4.0
UID:foo
FN:John Doe
TEL;TYPE=HOME:+1234
END:VCARD
VCF;

$vcard = Reader::read($input);
$vcard = $vcard->convert(Document::VCARD40);

$this->assertVObjectEqualsVObject(
$output,
$vcard
);
}
}

0 comments on commit 411317b

Please sign in to comment.