Skip to content

Commit 5f53d2d

Browse files
committed
feat(VCard): add getByTypes method (#717)
1 parent 90f3757 commit 5f53d2d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/Component/VCard.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,33 @@ public function getByType(string $propertyName, string $type)
409409
return null;
410410
}
411411

412+
/**
413+
* Returns a property with a specific TYPE value (ADR, TEL, or EMAIL).
414+
*
415+
* This function will return null if the exact property list does not exist.
416+
*
417+
* For example to get the property of `TEL;TYPE=HOME,CELL`
418+
* you would call `getByTypes('TEL', ['HOME', 'CELL'])`
419+
*
420+
* @param string[] $types
421+
* @return \ArrayAccess|array|null
422+
*/
423+
public function getByTypes(string $propertyName, array $types)
424+
{
425+
$types = array_map("strtolower", $types);
426+
foreach ($this->select($propertyName) as $field) {
427+
if (isset($field['TYPE'])) {
428+
$parts = array_map("strtolower", $field['TYPE']->getParts());
429+
430+
if (!array_diff($types, $parts) && !array_diff($parts, $types)) {
431+
return $field;
432+
}
433+
}
434+
}
435+
436+
return null;
437+
}
438+
412439
/**
413440
* This method should return a list of default property values.
414441
*/

tests/VObject/Component/VCardTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,27 @@ public function testGetByType(): void
147147
self::assertNull($vcard->getByType('ADR', 'non-existent'));
148148
}
149149

150+
public function testGetByTypes(): void
151+
{
152+
$vcard = <<<VCF
153+
BEGIN:VCARD
154+
VERSION:3.0
155+
TEL;TYPE=HOME,CELL:112233445566
156+
TEL;TYPE=WORK,cell:665544332211
157+
TEL;TYPE=WORK:7778889994455
158+
TEL;TYPE=EXTERNAL:555555555
159+
END:VCARD
160+
VCF;
161+
162+
$vcard = VObject\Reader::read($vcard);
163+
self::assertEquals('112233445566', $vcard->getByTypes('TEL', ['home', 'cell'])->getValue());
164+
self::assertEquals('665544332211', $vcard->getByTypes('TEL', ['work', 'cell'])->getValue());
165+
self::assertEquals('7778889994455', $vcard->getByTypes('TEL', ['work'])->getValue());
166+
self::assertEquals('555555555', $vcard->getByTypes('TEL', ['external'])->getValue());
167+
self::assertNull($vcard->getByTypes('TEL', ['non-existent']));
168+
self::assertNull($vcard->getByTypes('EMAIL', ['non-existent']));
169+
}
170+
150171
public function testPreferredNoPref(): void
151172
{
152173
$vcard = <<<VCF

0 commit comments

Comments
 (0)