/**
  * @param string        $value
  * @param string[]|null $context
  */
 public function isValid($value, $context = null)
 {
     if (!is_array($context) || !isset($context['countryCode'])) {
         $this->error(static::MISSING_COUNTRY_CODE);
         return false;
     }
     $countryCode = trim($context['countryCode']);
     $fields = $this->addressService->getFieldsForCountry($countryCode);
     if (!isset($fields[$this->fieldName]) || !$fields[$this->fieldName]) {
         return true;
     }
     return parent::isValid($value);
 }
Example #2
0
 public function hydrate(array $data, $object)
 {
     // Filters are not context aware, so we have to make this cleanup on
     // hydration. If anyone has a better idea, make a pull request!
     if (!isset($data['countryCode'])) {
         throw new RuntimeException('Missing countryCode in data');
     }
     $fields = $this->addressService->getFieldsForCountry($data['countryCode']);
     foreach ($data as $fieldName => $value) {
         if ($fieldName !== 'countryCode' && !isset($fields[$fieldName])) {
             $data[$fieldName] = null;
         }
     }
     return parent::hydrate($data, $object);
 }
Example #3
0
 /**
  * @covers ::getFieldsForCountry
  */
 public function testGetFieldsForCountryReturnsGenericFieldsOnUnknownCountryCode()
 {
     $addressService = new AddressService($this->options);
     $genericFields = ['recipient' => false, 'organization' => false, 'addressLine1' => true, 'addressLine2' => false, 'locality' => true];
     $this->assertSame($genericFields, $addressService->getFieldsForCountry('FOO'));
     $this->assertSame($genericFields, $addressService->getFieldsForCountry('UK'));
     $this->assertSame($genericFields, $addressService->getFieldsForCountry('DE'));
 }
 public function dispatch(ParametersInterface $parameters, Request $request)
 {
     return new JsonModel(['fields' => $this->addressService->getFieldsForCountry($parameters->get('countryCode'))]);
 }