/** * Returns a string represents a range between $startDate and $endDate, formatted according the given parameters * Examples: * $endDate is not specified * Thu Oct 17, 2013 - when $skipTime = true * Thu Oct 17, 2013 5:30pm - when $skipTime = false * $startDate equals to $endDate * Thu Oct 17, 2013 - when $skipTime = true * Thu Oct 17, 2013 5:30pm - when $skipTime = false * $startDate and $endDate are the same day * Thu Oct 17, 2013 - when $skipTime = true * Thu Oct 17, 2013 5:00pm – 5:30pm - when $skipTime = false * $startDate and $endDate are different days * Thu Oct 17, 2013 5:00pm – Thu Oct 18, 2013 5:00pm - when $skipTime = false * Thu Oct 17, 2013 – Thu Oct 18, 2013 - when $skipTime = true * * @param \DateTime|null $startDate * @param \DateTime|null $endDate * @param bool $skipTime * @param string|null $dateTimeFormat * @param string|int|null $dateType \IntlDateFormatter constant or it's string name * @param string|int|null $timeType \IntlDateFormatter constant or it's string name * @param string|null $locale * @param string|null $timeZone * * @return string */ public function formatCalendarDateRange(\DateTime $startDate = null, \DateTime $endDate = null, $skipTime = false, $dateTimeFormat = null, $dateType = null, $timeType = null, $locale = null, $timeZone = null) { if (is_null($startDate)) { // exit because nothing to format. // We have to accept null as $startDate because the validator of email templates calls functions // with empty arguments return ''; } // check if $endDate is not specified or $startDate equals to $endDate if (is_null($endDate) || $startDate == $endDate) { return $skipTime ? $this->formatter->formatDate($startDate, $dateType, $locale, $timeZone) : $this->formatter->format($startDate, $dateType, $timeType, $locale, $timeZone); } // check if $startDate and $endDate are the same day if ($startDate->format('Ymd') == $endDate->format('Ymd')) { if ($skipTime) { return $this->formatter->formatDate($startDate, $dateType, $locale, $timeZone); } return sprintf('%s %s - %s', $this->formatter->formatDate($startDate, $dateType, $locale, $timeZone), $this->formatter->formatTime($startDate, $timeType, $locale, $timeZone), $this->formatter->formatTime($endDate, $timeType, $locale, $timeZone)); } // $startDate and $endDate are different days if ($skipTime) { return sprintf('%s - %s', $this->formatter->formatDate($startDate, $dateType, $locale, $timeZone), $this->formatter->formatDate($endDate, $dateType, $locale, $timeZone)); } return sprintf('%s - %s', $this->formatter->format($startDate, $dateTimeFormat, $locale, $timeZone), $this->formatter->format($endDate, $dateTimeFormat, $locale, $timeZone)); }
/** * @param Reminder $reminder * @return array */ public function getMessageParams(Reminder $reminder) { $now = new \DateTime(); $expiredDate = $this->dateTimeFormatter->formatDate($reminder->getExpireAt(), \IntlDateFormatter::SHORT); $nowDate = $this->dateTimeFormatter->formatDate($now, \IntlDateFormatter::SHORT); if ($expiredDate === $nowDate) { $expireAt = $this->dateTimeFormatter->formatTime($reminder->getExpireAt()); } else { $expireAt = $this->dateTimeFormatter->format($reminder->getExpireAt()); } return array('templateId' => $this->getTemplateId($reminder), 'expireAt' => $expireAt, 'subject' => $reminder->getSubject(), 'url' => $this->urlProvider->getUrl($reminder), 'id' => $reminder->getId(), 'uniqueId' => md5($reminder->getRelatedEntityClassName() . $reminder->getRelatedEntityId())); }
/** * @dataProvider formatTimeDataProvider */ public function testFormatTime($expected, $date, $timeType, $locale, $timeZone, $language, $defaultLocale = null, $defaultTimeZone = null) { $this->localeSettings->expects($this->once())->method('getLanguage')->will($this->returnValue($language)); $methodCalls = 1; if ($defaultLocale) { $this->localeSettings->expects($this->once())->method('getLocale')->will($this->returnValue($defaultLocale)); $methodCalls++; } if ($defaultTimeZone) { $this->localeSettings->expects($this->once())->method('getTimeZone')->will($this->returnValue($defaultTimeZone)); $methodCalls++; } $this->localeSettings->expects($this->exactly($methodCalls))->method($this->anything()); $this->assertEquals($expected, $this->formatter->formatTime($date, $timeType, $locale, $timeZone)); }
/** * Apply formatting to totals values * * @param mixed|null $val * @param string|null $formatter * @return string|null */ protected function applyFrontendFormatting($val = null, $formatter = null) { if (null != $formatter) { switch ($formatter) { case PropertyInterface::TYPE_DATE: $val = $this->dateTimeFormatter->formatDate($val); break; case PropertyInterface::TYPE_DATETIME: $val = $this->dateTimeFormatter->format($val); break; case PropertyInterface::TYPE_TIME: $val = $this->dateTimeFormatter->formatTime($val); break; case PropertyInterface::TYPE_DECIMAL: $val = $this->numberFormatter->formatDecimal($val); break; case PropertyInterface::TYPE_INTEGER: $val = $this->numberFormatter->formatDecimal($val); break; case PropertyInterface::TYPE_PERCENT: $val = $this->numberFormatter->formatPercent($val); break; case PropertyInterface::TYPE_CURRENCY: $val = $this->numberFormatter->formatCurrency($val); break; } } return $val; }
/** * Formats date time according to locale settings. * * Options format: * array( * 'timeType' => <timeType>, * 'locale' => <locale>, * 'timeZone' => <timeZone>, * ) * * @param \DateTime|string|int $date * @param array $options * @return string */ public function formatTime($date, array $options = array()) { $timeType = $this->getOption($options, 'timeType'); $locale = $this->getOption($options, 'locale'); $timeZone = $this->getOption($options, 'timeZone'); return $this->formatter->formatTime($date, $timeType, $locale, $timeZone); }
/** * @dataProvider formatTimeDataProvider */ public function testFormatTime($expectedTimeType, \DateTime $date, $timeType, $locale, $timeZone, $language, $defaultLocale = null, $defaultTimeZone = null) { $this->localeSettings->expects($this->once())->method('getLanguage')->will($this->returnValue($language)); $methodCalls = 1; if ($defaultLocale) { $this->localeSettings->expects($this->once())->method('getLocale')->will($this->returnValue($defaultLocale)); $methodCalls++; } if ($defaultTimeZone) { $this->localeSettings->expects($this->once())->method('getTimeZone')->will($this->returnValue($defaultTimeZone)); $methodCalls++; } $this->localeSettings->expects($this->exactly($methodCalls))->method($this->anything()); $pattern = $this->getPattern($locale ?: $defaultLocale, \IntlDateFormatter::NONE, $expectedTimeType); $formatter = $this->getFormatter($language, $timeZone ?: $defaultTimeZone, $pattern); $expected = $formatter->format((int) $date->format('U')); $this->assertEquals($expected, $this->formatter->formatTime($date, $timeType, $locale, $timeZone)); }
/** * @param array $result * @param bool $addValue */ protected function addCurrentDateAndTime(array &$result, $addValue) { if ($addValue) { $dateTime = new \DateTime('now', new \DateTimeZone('UTC')); $dateTimeVal = $dateTime; $dateVal = $this->dateTimeFormatter->formatDate($dateTime); $timeVal = $this->dateTimeFormatter->formatTime($dateTime); } else { $dateTimeVal = ['type' => 'datetime', 'label' => $this->translator->trans('oro.email.emailtemplate.current_datetime')]; $dateVal = ['type' => 'string', 'label' => $this->translator->trans('oro.email.emailtemplate.current_date')]; $timeVal = ['type' => 'string', 'label' => $this->translator->trans('oro.email.emailtemplate.current_time')]; } // @todo: the datetime object cannot be added due __toString of DateTime is not allowed error // this code can be uncommented after validation and formatting are fixed //$result['currentDateTime'] = $dateTimeVal; $result['currentDate'] = $dateVal; $result['currentTime'] = $timeVal; }
/** * @param mixed $val * @param array $options * * @return string|null * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function applyFrontendFormatting($val, $options) { if (null !== $val) { $frontendType = isset($options['frontend_type']) ? $options['frontend_type'] : null; switch ($frontendType) { case PropertyInterface::TYPE_DATE: $val = $this->dateTimeFormatter->formatDate($val); break; case PropertyInterface::TYPE_DATETIME: $val = $this->dateTimeFormatter->format($val); break; case PropertyInterface::TYPE_TIME: $val = $this->dateTimeFormatter->formatTime($val); break; case PropertyInterface::TYPE_DECIMAL: $val = $this->numberFormatter->formatDecimal($val); break; case PropertyInterface::TYPE_INTEGER: $val = $this->numberFormatter->formatDecimal($val); break; case PropertyInterface::TYPE_BOOLEAN: $val = $this->translator->trans((bool) $val ? 'Yes' : 'No', [], 'jsmessages'); break; case PropertyInterface::TYPE_PERCENT: $val = $this->numberFormatter->formatPercent($val); break; case PropertyInterface::TYPE_CURRENCY: $val = $this->numberFormatter->formatCurrency($val); break; case PropertyInterface::TYPE_SELECT: if (isset($options['choices'][$val])) { $val = $this->translator->trans($options['choices'][$val]); } break; case PropertyInterface::TYPE_HTML: $val = $this->formatHtmlFrontendType($val, isset($options['export_type']) ? $options['export_type'] : null); break; } } return $val; }