/** * @dataProvider formatDataProvider * @param string $format * @param string $expected * @param bool $formatByCountry */ public function testFormat($format, $expected, $formatByCountry = false) { $address = new AddressStub(); $locale = 'en'; $country = 'CA'; $addressFormats = array($country => array(LocaleSettings::ADDRESS_FORMAT_KEY => $format)); $this->localeSettings->expects($this->once())->method('getAddressFormats')->will($this->returnValue($addressFormats)); $this->localeSettings->expects($this->once())->method('isFormatAddressByAddressCountry')->will($this->returnValue($formatByCountry)); $this->localeSettings->expects($this->once())->method('getCountry')->will($this->returnValue($country)); if ($formatByCountry) { $this->localeSettings->expects($this->once())->method('getLocaleByCountry')->with($address->getCountryIso2())->will($this->returnValue($locale)); } else { $this->localeSettings->expects($this->once())->method('getLocaleByCountry')->with($country)->will($this->returnValue($locale)); } $this->nameFormatter->expects($this->once())->method('format')->with($address, $locale)->will($this->returnValue('Formatted User NAME')); $this->assertEquals($expected, $this->addressFormatter->format($address)); }
/** * @dataProvider formatDataProvider * @param string $format * @param string $regionCode * @param string $expected * @param bool $formatByCountry * @param string $street2 * @param string|null $separator */ public function testFormat($format, $regionCode, $expected, $formatByCountry = false, $street2 = 'apartment 10', $separator = "\n") { $address = new AddressStub($street2); $address->setRegionCode($regionCode); $locale = 'en'; $country = 'CA'; $addressFormats = [$country => [LocaleSettings::ADDRESS_FORMAT_KEY => $format]]; $this->localeSettings->expects($this->once())->method('getAddressFormats')->will($this->returnValue($addressFormats)); $this->localeSettings->expects($this->once())->method('isFormatAddressByAddressCountry')->will($this->returnValue($formatByCountry)); $this->localeSettings->expects($this->once())->method('getCountry')->will($this->returnValue($country)); if ($formatByCountry) { $this->localeSettings->expects($this->once())->method('getLocaleByCountry')->with($address->getCountryIso2())->will($this->returnValue($locale)); } else { $this->localeSettings->expects($this->once())->method('getLocaleByCountry')->with($country)->will($this->returnValue($locale)); } $this->nameFormatter->expects($this->once())->method('format')->with($address, $locale)->will($this->returnValue('Formatted User NAME')); $this->assertEquals($expected, $this->addressFormatter->format($address, null, $separator)); }
/** * @param array $addresses * * @return array */ protected function getChoices(array $addresses = []) { array_walk_recursive($addresses, function (&$item) { if ($item instanceof AbstractAddress) { $item = $this->addressFormatter->format($item, null, ', '); } return $item; }); return $addresses; }
/** * @param ValueRenderEvent $fieldValueEvent */ public function beforeValueRender(ValueRenderEvent $fieldValueEvent) { $originalValue = $fieldValueEvent->getOriginalValue(); $metadata = $fieldValueEvent->getMetadata(); if ($originalValue instanceof AddressInterface) { $fieldValueEvent->setConvertedValue($this->addressFormatter->format($originalValue)); } elseif ($originalValue instanceof NamePrefixInterface || $originalValue instanceof FirstNameInterface || $originalValue instanceof MiddleNameInterface || $originalValue instanceof LastNameInterface || $originalValue instanceof NameSuffixInterface) { $fieldValueEvent->setConvertedValue($this->nameFormatter->format($originalValue)); } elseif ($originalValue instanceof \DateTime) { $dateType = $metadata->get('render_date_type'); $timeType = $metadata->get('render_time_type'); $dateTimePattern = $metadata->get('render_datetime_pattern'); $fieldValueEvent->setConvertedValue($this->dateTimeFormatter->format($originalValue, $dateType, $timeType, null, null, $dateTimePattern)); } elseif (is_numeric($originalValue)) { $numberStyle = $metadata->get('render_number_style'); if (!$numberStyle) { $numberStyle = 'default_style'; } $fieldValueEvent->setConvertedValue($this->numberFormatter->format($originalValue, $numberStyle)); } }
/** * Formats address according to locale settings. * * @param AddressInterface $address * @param string|null $country * @param string $newLineSeparator * @return string */ public function format(AddressInterface $address, $country = null, $newLineSeparator = "\n") { return $this->formatter->format($address, $country, $newLineSeparator); }