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.
MatthiasKuehneEllerhold marked this conversation as resolved.
Show resolved Hide resolved
*/
class NumberFormat extends AbstractHelper
{
use DeprecatedAbstractHelperHierarchyTrait;

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

Check failure on line 27 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingConstructor

src/View/Helper/NumberFormat.php:27:15: MissingConstructor: Laminas\I18n\View\Helper\NumberFormat has an uninitialized property Laminas\I18n\View\Helper\NumberFormat::$maxDecimals, but no constructor (see https://psalm.dev/073)

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

Check failure on line 34 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingConstructor

src/View/Helper/NumberFormat.php:34:15: MissingConstructor: Laminas\I18n\View\Helper\NumberFormat has an uninitialized property Laminas\I18n\View\Helper\NumberFormat::$minDecimals, but no constructor (see https://psalm.dev/073)

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

Expand All @@ -64,21 +71,24 @@
/**
* 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

Check failure on line 81 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPCodeSniffer [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Superfluous parameter comment
* @return string
*/
public function __invoke(

Check failure on line 84 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

InvalidDocblock

src/View/Helper/NumberFormat.php:84:5: InvalidDocblock: Found duplicated @param or prefixed @param tag in docblock for Laminas\I18n\View\Helper\NumberFormat::__invoke (see https://psalm.dev/008)
$number,

Check failure on line 85 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingParamType

src/View/Helper/NumberFormat.php:85:9: MissingParamType: Parameter $number has no provided type (see https://psalm.dev/154)
$formatStyle = null,

Check failure on line 86 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingParamType

src/View/Helper/NumberFormat.php:86:9: MissingParamType: Parameter $formatStyle has no provided type (see https://psalm.dev/154)
$formatType = null,

Check failure on line 87 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingParamType

src/View/Helper/NumberFormat.php:87:9: MissingParamType: Parameter $formatType has no provided type (see https://psalm.dev/154)
$locale = null,

Check failure on line 88 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingParamType

src/View/Helper/NumberFormat.php:88:9: MissingParamType: Parameter $locale has no provided type (see https://psalm.dev/154)
$decimals = null,
?array $textAttributes = null
$maxDecimals = null,

Check failure on line 89 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingParamType

src/View/Helper/NumberFormat.php:89:9: MissingParamType: Parameter $maxDecimals has no provided type (see https://psalm.dev/154)
?array $textAttributes = null,
$minDecimals = null

Check failure on line 91 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingParamType

src/View/Helper/NumberFormat.php:91:9: MissingParamType: Parameter $minDecimals has no provided type (see https://psalm.dev/154)
) {
if (null === $locale) {
$locale = $this->getLocale();
Expand All @@ -89,15 +99,22 @@
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)) {

Check failure on line 108 in src/View/Helper/NumberFormat.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

DocblockTypeContradiction

src/View/Helper/NumberFormat.php:108:13: DocblockTypeContradiction: Cannot resolve types for $minDecimals - docblock-defined type int does not contain null (see https://psalm.dev/155)
// 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 @@
} 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 @@
}

/**
* 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 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 @@
}

/**
* @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