/** * 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)); }
/** * Formats date time according to locale settings. * * Options format: * array( * 'dateType' => <dateType>, * 'timeType' => <timeType>, * 'locale' => <locale>, * 'timezone' => <timezone>, * ) * * @param \DateTime|string|int $date * @param array $options * @return string */ public function formatDateTime($date, array $options = array()) { $dateType = $this->getOption($options, 'dateType'); $timeType = $this->getOption($options, 'timeType'); $locale = $this->getOption($options, 'locale'); $timeZone = $this->getOption($options, 'timeZone'); return $this->formatter->format($date, $dateType, $timeType, $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())); }
/** * @param CaseComment $comment * @return array */ public function createCommentView(CaseComment $comment) { $result = ['id' => $comment->getId(), 'message' => nl2br(htmlspecialchars($comment->getMessage())), 'briefMessage' => htmlspecialchars(mb_substr(preg_replace('/[\\n\\r]+/', ' ', $comment->getMessage()), 0, 200)), 'public' => $comment->isPublic(), 'createdAt' => $comment->getCreatedAt() ? $this->dateTimeFormatter->format($comment->getCreatedAt()) : null, 'updatedAt' => $comment->getUpdatedAt() ? $this->dateTimeFormatter->format($comment->getUpdatedAt()) : null, 'permissions' => array('edit' => $this->securityFacade->isGranted('EDIT', $comment), 'delete' => $this->securityFacade->isGranted('DELETE', $comment))]; if ($comment->getContact()) { $result['createdBy'] = $this->createAuthorView($comment->getContact()); } elseif ($comment->getOwner()) { $result['createdBy'] = $this->createAuthorView($comment->getOwner()); } if ($comment->getUpdatedBy()) { $result['updatedBy'] = $this->createAuthorView($comment->getUpdatedBy()); } return $result; }
/** * @dataProvider formatDataProvider */ public function testFormat($expected, $date, $dateType, $timeType, $locale, $timeZone, $language, $defaultLocale = null, $defaultTimeZone = null) { $this->localeSettings->expects($this->once())->method('getLanguage')->will($this->returnValue($language)); $methodCalls = 1; if ($defaultLocale) { $methodCalls++; $this->localeSettings->expects($this->once())->method('getLocale')->will($this->returnValue($defaultLocale)); } if ($defaultTimeZone) { $methodCalls++; $this->localeSettings->expects($this->once())->method('getTimeZone')->will($this->returnValue($defaultTimeZone)); } $this->localeSettings->expects($this->exactly($methodCalls))->method($this->anything()); $this->assertEquals($expected, $this->formatter->format($date, $dateType, $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; }
/** * @param string $value * @param array $options * @return string */ protected function applyFrontendFormatting($value, array $options) { $frontendType = isset($options['frontend_type']) ? $options['frontend_type'] : null; switch ($frontendType) { case PropertyInterface::TYPE_DATE: $value = $this->dateTimeFormatter->formatDate($value); break; case PropertyInterface::TYPE_DATETIME: $value = $this->dateTimeFormatter->format($value); break; case PropertyInterface::TYPE_DECIMAL: $value = $this->numberFormatter->formatDecimal($value); break; case PropertyInterface::TYPE_INTEGER: $value = $this->numberFormatter->formatDecimal($value); break; case PropertyInterface::TYPE_BOOLEAN: $value = $this->translator->trans((bool) $value ? self::YES_LABEL_KEY : self::NO_LABEL_KEY); break; case PropertyInterface::TYPE_PERCENT: $value = $this->numberFormatter->formatPercent($value); break; case PropertyInterface::TYPE_CURRENCY: $value = $this->numberFormatter->formatCurrency($value); break; case PropertyInterface::TYPE_SELECT: if (isset($options['choices'][$value])) { $value = $this->translator->trans($options['choices'][$value]); } break; } return $value; }
/** * @SuppressWarnings(PHPMD.ExcessiveParameterList) * @dataProvider formatDataProvider */ public function testFormat($expectedDateType, $expectedTimeType, \DateTime $expectedDate, $date, $dateType, $timeType, $locale, $timeZone, $language, $defaultLocale = null, $defaultTimeZone = null) { $this->localeSettings->expects($this->once())->method('getLanguage')->will($this->returnValue($language)); $methodCalls = 1; if ($defaultLocale) { $methodCalls++; $this->localeSettings->expects($this->once())->method('getLocale')->will($this->returnValue($defaultLocale)); } if ($defaultTimeZone) { $methodCalls++; $this->localeSettings->expects($this->once())->method('getTimeZone')->will($this->returnValue($defaultTimeZone)); } $this->localeSettings->expects($this->exactly($methodCalls))->method($this->anything()); $pattern = $this->getPattern($locale ?: $defaultLocale, $expectedDateType, $expectedTimeType); $formatter = $this->getFormatter($language, $timeZone ?: $defaultTimeZone, $pattern); $expected = $formatter->format((int) $expectedDate->format('U')); $this->assertEquals($expected, $this->formatter->format($date, $dateType, $timeType, $locale, $timeZone)); }
/** * @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)); } }
/** * @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; }
/** * {@inheritdoc} */ public function format($parameter, array $formatterArguments = []) { return $this->formatter->format($parameter); }