Skip to content

Commit

Permalink
Merge pull request #72 from wadmiraal/master
Browse files Browse the repository at this point in the history
Issue #69: Handle grouped properties
  • Loading branch information
jeroendesloovere authored Feb 14, 2017
2 parents b352fa6 + 4fe278a commit 1973888
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/VCardParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ protected function parse()
} elseif (strtoupper($line) == "END:VCARD") {
$this->vcardObjects[] = $cardData;
} elseif (!empty($line)) {
// Strip grouping information. We don't use the group names. We
// simply use a list for entries that have multiple values.
// As per RFC, group names are alphanumerical, and end with a
// period (.).
$line = preg_replace('/^\w+\./', '', $line);

$type = '';
$value = '';
@list($type, $value) = explode(':', $line, 2);
Expand All @@ -172,6 +178,16 @@ protected function parse()
$element = strtoupper($types[0]);

array_shift($types);

// Normalize types. A type can either be a type-param directly,
// or can be prefixed with "type=". E.g.: "INTERNET" or
// "type=INTERNET".
if (!empty($types)) {
$types = array_map(function($type) {
return preg_replace('/^type=/i', '', $type);
}, $types);
}

$i = 0;
$rawValue = false;
foreach ($types as $type) {
Expand Down
10 changes: 7 additions & 3 deletions tests/VCardParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ public function testUrl()
$this->assertEquals($parser->getCardAtIndex(0)->url['PREF;WORK'][0], 'http://work1.example.com');
$this->assertEquals($parser->getCardAtIndex(0)->url['PREF;WORK'][1], 'http://work2.example.com');
}

public function testNote()
{
$vcard = new VCard();
$vcard->addNote('This is a testnote');
$parser = new VCardParser($vcard->buildVCard());

$vcardMultiline = new VCard();
$vcardMultiline->addNote("This is a multiline note\nNew line content!\r\nLine 2");
$parserMultiline = new VCardParser($vcardMultiline->buildVCard());

$this->assertEquals($parser->getCardAtIndex(0)->note, 'This is a testnote');
$this->assertEquals(nl2br($parserMultiline->getCardAtIndex(0)->note), nl2br("This is a multiline note" . PHP_EOL . "New line content!" . PHP_EOL . "Line 2"));
}
Expand Down Expand Up @@ -275,6 +275,10 @@ public function testFromFile()
$this->assertEquals($cards[0]->firstname, "Wouter");
$this->assertEquals($cards[0]->lastname, "Admiraal");
$this->assertEquals($cards[0]->fullname, "Wouter Admiraal");
// Check the parsing of grouped items as well, which are present in the
// example file.
$this->assertEquals($cards[0]->url['default'][0], 'http://example.com');
$this->assertEquals($cards[0]->email['INTERNET'][0], 'site@example.com');
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/example.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ VERSION:3.0
REV:2016-05-30T10:36:13Z
N;CHARSET=utf-8:Admiraal;Wouter;;;
FN;CHARSET=utf-8:Wouter Admiraal
item1.EMAIL;type=INTERNET:site@example.com
item1.X-ABLabel:$!<Email>!$
item2.URL:http://example.com
item2.X-ABLabel:$!<Home>!$
END:VCARD

0 comments on commit 1973888

Please sign in to comment.