Skip to content

Commit

Permalink
Allow address formats to declare default values.
Browse files Browse the repository at this point in the history
Fixes #209.
  • Loading branch information
bojanz committed Jan 9, 2024
1 parent d7faef1 commit f9007af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/AddressFormat/AddressFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class AddressFormat
*/
protected array $uppercaseFields = [];

protected array $defaultValues = [];

protected ?string $administrativeAreaType = null;

protected ?string $localityType = null;
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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.
*
Expand Down
30 changes: 24 additions & 6 deletions tests/AddressFormat/AddressFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
Expand All @@ -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",
Expand All @@ -56,6 +69,7 @@ public function testInvalidSubdivision(): void
* @covers ::getUsedSubdivisionFields
* @covers ::getRequiredFields
* @covers ::getUppercaseFields
* @covers ::getDefaultValues
* @covers ::getAdministrativeAreaType
* @covers ::getLocalityType
* @covers ::getDependentLocalityType
Expand All @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit f9007af

Please sign in to comment.