Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/4137' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 224 deletions.
4 changes: 2 additions & 2 deletions src/View/Helper/AbstractTranslatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function setTranslator(Translator $translator = null, $textDomain = null)
if (null !== $textDomain) {
$this->setTranslatorTextDomain($textDomain);
}

return $this;
}

Expand Down Expand Up @@ -82,8 +83,7 @@ public function hasTranslator()
/**
* Sets whether translator is enabled and should be used
*
* @param bool $enabled [optional] whether translator should be used.
* Default is true.
* @param bool $enabled
* @return AbstractTranslatorHelper
*/
public function setTranslatorEnabled($enabled = true)
Expand Down
144 changes: 72 additions & 72 deletions src/View/Helper/CurrencyFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@
class CurrencyFormat extends AbstractHelper
{
/**
* Locale to use instead of the default.
* The 3-letter ISO 4217 currency code indicating the currency to use
*
* @var string
*/
protected $locale;
protected $currencyCode;

/**
* Formatter instances
*
* @var array
*/
protected $formatters = array();

/**
* The 3-letter ISO 4217 currency code indicating the currency to use.
* Locale to use instead of the default
*
* @var string
*/
protected $currencyCode;
protected $locale;

/**
* If set to true, the currency will be returned with two decimals
Expand All @@ -40,14 +47,52 @@ class CurrencyFormat extends AbstractHelper
protected $showDecimals = true;

/**
* Formatter instances.
* Format a number
*
* @var array
* @param float $number
* @param string $currencyCode
* @param bool $showDecimals
* @param string $locale
* @return string
*/
protected $formatters = array();
public function __invoke(
$number,
$currencyCode = null,
$showDecimals = null,
$locale = null
) {
if (null === $locale) {
$locale = $this->getLocale();
}
if (null === $currencyCode) {
$currencyCode = $this->getCurrencyCode();
}
if (null !== $showDecimals) {
$this->setShouldShowDecimals($showDecimals);
}

$formatterId = md5($locale);

if (!isset($this->formatters[$formatterId])) {
$this->formatters[$formatterId] = new NumberFormatter(
$locale,
NumberFormatter::CURRENCY
);
}

if ($this->shouldShowDecimals()) {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
} else {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
}

return $this->formatters[$formatterId]->formatCurrency(
$number, $currencyCode
);
}

/**
* The 3-letter ISO 4217 currency code indicating the currency to use.
* The 3-letter ISO 4217 currency code indicating the currency to use
*
* @param string $currencyCode
* @return CurrencyFormat
Expand All @@ -59,7 +104,7 @@ public function setCurrencyCode($currencyCode)
}

/**
* Get the 3-letter ISO 4217 currency code indicating the currency to use.
* Get the 3-letter ISO 4217 currency code indicating the currency to use
*
* @return string
*/
Expand All @@ -69,29 +114,7 @@ public function getCurrencyCode()
}

/**
* Set if the view helper should show two decimals
*
* @param bool $showDecimals
* @return CurrencyFormat
*/
public function setShouldShowDecimals($showDecimals)
{
$this->showDecimals = (bool) $showDecimals;
return $this;
}

/**
* Get if the view helper should show two decimals
*
* @return bool
*/
public function shouldShowDecimals()
{
return $this->showDecimals;
}

/**
* Set locale to use instead of the default.
* Set locale to use instead of the default
*
* @param string $locale
* @return CurrencyFormat
Expand All @@ -103,7 +126,7 @@ public function setLocale($locale)
}

/**
* Get the locale to use.
* Get the locale to use
*
* @return string|null
*/
Expand All @@ -117,47 +140,24 @@ public function getLocale()
}

/**
* Format a number.
* Set if the view helper should show two decimals
*
* @param float $number
* @param string $currencyCode
* @param bool $showDecimals
* @param string $locale
* @return string
* @param bool $showDecimals
* @return CurrencyFormat
*/
public function __invoke(
$number,
$currencyCode = null,
$showDecimals = null,
$locale = null
) {
if (null === $locale) {
$locale = $this->getLocale();
}
if (null === $currencyCode) {
$currencyCode = $this->getCurrencyCode();
}
if (null !== $showDecimals) {
$this->setShouldShowDecimals($showDecimals);
}

$formatterId = md5($locale);

if (!isset($this->formatters[$formatterId])) {
$this->formatters[$formatterId] = new NumberFormatter(
$locale,
NumberFormatter::CURRENCY
);
}

if ($this->shouldShowDecimals()) {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
} else {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
}
public function setShouldShowDecimals($showDecimals)
{
$this->showDecimals = (bool) $showDecimals;
return $this;
}

return $this->formatters[$formatterId]->formatCurrency(
$number, $currencyCode
);
/**
* Get if the view helper should show two decimals
*
* @return bool
*/
public function shouldShowDecimals()
{
return $this->showDecimals;
}
}
117 changes: 59 additions & 58 deletions src/View/Helper/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,71 @@
class DateFormat extends AbstractHelper
{
/**
* Locale to use instead of the default.
* Locale to use instead of the default
*
* @var string
*/
protected $locale;

/**
* Timezone to use.
* Timezone to use
*
* @var string
*/
protected $timezone;

/**
* Formatter instances.
* Formatter instances
*
* @var array
*/
protected $formatters = array();

/**
* Set timezone to use instead of the default.
* Format a date
*
* @param string $timezone
* @return DateFormat
* @param DateTime|integer|array $date
* @param int $dateType
* @param int $timeType
* @param string $locale
* @param string|null $pattern
* @return string
*/
public function setTimezone($timezone)
{
$this->timezone = (string) $timezone;
public function __invoke(
$date,
$dateType = IntlDateFormatter::NONE,
$timeType = IntlDateFormatter::NONE,
$locale = null,
$pattern = null
) {
if ($locale === null) {
$locale = $this->getLocale();
}

foreach ($this->formatters as $formatter) {
$formatter->setTimeZoneId($this->timezone);
$timezone = $this->getTimezone();
$formatterId = md5($dateType . "\0" . $timeType . "\0" . $locale ."\0" . $pattern);

if (!isset($this->formatters[$formatterId])) {
$this->formatters[$formatterId] = new IntlDateFormatter(
$locale,
$dateType,
$timeType,
$timezone,
IntlDateFormatter::GREGORIAN,
$pattern
);
}
return $this;
}

/**
* Get the timezone to use.
*
* @return string|null
*/
public function getTimezone()
{
if (!$this->timezone) {
return date_default_timezone_get();
// DateTime support for IntlDateFormatter::format() was only added in 5.3.4
if ($date instanceof DateTime && version_compare(PHP_VERSION, '5.3.4', '<')) {
$date = $date->getTimestamp();
}

return $this->timezone;
return $this->formatters[$formatterId]->format($date);
}

/**
* Set locale to use instead of the default.
* Set locale to use instead of the default
*
* @param string $locale
* @return DateFormat
Expand All @@ -84,7 +97,7 @@ public function setLocale($locale)
}

/**
* Get the locale to use.
* Get the locale to use
*
* @return string|null
*/
Expand All @@ -98,45 +111,33 @@ public function getLocale()
}

/**
* Format a date.
* Set timezone to use instead of the default
*
* @param DateTime|integer|array $date
* @param int $dateType
* @param int $timeType
* @param string $locale
* @param string|null $pattern
* @return string
* @param string $timezone
* @return DateFormat
*/
public function __invoke(
$date,
$dateType = IntlDateFormatter::NONE,
$timeType = IntlDateFormatter::NONE,
$locale = null,
$pattern = null
) {
if ($locale === null) {
$locale = $this->getLocale();
}

$timezone = $this->getTimezone();
$formatterId = md5($dateType . "\0" . $timeType . "\0" . $locale ."\0" . $pattern);
public function setTimezone($timezone)
{
$this->timezone = (string) $timezone;

if (!isset($this->formatters[$formatterId])) {
$this->formatters[$formatterId] = new IntlDateFormatter(
$locale,
$dateType,
$timeType,
$timezone,
IntlDateFormatter::GREGORIAN,
$pattern
);
foreach ($this->formatters as $formatter) {
$formatter->setTimeZoneId($this->timezone);
}

// DateTime support for IntlDateFormatter::format() was only added in 5.3.4
if ($date instanceof DateTime && version_compare(PHP_VERSION, '5.3.4', '<')) {
$date = $date->getTimestamp();
return $this;
}

/**
* Get the timezone to use
*
* @return string|null
*/
public function getTimezone()
{
if (!$this->timezone) {
return date_default_timezone_get();
}

return $this->formatters[$formatterId]->format($date);
return $this->timezone;
}
}
Loading

0 comments on commit d3afda7

Please sign in to comment.