Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to set the minimum number of decimals in the numb… #113

Open
wants to merge 14 commits into
base: 2.24.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<directory name="test" />
</errorLevel>
</DeprecatedMethod>
<PossiblyUnusedMethod>
<errorLevel type="suppress">
<directory name="test" />
</errorLevel>
</PossiblyUnusedMethod>
</issueHandlers>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
Expand Down
111 changes: 88 additions & 23 deletions src/View/Helper/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@
use function serialize;

/**
* View helper for formatting dates.
* View helper for formatting numbers.
*/
class NumberFormat extends AbstractHelper
{
use DeprecatedAbstractHelperHierarchyTrait;

/**
* number of decimals to use.
* The maximum number of decimals to use.
*
* @var int
*/
protected $decimals;
protected $maxDecimals;

/**
* The minimum number of decimals to use.
*
* @var int
*/
protected $minDecimals;

/**
* NumberFormat style to use
Expand All @@ -50,7 +57,7 @@ class NumberFormat extends AbstractHelper
/**
* Text attributes.
*
* @var array
* @var array<int, string>
*/
protected $textAttributes = [];

Expand All @@ -64,21 +71,24 @@ class NumberFormat extends AbstractHelper
/**
* Format a number
*
* @param int|float $number
* @param int|null $formatStyle
* @param int|null $formatType
* @param string|null $locale
* @param int|null $decimals
* @param array|null $textAttributes
* @param int|float $number
* @param int|null $formatStyle
* @param int|null $formatType
* @param string|null $locale
* @param int|null $maxDecimals
* @param array<int, string> $textAttributes
* @param int|float $number
* @param int|null $minDecimals
* @return string
*/
public function __invoke(
$number,
$formatStyle = null,
$formatType = null,
$locale = null,
$decimals = null,
?array $textAttributes = null
$maxDecimals = null,
?array $textAttributes = null,
$minDecimals = null
) {
if (null === $locale) {
$locale = $this->getLocale();
Expand All @@ -89,15 +99,22 @@ public function __invoke(
if (null === $formatType) {
$formatType = $this->getFormatType();
}
if (! is_int($decimals) || $decimals < 0) {
$decimals = $this->getDecimals();
if (! is_int($minDecimals) || $minDecimals < 0) {
$minDecimals = $this->getMinDecimals();
}
if (! is_int($maxDecimals) || $maxDecimals < 0) {
$maxDecimals = $this->getMaxDecimals();
}
if (($maxDecimals !== null) && ($minDecimals === null)) {
// Fallback to old behavior
$minDecimals = $maxDecimals;
}
if (! is_array($textAttributes)) {
$textAttributes = $this->getTextAttributes();
}

$formatterId = md5(
$formatStyle . "\0" . $locale . "\0" . $decimals . "\0"
$formatStyle . "\0" . $locale . "\0" . ($minDecimals ?? '') . "\0" . ($maxDecimals ?? '') . "\0"
. md5(serialize($textAttributes))
);

Expand All @@ -106,9 +123,12 @@ public function __invoke(
} else {
$formatter = new NumberFormatter($locale, $formatStyle);

if ($decimals !== null) {
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimals);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
if ($minDecimals !== null) {
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $minDecimals);
}

if ($maxDecimals !== null) {
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxDecimals);
}

foreach ($textAttributes as $textAttribute => $value) {
Expand Down Expand Up @@ -173,14 +193,39 @@ public function getFormatType()
}

/**
* Set number of decimals to use instead of the default.
* Set number of decimals (both min & max) to use instead of the default.
*
* @param int $decimals
* @return $this
*/
public function setDecimals($decimals)
{
$this->decimals = $decimals;
$this->minDecimals = $decimals;
$this->maxDecimals = $decimals;
return $this;
}

/**
* Set the maximum number of decimals to use instead of the default.
*
* @param int $maxDecimals
* @return $this
*/
public function setMaxDecimals($maxDecimals)
{
$this->maxDecimals = $maxDecimals;
return $this;
}

/**
* Set the minimum number of decimals to use instead of the default.
*
* @param int $minDecimals
* @return $this
*/
public function setMinDecimals($minDecimals)
{
$this->minDecimals = $minDecimals;
return $this;
}

Expand All @@ -191,7 +236,27 @@ public function setDecimals($decimals)
*/
public function getDecimals()
{
return $this->decimals;
return $this->maxDecimals;
}

/**
* Get the maximum number of decimals.
*
* @return int
*/
public function getMaxDecimals()
{
return $this->maxDecimals;
}

/**
* Get the minimum number of decimals.
*
* @return int
*/
public function getMinDecimals()
{
return $this->minDecimals;
}

/**
Expand Down Expand Up @@ -221,15 +286,15 @@ public function getLocale()
}

/**
* @return array
* @return array<int, string>
*/
public function getTextAttributes()
{
return $this->textAttributes;
}

/**
* @param array $textAttributes
* @param array<int, string> $textAttributes
* @return $this
*/
public function setTextAttributes(array $textAttributes)
Expand Down
2 changes: 1 addition & 1 deletion src/View/HelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @method string countryCodeDataList(?string $locale = null, array $dataListAttributes = [])
* @method string currencyFormat(float $number, string|null $currencyCode = null, bool|null $showDecimals = null, string|null $locale = null, string|null $pattern = null)
* @method string dateFormat(\DateTimeInterface|\IntlCalendar|int|array $date, int $dateType = IntlDateFormatter::NONE, int $timeType = IntlDateFormatter::NONE, string|null $locale = null, string|null $pattern = null)
* @method string numberFormat(int|float $number, int|null $formatStyle = null, int|null $formatType = null, string|null $locale = null, int|null $decimals = null, array|null $textAttributes = null)
* @method string numberFormat(int|float $number, int|null $formatStyle = null, int|null $formatType = null, string|null $locale = null, int|null $maxDecimals = null, array|null $textAttributes = null, int|null $minDecimals = null)
* @method string plural(array|string $strings, int $number)
* @method string translate(string $message, string|null $textDomain = null, string|null $locale = null)
* @method string translatePlural(string $singular, string $plural, int $number, string|null $textDomain = null, string|null $locale = null)
Expand Down
Loading