/**
  * Test convert date values to localized values
  */
 public function testConvertDateToLocalizedValues()
 {
     $this->setCustomLocale('ru_RU');
     $this->assertEquals('21 мая 2014 г.', LocaleUtility::convertToLocalizedValue('2014-05-21', 'date', IntlDateFormatter::MEDIUM));
     $this->setCustomLocale('en_US');
     $this->assertEquals('Mar 16, 2012', LocaleUtility::convertToLocalizedValue('2012-03-16', 'date', IntlDateFormatter::MEDIUM));
     $this->setCustomLocale('fr_FR');
     $this->assertEquals('16 mars 2012', LocaleUtility::convertToLocalizedValue('2012-03-16', 'date', IntlDateFormatter::MEDIUM));
 }
Exemplo n.º 2
0
 /**
  * Get date
  *
  * @param string|integer $date
  * @param array $options
  *      string  type (date or date_unixtime)
  *      string format (full, long, medium, short)
  * @param string $locale
  * @return string
  */
 public function __invoke($date, array $options = [], $locale = null)
 {
     $type = !empty($options['type']) && $options['type'] == 'date' ? 'date' : 'date_unixtime';
     if ($type == 'date_unixtime' && !(int) $date) {
         return;
     }
     $format = isset($options['format']) ? $options['format'] : SettingService::getSetting('application_default_date_format');
     $format = strtolower($format);
     switch ($format) {
         case 'full':
             $format = IntlDateFormatter::FULL;
             break;
         case 'long':
             $format = IntlDateFormatter::LONG;
             break;
         case 'medium':
             $format = IntlDateFormatter::MEDIUM;
             break;
         case 'short':
         default:
             $format = IntlDateFormatter::SHORT;
     }
     return LocaleUtility::convertToLocalizedValue($date, $type, $format, $locale);
 }
 /**
  * Set data to validate and/or populate elements
  *
  * Typically, also passes data on to the composed input filter.
  *
  * @param  array|\ArrayAccess|Traversable $data
  * @param boolean $convertValues
  * @return Form|FormInterface
  * @throws Zend\Form\Exception\InvalidArgumentException
  */
 public function setData($data, $convertValues = true)
 {
     // unset not validated fields
     foreach ($this->notValidatedElements as $name) {
         if (isset($data[$name])) {
             unset($data[$name]);
         }
     }
     // convert localized values
     if ($convertValues) {
         foreach ($data as $fieldName => $fieldValue) {
             if (!isset($this->customElements[$fieldName])) {
                 continue;
             }
             $data[$fieldName] = LocaleUtility::convertToLocalizedValue($fieldValue, $this->customElements[$fieldName]);
         }
     }
     return parent::setData($data);
 }