/**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$value instanceof AddressInterface) {
         throw new UnexpectedTypeException($value, 'AddressInterface');
     }
     $address = $value;
     $countryCode = $address->getCountryCode();
     if ($countryCode === null || $countryCode === '') {
         return;
     }
     $values = $this->extractAddressValues($address);
     $addressFormat = $this->addressFormatRepository->get($address->getCountryCode());
     $this->validateFields($values, $addressFormat, $constraint);
     $subdivisions = $this->validateSubdivisions($values, $addressFormat, $constraint);
     $this->validatePostalCode($address->getPostalCode(), $subdivisions, $addressFormat, $constraint);
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function format(AddressInterface $address)
 {
     $countryCode = $address->getCountryCode();
     $addressFormat = $this->addressFormatRepository->get($countryCode);
     // Add the country to the bottom or the top of the format string,
     // depending on whether the format is minor-to-major or major-to-minor.
     if (LocaleHelper::match($addressFormat->getLocale(), $address->getLocale())) {
         $formatString = '%country' . "\n" . $addressFormat->getLocalFormat();
     } else {
         $formatString = $addressFormat->getFormat() . "\n" . '%country';
     }
     $view = $this->buildView($address, $addressFormat);
     $view = $this->renderView($view);
     // Insert the rendered elements into the format string.
     $replacements = [];
     foreach ($view as $key => $element) {
         $replacements['%' . $key] = $element;
     }
     $output = strtr($formatString, $replacements);
     $output = $this->cleanupOutput($output);
     if (!empty($this->options['html'])) {
         $output = nl2br($output, false);
         // Add the HTML wrapper element.
         $attributes = $this->renderAttributes($this->options['html_attributes']);
         $prefix = '<' . $this->options['html_tag'] . ' ' . $attributes . '>' . "\n";
         $suffix = "\n" . '</' . $this->options['html_tag'] . '>';
         $output = $prefix . $output . $suffix;
     }
     return $output;
 }