/**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if ($value === null || $value === '') {
         return;
     }
     if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
         throw new UnexpectedTypeException($value, 'string');
     }
     $countries = $this->countryRepository->getList();
     $value = (string) $value;
     if (!isset($countries[$value])) {
         if ($this->context instanceof \Symfony\Component\Validator\Context\ExecutionContextInterface) {
             $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->addViolation();
         } else {
             $this->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->addViolation();
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Builds the view for the given address.
  *
  * @param AddressInterface $address       The address.
  * @param AddressFormat    $addressFormat The address format.
  *
  * @return array The view.
  */
 protected function buildView(AddressInterface $address, AddressFormat $addressFormat)
 {
     $countries = $this->countryRepository->getList($this->locale);
     $values = $this->getValues($address, $addressFormat);
     $view = [];
     $view['country'] = ['html_tag' => 'span', 'html_attributes' => ['class' => 'country'], 'value' => $countries[$address->getCountryCode()]];
     foreach ($addressFormat->getUsedFields() as $field) {
         // The constant is more suitable as a class than the value since
         // it's snake_case and not camelCase.
         $class = str_replace('_', '-', strtolower(AddressField::getKey($field)));
         $view[$field] = ['html_tag' => 'span', 'html_attributes' => ['class' => $class], 'value' => $values[$field]];
     }
     return $view;
 }