Indicates the number of levels of predefined subdivisions.
Note that a country might use a subdivision field without having
predefined subdivisions for it.
For example, if the locality field is used by the address format, but
the subdivision depth is 1, that means that the field element should be
rendered as a textbox, since there's no known data to put in a dropdown.
It is also possible to have no subdivisions for specific parents, even
though the country generally has predefined subdivisions at that depth.
public getSubdivisionDepth ( ) : integer | ||
return | integer | The subdivision depth. Possible values: 0: no subdivisions have been predefined. 1: administrative areas. 2: administrative areas, localities. 3: administrative areas, localities, dependent localities. |
/** * @covers ::__construct * @covers ::getCountryCode * @covers ::getLocale * @covers ::getFormat * @covers ::getLocalFormat * @covers ::getUsedFields * @covers ::getUsedSubdivisionFields * @covers ::getRequiredFields * @covers ::getUppercaseFields * @covers ::getAdministrativeAreaType * @covers ::getLocalityType * @covers ::getDependentLocalityType * @covers ::getPostalCodeType * @covers ::getPostalCodePattern * @covers ::getPostalCodePrefix * @covers ::getSubdivisionDepth */ public function testValid() { $definition = ['country_code' => 'US', 'locale' => 'en', 'format' => "%givenName %familyName\n%organization\n%addressLine1\n%addressLine2\n%locality, %administrativeArea %postalCode", 'local_format' => '%postalCode\\n%addressLine1\\n%organization\\n%givenName %familyName', 'required_fields' => [AddressField::ADMINISTRATIVE_AREA, AddressField::LOCALITY, AddressField::POSTAL_CODE, AddressField::ADDRESS_LINE1], 'uppercase_fields' => [AddressField::ADMINISTRATIVE_AREA, AddressField::LOCALITY], 'administrative_area_type' => AdministrativeAreaType::STATE, 'locality_type' => LocalityType::CITY, 'dependent_locality_type' => DependentLocalityType::DISTRICT, 'postal_code_type' => PostalCodeType::ZIP, 'postal_code_pattern' => '(\\d{5})(?:[ \\-](\\d{4}))?', 'postal_code_prefix' => 'US', 'subdivision_depth' => 1]; $addressFormat = new AddressFormat($definition); $this->assertEquals($definition['country_code'], $addressFormat->getCountryCode()); $this->assertEquals($definition['locale'], $addressFormat->getLocale()); $this->assertEquals($definition['format'], $addressFormat->getFormat()); $this->assertEquals($definition['local_format'], $addressFormat->getLocalFormat()); $this->assertEquals($definition['required_fields'], $addressFormat->getRequiredFields()); $this->assertEquals($definition['uppercase_fields'], $addressFormat->getUppercaseFields()); $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. $this->assertNull($addressFormat->getDependentLocalityType()); $this->assertEquals($definition['postal_code_type'], $addressFormat->getPostalCodeType()); $this->assertEquals($definition['postal_code_pattern'], $addressFormat->getPostalCodePattern()); $this->assertEquals($definition['postal_code_prefix'], $addressFormat->getPostalCodePrefix()); $this->assertEquals($definition['subdivision_depth'], $addressFormat->getSubdivisionDepth()); $expectedUsedFields = [AddressField::ADMINISTRATIVE_AREA, AddressField::LOCALITY, AddressField::POSTAL_CODE, AddressField::ADDRESS_LINE1, AddressField::ADDRESS_LINE2, AddressField::ORGANIZATION, AddressField::GIVEN_NAME, AddressField::FAMILY_NAME]; $this->assertEquals($expectedUsedFields, $addressFormat->getUsedFields()); $expectedUsedSubdivisionFields = [AddressField::ADMINISTRATIVE_AREA, AddressField::LOCALITY]; $this->assertEquals($expectedUsedSubdivisionFields, $addressFormat->getUsedSubdivisionFields()); }
/** * Validates the provided subdivision values. * * @param array $values The field values, keyed by field constants. * @param AddressFormat $addressFormat The address format. * @param Constraint $constraint The constraint. * * @return array An array of found valid subdivisions. */ protected function validateSubdivisions($values, AddressFormat $addressFormat, $constraint) { $countryCode = $addressFormat->getCountryCode(); if ($addressFormat->getSubdivisionDepth() < 1) { // No predefined subdivisions exist, nothing to validate against. return []; } $subdivisionFields = $addressFormat->getUsedSubdivisionFields(); $parents = []; $subdivisions = []; foreach ($subdivisionFields as $index => $field) { if (empty($values[$field]) || !in_array($field, $constraint->fields)) { // The field is empty or validation is disabled. break; } $parents[] = $index ? $values[$subdivisionFields[$index - 1]] : $countryCode; $subdivision = $this->subdivisionRepository->get($values[$field], $parents); if (!$subdivision) { $this->addViolation($field, $constraint->invalidMessage, $values[$field], $addressFormat); break; } $subdivisions[] = $subdivision; if (!$subdivision->hasChildren()) { // No predefined subdivisions below this level, stop here. break; } } return $subdivisions; }