diff --git a/typo3/sysext/core/Classes/Type/Enumeration.php b/typo3/sysext/core/Classes/Type/Enumeration.php index 24179b1715972f42baa4631d24bff79e2bb6115a..282ee3a98856012f412c3e508df1977158c39fc5 100644 --- a/typo3/sysext/core/Classes/Type/Enumeration.php +++ b/typo3/sysext/core/Classes/Type/Enumeration.php @@ -208,4 +208,32 @@ abstract class Enumeration implements TypeInterface { return (string)$this->value; } + + /** + * Returns the constants name as is, without manipulation (usually all upper case) + * + * @param string|int $value + * @return string + */ + public static function getName($value) + { + $name = ''; + $constants = array_flip(static::getConstants()); + if (array_key_exists($value, $constants)) { + $name = $constants[$value]; + } + return $name; + } + + /** + * Returns the name of the constant, first char upper, underscores as spaces + * + * @param string|int $value + * @return string + */ + public static function getHumanReadableName($value) + { + $name = static::getName($value); + return ucwords(strtolower(str_replace('_', ' ', $name))); + } } diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-78575-EnumerationConstantsProvideTheirNames.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-78575-EnumerationConstantsProvideTheirNames.rst new file mode 100644 index 0000000000000000000000000000000000000000..0379e3d09cf8b070be0ac8566ceef5fda1af1736 --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/master/Feature-78575-EnumerationConstantsProvideTheirNames.rst @@ -0,0 +1,24 @@ +.. include:: ../../Includes.txt + +================================================================= +Feature: #78575 - Enumeration constants don't provide their names +================================================================= + +See :issue:`78575` + +Description +=========== + +Requesting the name of Enumeration constant has been introduced. + +There are two methods you can use: +:php:`MyEnumerationClass::getName($value);` will return the name exactly as it is. Usually this will be all uppercase and underscores. +:php:`MyEnumerationClass::getHumanReadableName($value);` replaces underscores with spaces and turns all words into lowercase with the first letter uppercase. + + +Impact +====== + +You can use the constant names as part of your application more easily. + +.. index:: PHP-API diff --git a/typo3/sysext/core/Tests/Unit/Type/EnumerationTest.php b/typo3/sysext/core/Tests/Unit/Type/EnumerationTest.php index e12365f2e00b71a9e25bdc42dbb0af1aad6d7770..36a0c947944c0b2a0553880836334dce57828eb9 100644 --- a/typo3/sysext/core/Tests/Unit/Type/EnumerationTest.php +++ b/typo3/sysext/core/Tests/Unit/Type/EnumerationTest.php @@ -380,4 +380,40 @@ class EnumerationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase $enumerationBar = new Enumeration\CompleteEnumeration('foo'); $this->assertFalse($enumerationFoo->equals($enumerationBar)); } + + /** + * @test + */ + public function getNameProvidesNameForAvailableConstant() + { + $result = Enumeration\CompleteEnumeration::getName(Enumeration\CompleteEnumeration::INTEGER_VALUE); + $this->assertSame('INTEGER_VALUE', $result); + } + + /** + * @test + */ + public function getNameReturnsEmptyStringForNotAvailableConstant() + { + $result = Enumeration\CompleteEnumeration::getName(42); + $this->assertSame('', $result); + } + + /** + * @test + */ + public function getHumanReadableNameProvidesNameForAvailableConstant() + { + $result = Enumeration\CompleteEnumeration::getHumanReadableName(Enumeration\CompleteEnumeration::INTEGER_VALUE); + $this->assertSame('Integer Value', $result); + } + + /** + * @test + */ + public function getHumanReadableNameReturnsEmptyStringForNotAvailableConstant() + { + $result = Enumeration\CompleteEnumeration::getName(42); + $this->assertSame('', $result); + } }