diff --git a/lib/Component/VCard.php b/lib/Component/VCard.php index 6bbec571..0dafb674 100644 --- a/lib/Component/VCard.php +++ b/lib/Component/VCard.php @@ -409,6 +409,34 @@ public function getByType(string $propertyName, string $type) return null; } + /** + * Returns a property with a specific TYPE value (ADR, TEL, or EMAIL). + * + * This function will return null if the exact property list does not exist. + * + * For example to get the property of `TEL;TYPE=HOME,CELL` + * you would call `getByTypes('TEL', ['HOME', 'CELL'])` + * + * @param string[] $types + * + * @return \ArrayAccess|array|null + */ + public function getByTypes(string $propertyName, array $types) + { + $types = array_map('strtolower', $types); + foreach ($this->select($propertyName) as $field) { + if (isset($field['TYPE'])) { + $parts = array_map('strtolower', $field['TYPE']->getParts()); + + if (!array_diff($types, $parts) && !array_diff($parts, $types)) { + return $field; + } + } + } + + return null; + } + /** * This method should return a list of default property values. */ diff --git a/tests/VObject/Component/VCardTest.php b/tests/VObject/Component/VCardTest.php index 3851d97d..ff4979ed 100644 --- a/tests/VObject/Component/VCardTest.php +++ b/tests/VObject/Component/VCardTest.php @@ -147,6 +147,27 @@ public function testGetByType(): void self::assertNull($vcard->getByType('ADR', 'non-existent')); } + public function testGetByTypes(): void + { + $vcard = <<getByTypes('TEL', ['home', 'cell'])->getValue()); + self::assertEquals('665544332211', $vcard->getByTypes('TEL', ['work', 'cell'])->getValue()); + self::assertEquals('7778889994455', $vcard->getByTypes('TEL', ['work'])->getValue()); + self::assertEquals('555555555', $vcard->getByTypes('TEL', ['external'])->getValue()); + self::assertNull($vcard->getByTypes('TEL', ['non-existent'])); + self::assertNull($vcard->getByTypes('EMAIL', ['non-existent'])); + } + public function testPreferredNoPref(): void { $vcard = <<