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 0459a2aa..3ab140a6 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,21 @@ public function testNullValueForBooleanAttributeDisablesIt(): void $this->helper->createAttributesString(['disabled' => null]) ); } + + /** + * @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'); + $select->setValueOptions(range(0, 10)); + + $helper = new FormSelect(); + + $markup = $helper->render($select); + + self::assertStringContainsString('', $markup); + } }