Example #1
0
 /**
  * @covers ::getValueOptions
  */
 public function testAddressServiceIsOnlyAskedOnce()
 {
     $this->addressService->expects($this->once())->method('getCountryCodes')->will($this->returnValue([]));
     $countrySelect = new CountrySelect($this->addressService, $this->countryCodeFormatter);
     $countrySelect->getValueOptions();
     $countrySelect->getValueOptions();
 }
 /**
  * @covers ::isValid
  */
 public function testFailureOnRequiredEmptyField()
 {
     $this->addressService->expects($this->once())->method('getFieldsForCountry')->with($this->equalTo('bar'))->will($this->returnValue(['foo' => true]));
     $addressFieldValidator = new AddressFieldValidator($this->addressService, 'foo');
     $this->assertFalse($addressFieldValidator->isValid('', ['countryCode' => 'bar']));
     $this->assertArrayHasKey(AddressFieldValidator::IS_EMPTY, $addressFieldValidator->getMessages());
 }
Example #3
0
 /**
  * @covers ::__invoke
  */
 public function testInvokeFormatsForHtml()
 {
     $address = new Address();
     $this->addressService->expects($this->once())->method('formatAddress')->with($this->equalTo($address))->will($this->returnValue(['foo', '&bar']));
     $addressFormat = new AddressFormat($this->addressService);
     $addressFormat->setView(new PhpRenderer());
     $this->assertEquals("foo<br />\n&amp;bar", $addressFormat($address));
 }
Example #4
0
 /**
  * @covers ::__construct
  * @covers ::hydrate
  */
 public function testHydrateClearsNonRequiredFields()
 {
     $this->addressService->expects($this->once())->method('getFieldsForCountry')->with($this->equalTo('foo'))->will($this->returnValue(['addressLine1' => true, 'addressLine2' => false]));
     $hydrator = new AddressHydrator($this->addressService);
     $address = $hydrator->hydrate(['countryCode' => 'foo', 'addressLine1' => 'bar', 'locality' => 'baz'], new Address());
     $this->assertSame('foo', $address->getCountryCode());
     $this->assertSame('bar', $address->getAddressLine1());
     $this->assertNull($address->getAddressLine2());
     $this->assertNull($address->getLocality());
 }
 /**
  * @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 #6
0
 public function getValueOptions()
 {
     if ($this->valueOptions !== null) {
         return $this->valueOptions;
     }
     $countryCodes = $this->addressService->getCountryCodes();
     $valueOptions = [];
     foreach ($countryCodes as $countryCode) {
         $valueOptions[$countryCode] = $this->countryCodeFormatter->format($countryCode);
     }
     $this->setValueOptions($valueOptions);
     return $this->valueOptions;
 }
Example #7
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 #8
0
 /**
  * @param XMLWriter $xmlWriter
  * @param Invoice   $invoice
  */
 protected function addGenericData(XMLWriter $xmlWriter, Invoice $invoice)
 {
     $xmlWriter->writeElement('address', implode("\n", $this->addressService->formatAddress($invoice->getClient()->getAddress())));
     $xmlWriter->writeElement('invoice-id', $invoice->getInvoiceNumber());
     $xmlWriter->writeElement('issue-date', $this->dateFormatter->format($invoice->getIssueDate()));
     if ($invoice->getDueDate() !== null) {
         $xmlWriter->writeElement('due-date', $this->dateFormatter->format($invoice->getDueDate()));
     }
 }
Example #9
0
 /**
  * @covers ::getCountryCodes
  */
 public function testGetCountryCodesOnlyAccessesFilesystemOnce()
 {
     $addressService = new AddressService($this->options);
     $this->assertSame(['US', 'UK', 'DE', 'FR'], $addressService->getCountryCodes());
     $this->root->removeChild($this->root->url() . '/country-codes.php');
     $this->assertSame(['US', 'UK', 'DE', 'FR'], $addressService->getCountryCodes());
 }
 public function dispatch(ParametersInterface $parameters, Request $request)
 {
     return new JsonModel(['fields' => $this->addressService->getFieldsForCountry($parameters->get('countryCode'))]);
 }
Example #11
0
 /**
  * @param  Address $address
  * @return string
  */
 public function __invoke(Address $address)
 {
     return nl2br($this->getView()->escapeHtml(implode("\n", $this->addressService->formatAddress($address))));
 }