diff --git a/README.md b/README.md index 1aed853..5f06da0 100644 --- a/README.md +++ b/README.md @@ -214,10 +214,12 @@ $view = $this->factory->create(EnumType::class, null, array( #### Twig extension -This package comes with an `enum_label` filter, available thanks to the `EnumExtension` Twig class. +This package comes with an `EnumExtension` Twig class. It contains a filter and some functions. You have to require the `twig/twig` package to get it working. -The filter will try to return the constant label corresponding to the given value. +##### Filter + +The `enum_label` filter will try to return the constant label corresponding to the given value. It will try to translate it if possible. To enable translation, require the `symfony/translation` component and pass a `Symfony\Component\Translation\TranslationInterface` instance on the `EnumExtension` constructor. @@ -234,6 +236,24 @@ Usage: {{ value|enum_label('Your\\Enum\\Class', false, true, '.') }} {# Disable translation but keep class prefix with a custom separator #} ``` +##### Functions + +The 3 available twig functions are ports of some `AbstractEnum` methods that can be useful in a twig template: + +* `enum_get_constants` => `AbstractEnum::getConstants` +* `enum_get_keys` => `AbstractEnum::getKeys` +* `enum_get_class_prefixed_keys` => `AbstractEnum::getClassPrefixedKeys` + +The arguments are exactly the same except you have to specify the targeted class first (as `enum_label` filter). + +Here is a concrete example with `enum_get_constants` function: + +```twig +{% for enum_key, enum_value in enum_get_constants('Your\\Enum\\Class') %} + {{ enum_key }} -> {{ enum_value }} +{% endfor %} +``` + ##### Twig extension as a service On Symfony projects, the extension can be autoloaded. diff --git a/src/Bridge/Twig/Extension/EnumExtension.php b/src/Bridge/Twig/Extension/EnumExtension.php index 0f84c9f..4729873 100644 --- a/src/Bridge/Twig/Extension/EnumExtension.php +++ b/src/Bridge/Twig/Extension/EnumExtension.php @@ -2,6 +2,7 @@ namespace Greg0ire\Enum\Bridge\Twig\Extension; +use Greg0ire\Enum\AbstractEnum; use Symfony\Component\Translation\TranslatorInterface; /** @@ -32,6 +33,18 @@ public function getFilters() ]; } + /** + * {@inheritdoc} + */ + public function getFunctions() + { + return [ + new \Twig_SimpleFunction('enum_get_constants', [$this, 'getConstants']), + new \Twig_SimpleFunction('enum_get_keys', [$this, 'getKeys']), + new \Twig_SimpleFunction('enum_get_class_prefixed_keys', [$this, 'getClassPrefixedKeys']), + ]; + } + /** * Displays the label corresponding to a specific value of an enumeration. * @@ -73,6 +86,48 @@ public function label($value, $class, $translationDomain = null, $classPrefixed return $label; } + /** + * @see AbstractEnum::getConstants() + * + * @param string $class + * @param callable|null $keysCallback + * @param bool $classPrefixed + * @param string $namespaceSeparator + * + * @return array + */ + public function getConstants($class, $keysCallback = null, $classPrefixed = false, $namespaceSeparator = null) + { + return call_user_func([$class, 'getConstants'], $keysCallback, $classPrefixed, $namespaceSeparator); + } + + /** + * @see AbstractEnum::getKeys() + * + * @param string $class + * @param $callback|null $callback + * + * @return array + */ + public function getKeys($class, $callback = null) + { + return call_user_func([$class, 'getKeys'], $callback); + } + + /** + * @see AbstractEnum::getClassPrefixedKeys() + * + * @param string $class + * @param callable|null $callback + * @param string|null $namespaceSeparator + * + * @return mixed + */ + public function getClassPrefixedKeys($class, $callback = null, $namespaceSeparator = null) + { + return call_user_func([$class, 'getClassPrefixedKeys'], $callback, $namespaceSeparator); + } + /** * {@inheritdoc} */ diff --git a/test/Bridge/Twig/Extension/EnumExtensionTest.php b/test/Bridge/Twig/Extension/EnumExtensionTest.php index 394e6a8..3499c7d 100644 --- a/test/Bridge/Twig/Extension/EnumExtensionTest.php +++ b/test/Bridge/Twig/Extension/EnumExtensionTest.php @@ -77,4 +77,40 @@ public function testLabelWithTranslator() 'Without any available translation, the filter should just return the key.' ); } + + public function testGetConstants() + { + $this->assertSame( + [ + 'GOD' => 'Dieu', + 'CHUCK' => 'Chuck Norris', + 'GUITRY' => 'Sacha Guitry', + ], + $this->extension->getConstants(FooEnum::class) + ); + } + + public function testGetKeys() + { + $this->assertSame( + [ + 'GOD', + 'CHUCK', + 'GUITRY', + ], + $this->extension->getKeys(FooEnum::class) + ); + } + + public function testGetClassPrefixedKeys() + { + $this->assertSame( + [ + 'greg0ire_enum_tests_fixtures_foo_enum_GOD', + 'greg0ire_enum_tests_fixtures_foo_enum_CHUCK', + 'greg0ire_enum_tests_fixtures_foo_enum_GUITRY', + ], + $this->extension->getClassPrefixedKeys(FooEnum::class) + ); + } }