From 332a31ae1aa3e29b2cb8170c7923eee689b0c039 Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 11 Jan 2024 13:48:40 +0000 Subject: [PATCH 1/2] Add failing test case for integer element labels Signed-off-by: George Steel --- test/View/Helper/AbstractHelperTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/View/Helper/AbstractHelperTest.php b/test/View/Helper/AbstractHelperTest.php index 0459a2aa..1dbde7c5 100644 --- a/test/View/Helper/AbstractHelperTest.php +++ b/test/View/Helper/AbstractHelperTest.php @@ -5,10 +5,14 @@ namespace LaminasTest\Form\View\Helper; use Laminas\Escaper\Escaper; +use Laminas\Form\Element\Select; use Laminas\Form\Exception\InvalidArgumentException; use Laminas\Form\View\Helper\AbstractHelper; +use Laminas\Form\View\Helper\FormSelect; use Laminas\I18n\Translator\Translator; +use function range; + /** * Tests for {@see \Laminas\Form\View\Helper\AbstractHelper} * @@ -236,4 +240,16 @@ public function testNullValueForBooleanAttributeDisablesIt(): void $this->helper->createAttributesString(['disabled' => null]) ); } + + public function testThatAnIntegerElementLabelWillBeCastToAString(): void + { + $select = new Select('some-name'); + $select->setValueOptions(range(0, 10)); + + $helper = new FormSelect(); + + $markup = $helper->render($select); + + self::assertStringContainsString('', $markup); + } } From cdeb1493cf021d48a8c306fb7b41484318a071c2 Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 11 Jan 2024 14:09:33 +0000 Subject: [PATCH 2/2] Expand types for `translateLabel()` argument to preserve legacy BC. String is the correct argument type, but it is not possible to implement in the 3.x series Signed-off-by: George Steel --- psalm-baseline.xml | 3 +++ src/View/Helper/AbstractHelper.php | 9 +++++++-- test/View/Helper/AbstractHelperTest.php | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 5a34f811..23a3f357 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -295,6 +295,9 @@ translate translate + + (string) $label + diff --git a/src/View/Helper/AbstractHelper.php b/src/View/Helper/AbstractHelper.php index fe898e86..f50ee9b3 100644 --- a/src/View/Helper/AbstractHelper.php +++ b/src/View/Helper/AbstractHelper.php @@ -561,12 +561,17 @@ protected function hasAllowedPrefix(string $attribute): bool } /** - * translate the label + * Translate the label * * @internal + * + * @todo Reduce argument to only string in the next major + * @param string $label */ - protected function translateLabel(string $label): string + protected function translateLabel(int|string|float|bool $label): string { + $label = (string) $label; + return $this->getTranslator()?->translate($label, $this->getTranslatorTextDomain()) ?? $label; } diff --git a/test/View/Helper/AbstractHelperTest.php b/test/View/Helper/AbstractHelperTest.php index 1dbde7c5..3ab140a6 100644 --- a/test/View/Helper/AbstractHelperTest.php +++ b/test/View/Helper/AbstractHelperTest.php @@ -241,6 +241,11 @@ public function testNullValueForBooleanAttributeDisablesIt(): void ); } + /** + * @deprecated This test should be removed in 4.0 and string should become a hard requirement for labels + * + * @covers \Laminas\Form\View\Helper\AbstractHelper::translateLabel + */ public function testThatAnIntegerElementLabelWillBeCastToAString(): void { $select = new Select('some-name');