/**
  * Validates the provided subdivision values.
  *
  * @param array                  $values        The field values, keyed by field constants.
  * @param AddressFormatInterface $addressFormat The address format.
  * @param Constraint             $constraint    The constraint.
  *
  * @return array An array of found valid subdivisions.
  */
 protected function validateSubdivisions($values, AddressFormatInterface $addressFormat, $constraint)
 {
     $countryCode = $addressFormat->getCountryCode();
     $subdivisionFields = $addressFormat->getUsedSubdivisionFields();
     $foundIds = [];
     foreach ($subdivisionFields as $index => $field) {
         if (empty($values[$field]) || !in_array($field, $constraint->fields)) {
             // The field is empty or validation is disabled.
             break;
         }
         $parentField = $index ? $subdivisionFields[$index - 1] : null;
         $parentId = $parentField ? $values[$parentField] : null;
         $children = $this->subdivisionRepository->getList($countryCode, $parentId);
         if (!$children) {
             // No predefined subdivisions found.
             break;
         }
         $found = false;
         $value = $values[$field];
         if (isset($children[$value])) {
             $found = true;
             $foundIds[] = $value;
         }
         if (!$found) {
             $this->addViolation($field, $constraint->invalidMessage, $value, $addressFormat);
             break;
         }
     }
     // Load the found subdivision ids.
     $subdivisions = [];
     foreach ($foundIds as $id) {
         $subdivisions[] = $this->subdivisionRepository->get($id);
     }
     return $subdivisions;
 }
 /**
  * Gets the address values used to build the view.
  *
  * @param AddressInterface       $address       The address.
  * @param AddressFormatInterface $addressFormat The address format.
  *
  * @return array The values, keyed by address field.
  */
 protected function getValues(AddressInterface $address, AddressFormatInterface $addressFormat)
 {
     $values = [];
     foreach (AddressField::getAll() as $field) {
         $getter = 'get' . ucfirst($field);
         $values[$field] = $address->{$getter}();
     }
     // Replace the subdivision values with the names of any predefined ones.
     foreach ($addressFormat->getUsedSubdivisionFields() as $field) {
         if (empty($values[$field])) {
             // This level is empty, so there can be no sublevels.
             break;
         }
         $subdivision = $this->subdivisionRepository->get($values[$field], $address->getLocale());
         if (!$subdivision) {
             // This level has no predefined subdivisions, stop.
             break;
         }
         $values[$field] = $subdivision->getCode();
         if (!$subdivision->hasChildren()) {
             // The current subdivision has no children, stop.
             break;
         }
     }
     return $values;
 }