/**
  * @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;
 }
 /**
  * @dataProvider getAddressFormatDataProvider
  *
  * @param array $addressFormats
  * @param string $localeOrRegion
  * @param string $expectedFormat
  * @param string $defaultCountry
  */
 public function testGetAddressFormat(array $addressFormats, $localeOrRegion, $expectedFormat, $defaultCountry = null)
 {
     $this->localeSettings->expects($this->once())->method('getAddressFormats')->will($this->returnValue($addressFormats));
     if (!$localeOrRegion) {
         $this->localeSettings->expects($this->once())->method('getLocale')->will($this->returnValue('en_US'));
     }
     if ($defaultCountry) {
         $this->localeSettings->expects($this->once())->method('getCountry')->will($this->returnValue($defaultCountry));
     }
     $this->assertEquals($expectedFormat, $this->addressFormatter->getAddressFormat($localeOrRegion));
 }
 /**
  * @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));
     }
 }
Example #4
0
 /**
  * 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);
 }