diff --git a/src/AddressFormat/AddressFormat.php b/src/AddressFormat/AddressFormat.php index 88348bbc..5c3a0b0c 100644 --- a/src/AddressFormat/AddressFormat.php +++ b/src/AddressFormat/AddressFormat.php @@ -29,6 +29,8 @@ class AddressFormat */ protected array $uppercaseFields = []; + protected array $defaultValues = []; + protected ?string $administrativeAreaType = null; protected ?string $localityType = null; @@ -65,6 +67,10 @@ public function __construct(array $definition) AddressField::assertAllExist($definition['required_fields']); $this->requiredFields = $definition['required_fields']; } + if (isset($definition['default_values'])) { + AddressField::assertAllExist(array_keys($definition['default_values'])); + $this->defaultValues = $definition['default_values']; + } if (isset($definition['uppercase_fields'])) { AddressField::assertAllExist($definition['uppercase_fields']); $this->uppercaseFields = $definition['uppercase_fields']; @@ -211,12 +217,24 @@ public function getRequiredFields(): array /** * Gets the list of fields that need to be uppercased. + * + * @return AddressField[] */ public function getUppercaseFields(): array { return $this->uppercaseFields; } + /** + * Gets the default values. + * + * @return array The default values, keyed by field name. + */ + public function getDefaultValues(): array + { + return $this->defaultValues; + } + /** * Gets the administrative area type. * diff --git a/tests/AddressFormat/AddressFormatTest.php b/tests/AddressFormat/AddressFormatTest.php index 2afece7a..930188e3 100644 --- a/tests/AddressFormat/AddressFormatTest.php +++ b/tests/AddressFormat/AddressFormatTest.php @@ -17,12 +17,11 @@ final class AddressFormatTest extends TestCase { /** * @covers ::__construct - * - * */ - public function testMissingProperty(): void + public function testMissingFormat(): void { $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Missing required property format.'); $definition = [ 'country_code' => 'US', ]; @@ -31,12 +30,26 @@ public function testMissingProperty(): void /** * @covers ::__construct - * - * */ - public function testInvalidSubdivision(): void + public function testInvalidDefaultValue(): void { $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('"unknown" is not a valid AddressField value.'); + $definition = [ + 'country_code' => 'US', + 'format' => "%givenName %familyName\n%organization\n%addressLine1\n%addressLine2\n%addressLine3\n%dependentLocality", + 'default_values' => ['unknown' => 'CA'], + ]; + $addressFormat = new AddressFormat($definition); + } + + /** + * @covers ::__construct + */ + public function testInvalidDependentLocalityType(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('"WRONG" is not a valid DependentLocalityType value.'); $definition = [ 'country_code' => 'US', 'format' => "%givenName %familyName\n%organization\n%addressLine1\n%addressLine2\n%addressLine3\n%dependentLocality", @@ -56,6 +69,7 @@ public function testInvalidSubdivision(): void * @covers ::getUsedSubdivisionFields * @covers ::getRequiredFields * @covers ::getUppercaseFields + * @covers ::getDefaultValues * @covers ::getAdministrativeAreaType * @covers ::getLocalityType * @covers ::getDependentLocalityType @@ -82,6 +96,9 @@ public function testValid(): void AddressField::ADMINISTRATIVE_AREA, AddressField::LOCALITY, ], + 'default_values' => [ + AddressField::ADMINISTRATIVE_AREA => 'CA', + ], 'administrative_area_type' => AdministrativeAreaType::STATE, 'locality_type' => LocalityType::CITY, 'dependent_locality_type' => DependentLocalityType::DISTRICT, @@ -99,6 +116,7 @@ public function testValid(): void $this->assertEquals($definition['local_format'], $addressFormat->getLocalFormat()); $this->assertEquals($definition['required_fields'], $addressFormat->getRequiredFields()); $this->assertEquals($definition['uppercase_fields'], $addressFormat->getUppercaseFields()); + $this->assertEquals($definition['default_values'], $addressFormat->getDefaultValues()); $this->assertEquals($definition['administrative_area_type'], $addressFormat->getAdministrativeAreaType()); $this->assertEquals($definition['locality_type'], $addressFormat->getLocalityType()); // The format has no %dependentLocality, the type must be NULL.