From e74c7758c3b445dfd6a16f59a88d07b5618d24c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 08:03:21 +0100 Subject: [PATCH 01/14] Add the possibility to set the minimum number of decimals in the number formatter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 90 +++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 14 deletions(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 4b4c0648..781f51c6 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -20,11 +20,18 @@ 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 @@ -68,8 +75,10 @@ class NumberFormat extends AbstractHelper * @param int|null $formatStyle * @param int|null $formatType * @param string|null $locale - * @param int|null $decimals + * @param int|null $maxDecimals * @param array|null $textAttributes + * @param int|null $minDecimals + * * @return string */ public function __invoke( @@ -77,8 +86,9 @@ public function __invoke( $formatStyle = null, $formatType = null, $locale = null, - $decimals = null, - ?array $textAttributes = null + $maxDecimals = null, + ?array $textAttributes = null, + ?int $minDecimals = null ) { if (null === $locale) { $locale = $this->getLocale(); @@ -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)) ); @@ -106,9 +123,9 @@ 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 ($maxDecimals !== null) { + $formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $minDecimals); + $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxDecimals); } foreach ($textAttributes as $textAttribute => $value) { @@ -173,14 +190,39 @@ public function getFormatType() } /** - * Set number of decimals to use instead of the default. + * Set number (both min & max) of decimals 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|null $maxDecimals + * @return $this + */ + public function setMaxDecimals(?int $maxDecimals): self + { + $this->maxDecimals = $maxDecimals; + return $this; + } + + /** + * Set the minimum number of decimals to use instead of the default. + * + * @param int|null $minDecimals + * @return $this + */ + public function setMinDecimals(?int $minDecimals): self + { + $this->minDecimals = $minDecimals; return $this; } @@ -191,7 +233,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(): int + { + return $this->maxDecimals; + } + + /** + * Get the minimum number of decimals. + * + * @return int + */ + public function getMinDecimals(): int + { + return $this->minDecimals; } /** From b70b87ec2301e5936f3e17d583e6c5d89917fb6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 08:25:23 +0100 Subject: [PATCH 02/14] added types to variables and function returns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 46 ++++++++++++++++---------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 781f51c6..260afa80 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -24,49 +24,49 @@ class NumberFormat extends AbstractHelper * * @var int */ - protected $maxDecimals; + protected ?int $maxDecimals = null; /** * The minimum number of decimals to use. * * @var int */ - protected $minDecimals; + protected ?int $minDecimals = null; /** * NumberFormat style to use * * @var int */ - protected $formatStyle; + protected ?int $formatStyle = null; /** * NumberFormat type to use * * @var int */ - protected $formatType; + protected ?int $formatType = null; /** * Formatter instances * * @var array */ - protected $formatters = []; + protected array $formatters = []; /** * Text attributes. * * @var array */ - protected $textAttributes = []; + protected array $textAttributes = []; /** * Locale to use instead of the default * * @var string */ - protected $locale; + protected ?string $locale = null; /** * Format a number @@ -83,10 +83,10 @@ class NumberFormat extends AbstractHelper */ public function __invoke( $number, - $formatStyle = null, - $formatType = null, - $locale = null, - $maxDecimals = null, + ?int $formatStyle = null, + ?int $formatType = null, + ?string $locale = null, + ?int $maxDecimals = null, ?array $textAttributes = null, ?int $minDecimals = null ) { @@ -144,7 +144,7 @@ public function __invoke( * @param int $formatStyle * @return $this */ - public function setFormatStyle($formatStyle) + public function setFormatStyle($formatStyle): self { $this->formatStyle = (int) $formatStyle; return $this; @@ -155,7 +155,7 @@ public function setFormatStyle($formatStyle) * * @return int */ - public function getFormatStyle() + public function getFormatStyle(): int { if (null === $this->formatStyle) { $this->formatStyle = NumberFormatter::DECIMAL; @@ -170,7 +170,7 @@ public function getFormatStyle() * @param int $formatType * @return $this */ - public function setFormatType($formatType) + public function setFormatType($formatType): self { $this->formatType = (int) $formatType; return $this; @@ -181,7 +181,7 @@ public function setFormatType($formatType) * * @return int */ - public function getFormatType() + public function getFormatType(): int { if (null === $this->formatType) { $this->formatType = NumberFormatter::TYPE_DEFAULT; @@ -195,10 +195,10 @@ public function getFormatType() * @param int $decimals * @return $this */ - public function setDecimals($decimals) + public function setDecimals($decimals): self { - $this->minDecimals = $decimals; - $this->maxDecimals = $decimals; + $this->minDecimals = (int) $decimals; + $this->maxDecimals = (int) $decimals; return $this; } @@ -231,7 +231,7 @@ public function setMinDecimals(?int $minDecimals): self * * @return int */ - public function getDecimals() + public function getDecimals(): int { return $this->maxDecimals; } @@ -262,7 +262,7 @@ public function getMinDecimals(): int * @param string $locale * @return $this */ - public function setLocale($locale) + public function setLocale($locale): self { $this->locale = (string) $locale; return $this; @@ -273,7 +273,7 @@ public function setLocale($locale) * * @return string */ - public function getLocale() + public function getLocale(): string { if ($this->locale === null) { $this->locale = Locale::getDefault(); @@ -285,7 +285,7 @@ public function getLocale() /** * @return array */ - public function getTextAttributes() + public function getTextAttributes(): array { return $this->textAttributes; } @@ -294,7 +294,7 @@ public function getTextAttributes() * @param array $textAttributes * @return $this */ - public function setTextAttributes(array $textAttributes) + public function setTextAttributes(array $textAttributes): self { $this->textAttributes = $textAttributes; return $this; From b33f75841deb88d836078bf2f49720edf3c6da1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 09:05:26 +0100 Subject: [PATCH 03/14] added types to variables and function returns 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 260afa80..d3bd3443 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -144,9 +144,9 @@ public function __invoke( * @param int $formatStyle * @return $this */ - public function setFormatStyle($formatStyle): self + public function setFormatStyle(int $formatStyle): self { - $this->formatStyle = (int) $formatStyle; + $this->formatStyle = $formatStyle; return $this; } @@ -229,9 +229,9 @@ public function setMinDecimals(?int $minDecimals): self /** * Get number of decimals. * - * @return int + * @return int|null */ - public function getDecimals(): int + public function getDecimals(): ?int { return $this->maxDecimals; } @@ -239,9 +239,9 @@ public function getDecimals(): int /** * Get the maximum number of decimals. * - * @return int + * @return int|null */ - public function getMaxDecimals(): int + public function getMaxDecimals(): ?int { return $this->maxDecimals; } @@ -249,9 +249,9 @@ public function getMaxDecimals(): int /** * Get the minimum number of decimals. * - * @return int + * @return int|null */ - public function getMinDecimals(): int + public function getMinDecimals(): ?int { return $this->minDecimals; } From c7d574eb450bbfba325224a7a1f0904fcc45cdbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 09:07:29 +0100 Subject: [PATCH 04/14] add new parameter to HelperTrait MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/HelperTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/View/HelperTrait.php b/src/View/HelperTrait.php index 7c05fe84..26b68ba8 100644 --- a/src/View/HelperTrait.php +++ b/src/View/HelperTrait.php @@ -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) From b9856454d4b4c3056caee338f1cee37390063264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 09:26:22 +0100 Subject: [PATCH 05/14] set both parameters separatly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index d3bd3443..4a533612 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -123,8 +123,11 @@ public function __invoke( } else { $formatter = new NumberFormatter($locale, $formatStyle); - if ($maxDecimals !== null) { + if ($minDecimals !== null) { $formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $minDecimals); + } + + if ($maxDecimals !== null) { $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxDecimals); } From 6836760d6ab4aee7a28f36eb190122ba02dfec6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 09:33:30 +0100 Subject: [PATCH 06/14] Fix Psalm "cannot concatenate with possibly null" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 4a533612..1ef99702 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -21,29 +21,21 @@ class NumberFormat extends AbstractHelper /** * The maximum number of decimals to use. - * - * @var int */ protected ?int $maxDecimals = null; /** * The minimum number of decimals to use. - * - * @var int */ protected ?int $minDecimals = null; /** * NumberFormat style to use - * - * @var int */ protected ?int $formatStyle = null; /** * NumberFormat type to use - * - * @var int */ protected ?int $formatType = null; @@ -56,15 +48,11 @@ class NumberFormat extends AbstractHelper /** * Text attributes. - * - * @var array */ protected array $textAttributes = []; /** * Locale to use instead of the default - * - * @var string */ protected ?string $locale = null; @@ -72,12 +60,6 @@ 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 $maxDecimals - * @param array|null $textAttributes - * @param int|null $minDecimals * * @return string */ @@ -114,7 +96,7 @@ public function __invoke( } $formatterId = md5( - $formatStyle . "\0" . $locale . "\0" . $minDecimals . "\0" . $maxDecimals . "\0" + $formatStyle . "\0" . $locale . "\0" . ($minDecimals ?? '') . "\0" . ($maxDecimals ?? '') . "\0" . md5(serialize($textAttributes)) ); @@ -195,13 +177,13 @@ public function getFormatType(): int /** * Set number (both min & max) of decimals to use instead of the default. * - * @param int $decimals + * @param int|null $decimals * @return $this */ - public function setDecimals($decimals): self + public function setDecimals(?int $decimals): self { - $this->minDecimals = (int) $decimals; - $this->maxDecimals = (int) $decimals; + $this->minDecimals = $decimals; + $this->maxDecimals = $decimals; return $this; } From c00e084c9a3e3878ed73ec3fce40dace139ebbb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 09:35:10 +0100 Subject: [PATCH 07/14] Remove "useless annotations" according to PHPCodeSniffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 45 +++----------------------------- 1 file changed, 3 insertions(+), 42 deletions(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 1ef99702..9de581aa 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -60,8 +60,6 @@ class NumberFormat extends AbstractHelper * Format a number * * @param int|float $number - * - * @return string */ public function __invoke( $number, @@ -71,7 +69,7 @@ public function __invoke( ?int $maxDecimals = null, ?array $textAttributes = null, ?int $minDecimals = null - ) { + ): string { if (null === $locale) { $locale = $this->getLocale(); } @@ -125,9 +123,6 @@ public function __invoke( /** * Set format style to use instead of the default - * - * @param int $formatStyle - * @return $this */ public function setFormatStyle(int $formatStyle): self { @@ -137,8 +132,6 @@ public function setFormatStyle(int $formatStyle): self /** * Get the format style to use - * - * @return int */ public function getFormatStyle(): int { @@ -151,9 +144,6 @@ public function getFormatStyle(): int /** * Set format type to use instead of the default - * - * @param int $formatType - * @return $this */ public function setFormatType($formatType): self { @@ -163,8 +153,6 @@ public function setFormatType($formatType): self /** * Get the format type to use - * - * @return int */ public function getFormatType(): int { @@ -176,9 +164,6 @@ public function getFormatType(): int /** * Set number (both min & max) of decimals to use instead of the default. - * - * @param int|null $decimals - * @return $this */ public function setDecimals(?int $decimals): self { @@ -189,9 +174,6 @@ public function setDecimals(?int $decimals): self /** * Set the maximum number of decimals to use instead of the default. - * - * @param int|null $maxDecimals - * @return $this */ public function setMaxDecimals(?int $maxDecimals): self { @@ -201,9 +183,6 @@ public function setMaxDecimals(?int $maxDecimals): self /** * Set the minimum number of decimals to use instead of the default. - * - * @param int|null $minDecimals - * @return $this */ public function setMinDecimals(?int $minDecimals): self { @@ -213,8 +192,6 @@ public function setMinDecimals(?int $minDecimals): self /** * Get number of decimals. - * - * @return int|null */ public function getDecimals(): ?int { @@ -223,8 +200,6 @@ public function getDecimals(): ?int /** * Get the maximum number of decimals. - * - * @return int|null */ public function getMaxDecimals(): ?int { @@ -233,8 +208,6 @@ public function getMaxDecimals(): ?int /** * Get the minimum number of decimals. - * - * @return int|null */ public function getMinDecimals(): ?int { @@ -243,20 +216,15 @@ public function getMinDecimals(): ?int /** * Set locale to use instead of the default. - * - * @param string $locale - * @return $this */ - public function setLocale($locale): self + public function setLocale(?string $locale): self { - $this->locale = (string) $locale; + $this->locale = $locale; return $this; } /** * Get the locale to use - * - * @return string */ public function getLocale(): string { @@ -267,18 +235,11 @@ public function getLocale(): string return $this->locale; } - /** - * @return array - */ public function getTextAttributes(): array { return $this->textAttributes; } - /** - * @param array $textAttributes - * @return $this - */ public function setTextAttributes(array $textAttributes): self { $this->textAttributes = $textAttributes; From 4de17d6b44a4f0b7387ec9214ce3d77e569b7f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 09:37:34 +0100 Subject: [PATCH 08/14] Add array type-hint to text attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 9de581aa..d9d909c6 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -48,6 +48,8 @@ class NumberFormat extends AbstractHelper /** * Text attributes. + * + * @var array */ protected array $textAttributes = []; @@ -59,7 +61,8 @@ class NumberFormat extends AbstractHelper /** * Format a number * - * @param int|float $number + * @param int|float $number + * @param array $textAttributes */ public function __invoke( $number, @@ -145,9 +148,9 @@ public function getFormatStyle(): int /** * Set format type to use instead of the default */ - public function setFormatType($formatType): self + public function setFormatType(?int $formatType): self { - $this->formatType = (int) $formatType; + $this->formatType = $formatType; return $this; } @@ -235,11 +238,17 @@ public function getLocale(): string return $this->locale; } + /** + * @return array + */ public function getTextAttributes(): array { return $this->textAttributes; } + /** + * @param array $textAttributes + */ public function setTextAttributes(array $textAttributes): self { $this->textAttributes = $textAttributes; From 010f4c1fdcf47cf041b527258f7ed7f52c97d21f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 09:38:06 +0100 Subject: [PATCH 09/14] declare strict_types=1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index d9d909c6..864c7fb5 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -1,5 +1,7 @@ Date: Mon, 13 Nov 2023 09:38:28 +0100 Subject: [PATCH 10/14] Fix Class description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 864c7fb5..1ba3f854 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -15,7 +15,7 @@ use function serialize; /** - * View helper for formatting dates. + * View helper for formatting numbers. */ class NumberFormat extends AbstractHelper { From 17d34e2feb5b9ce0fd26af910b4de7f3fe09455f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 10:01:05 +0100 Subject: [PATCH 11/14] add Unit-Testcase for the changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- test/View/Helper/NumberFormatTest.php | 48 +++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/test/View/Helper/NumberFormatTest.php b/test/View/Helper/NumberFormatTest.php index 8286d1e5..c2b8d471 100644 --- a/test/View/Helper/NumberFormatTest.php +++ b/test/View/Helper/NumberFormatTest.php @@ -23,7 +23,7 @@ protected function setUp(): void } /** @return array, 5: float, 6: string}> */ - public static function currencyTestsDataProvider(): array + public static function numberTestsDataProvider(): array { return [ [ @@ -32,6 +32,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234567890000, '1.234.567,891', ], @@ -41,6 +42,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, 6, [], + null, 1234567.891234567890000, '1.234.567,891235', ], @@ -50,6 +52,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234567890000, '123.456.789 %', ], @@ -59,6 +62,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, 1, [], + null, 1234567.891234567890000, '123.456.789,1 %', ], @@ -68,6 +72,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234560000, '1,23456789123456E6', ], @@ -77,6 +82,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234567890000, '1 234 567,891', ], @@ -86,6 +92,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234567890000, '123 456 789 %', ], @@ -95,6 +102,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234560000, '1,23456789123456E6', ], @@ -104,6 +112,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234567890000, '1,234,567.891', ], @@ -113,6 +122,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234567890000, '123,456,789%', ], @@ -122,6 +132,7 @@ public static function currencyTestsDataProvider(): array NumberFormatter::TYPE_DOUBLE, null, [], + null, 1234567.891234560000, '1.23456789123456E6', ], @@ -133,22 +144,44 @@ public static function currencyTestsDataProvider(): array [ NumberFormatter::NEGATIVE_PREFIX => 'MINUS', ], + null, -1234567.891234567890000, 'MINUS123,456,789%', ], + [ + 'de_DE', + NumberFormatter::DECIMAL, + NumberFormatter::TYPE_DOUBLE, + 5, + [], + 0, + 1234567.891234567890000, + '1.234.567,89123', + ], + [ + 'de_DE', + NumberFormatter::DECIMAL, + NumberFormatter::TYPE_DOUBLE, + 5, + [], + 0, + 1234567, + '1.234.567', + ], ]; } /** * @param array $textAttributes */ - #[DataProvider('currencyTestsDataProvider')] + #[DataProvider('numberTestsDataProvider')] public function testBasic( string $locale, int $formatStyle, int $formatType, ?int $decimals, array $textAttributes, + ?int $minDecimals, float $number, string $expected ): void { @@ -158,29 +191,32 @@ public function testBasic( $formatType, $locale, $decimals, - $textAttributes + $textAttributes, + $minDecimals )); } /** * @param array $textAttributes */ - #[DataProvider('currencyTestsDataProvider')] + #[DataProvider('numberTestsDataProvider')] public function testSettersProvideDefaults( string $locale, int $formatStyle, int $formatType, ?int $decimals, array $textAttributes, + ?int $minDecimals, float $number, string $expected ): void { $this->helper ->setLocale($locale) ->setFormatStyle($formatStyle) - ->setDecimals($decimals) + ->setMaxDecimals($decimals) ->setFormatType($formatType) - ->setTextAttributes($textAttributes); + ->setTextAttributes($textAttributes) + ->setMinDecimals($minDecimals); self::assertMbStringEquals($expected, $this->helper->__invoke($number)); } From 864b2c75ea840b58a57f43b44ba339a8a235f9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 10:04:24 +0100 Subject: [PATCH 12/14] fix @return in Testcase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- test/View/Helper/NumberFormatTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/View/Helper/NumberFormatTest.php b/test/View/Helper/NumberFormatTest.php index c2b8d471..7c49a822 100644 --- a/test/View/Helper/NumberFormatTest.php +++ b/test/View/Helper/NumberFormatTest.php @@ -22,7 +22,7 @@ protected function setUp(): void $this->helper = new NumberFormatHelper(); } - /** @return array, 5: float, 6: string}> */ + /** @return array, 5: int|null, 6: float, 7: string}> */ public static function numberTestsDataProvider(): array { return [ From e2322efbc607f8abd7837a9bbbae1ac9523c3393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 16:03:51 +0100 Subject: [PATCH 13/14] Revert type hinting. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- src/View/Helper/NumberFormat.php | 116 +++++++++++++++++++++---------- 1 file changed, 81 insertions(+), 35 deletions(-) diff --git a/src/View/Helper/NumberFormat.php b/src/View/Helper/NumberFormat.php index 1ba3f854..4af2f02b 100644 --- a/src/View/Helper/NumberFormat.php +++ b/src/View/Helper/NumberFormat.php @@ -1,7 +1,5 @@ */ - protected array $formatters = []; + protected $formatters = []; /** * Text attributes. * * @var array */ - protected array $textAttributes = []; + protected $textAttributes = []; /** * Locale to use instead of the default + * + * @var string */ - protected ?string $locale = null; + protected $locale; /** * Format a number * - * @param int|float $number - * @param array $textAttributes + * @param int|float $number + * @param int|null $formatStyle + * @param int|null $formatType + * @param string|null $locale + * @param int|null $maxDecimals + * @param array $textAttributes + * @param int|float $number + * @param int|null $minDecimals + * @return string */ public function __invoke( $number, - ?int $formatStyle = null, - ?int $formatType = null, - ?string $locale = null, - ?int $maxDecimals = null, + $formatStyle = null, + $formatType = null, + $locale = null, + $maxDecimals = null, ?array $textAttributes = null, - ?int $minDecimals = null - ): string { + $minDecimals = null + ) { if (null === $locale) { $locale = $this->getLocale(); } @@ -128,17 +143,22 @@ public function __invoke( /** * Set format style to use instead of the default + * + * @param int $formatStyle + * @return $this */ - public function setFormatStyle(int $formatStyle): self + public function setFormatStyle($formatStyle) { - $this->formatStyle = $formatStyle; + $this->formatStyle = (int) $formatStyle; return $this; } /** * Get the format style to use + * + * @return int */ - public function getFormatStyle(): int + public function getFormatStyle() { if (null === $this->formatStyle) { $this->formatStyle = NumberFormatter::DECIMAL; @@ -149,17 +169,22 @@ public function getFormatStyle(): int /** * Set format type to use instead of the default + * + * @param int $formatType + * @return $this */ - public function setFormatType(?int $formatType): self + public function setFormatType($formatType) { - $this->formatType = $formatType; + $this->formatType = (int) $formatType; return $this; } /** * Get the format type to use + * + * @return int */ - public function getFormatType(): int + public function getFormatType() { if (null === $this->formatType) { $this->formatType = NumberFormatter::TYPE_DEFAULT; @@ -168,9 +193,12 @@ public function getFormatType(): int } /** - * Set number (both min & max) 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(?int $decimals): self + public function setDecimals($decimals) { $this->minDecimals = $decimals; $this->maxDecimals = $decimals; @@ -179,8 +207,11 @@ public function setDecimals(?int $decimals): self /** * Set the maximum number of decimals to use instead of the default. + * + * @param int $maxDecimals + * @return $this */ - public function setMaxDecimals(?int $maxDecimals): self + public function setMaxDecimals($maxDecimals) { $this->maxDecimals = $maxDecimals; return $this; @@ -188,8 +219,11 @@ public function setMaxDecimals(?int $maxDecimals): self /** * Set the minimum number of decimals to use instead of the default. + * + * @param int $minDecimals + * @return $this */ - public function setMinDecimals(?int $minDecimals): self + public function setMinDecimals($minDecimals) { $this->minDecimals = $minDecimals; return $this; @@ -197,41 +231,52 @@ public function setMinDecimals(?int $minDecimals): self /** * Get number of decimals. + * + * @return int */ - public function getDecimals(): ?int + public function getDecimals() { return $this->maxDecimals; } /** * Get the maximum number of decimals. + * + * @return int */ - public function getMaxDecimals(): ?int + public function getMaxDecimals() { return $this->maxDecimals; } /** * Get the minimum number of decimals. + * + * @return int */ - public function getMinDecimals(): ?int + public function getMinDecimals() { return $this->minDecimals; } /** * Set locale to use instead of the default. + * + * @param string $locale + * @return $this */ - public function setLocale(?string $locale): self + public function setLocale($locale) { - $this->locale = $locale; + $this->locale = (string) $locale; return $this; } /** * Get the locale to use + * + * @return string */ - public function getLocale(): string + public function getLocale() { if ($this->locale === null) { $this->locale = Locale::getDefault(); @@ -243,15 +288,16 @@ public function getLocale(): string /** * @return array */ - public function getTextAttributes(): array + public function getTextAttributes() { return $this->textAttributes; } /** * @param array $textAttributes + * @return $this */ - public function setTextAttributes(array $textAttributes): self + public function setTextAttributes(array $textAttributes) { $this->textAttributes = $textAttributes; return $this; From 06a20fdaf1039cf0a87d6e3013c7a3f76a5ea03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=BChne?= Date: Mon, 13 Nov 2023 16:05:56 +0100 Subject: [PATCH 14/14] add psalm supression to "possibly unused methods" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matthias Kühne --- psalm.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/psalm.xml b/psalm.xml index df40fdc3..851497c0 100644 --- a/psalm.xml +++ b/psalm.xml @@ -34,6 +34,11 @@ + + + + +