public static getGroupedFields ( $format ) : array | ||
return | array | An array of address fields grouped by line, in the same order as they appear in the format string. For example: [ [givenName, familyName], [organization], [addressLine1], [addressLine2], [locality, administrativeArea, postalCode] ] |
public static function getGroupedFields($format)
{
$groupedFields = [];
$expression = '/\\%(' . implode('|', AddressField::getAll()) . ')/';
$formatLines = explode("\n", $format);
foreach ($formatLines as $index => $formatLine) {
preg_match_all($expression, $formatLine, $foundTokens);
foreach ($foundTokens[0] as $token) {
$groupedFields[$index][] = substr($token, 1);
}
}
// The indexes won't be sequential if there were any rows
// without tokens, so reset them.
$groupedFields = array_values($groupedFields);
return $groupedFields;
}
/** * @covers ::getGroupedFields */ public function testGetGroupedFields() { $format = "%givenName %familyName\n%organization\n%addressLine1\n%addressLine2\n%locality, %postalCode"; $expectedGroupedFields = [[AddressField::GIVEN_NAME, AddressField::FAMILY_NAME], [AddressField::ORGANIZATION], [AddressField::ADDRESS_LINE1], [AddressField::ADDRESS_LINE2], [AddressField::LOCALITY, AddressField::POSTAL_CODE]]; $this->assertEquals($expectedGroupedFields, AddressFormatHelper::getGroupedFields($format)); }