-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from Soullivaneuh/twig
Twig integration
- Loading branch information
Showing
12 changed files
with
441 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,35 @@ | |
|
||
namespace Greg0ire\Enum; | ||
|
||
use Doctrine\Common\Inflector\Inflector; | ||
|
||
/** | ||
* @author Grégoire Paris <[email protected]> | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
abstract class AbstractEnum | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public static $defaultNamespaceSeparator = '_'; | ||
|
||
private static $constCache = []; | ||
|
||
/** | ||
* Uses reflection to find the constants defined in the class and cache | ||
* them in a local property for performance, before returning them. | ||
* | ||
* @param callable|null $keysCallback | ||
* @param bool $classPrefixed True if you want the enum class prefix on each keys, false otherwise. | ||
* @param string $namespaceSeparator Only relevant if $classPrefixed is set to true. | ||
* | ||
* @return array a hash with your constants and their value. Useful for | ||
* building a choice widget | ||
*/ | ||
final public static function getConstants($keysCallback = null) | ||
final public static function getConstants($keysCallback = null, $classPrefixed = false, $namespaceSeparator = null) | ||
{ | ||
$namespaceSeparator = $namespaceSeparator ?: static::$defaultNamespaceSeparator; | ||
$enumTypes = static::getEnumTypes(); | ||
$enums = []; | ||
|
||
|
@@ -44,8 +54,13 @@ final public static function getConstants($keysCallback = null) | |
} | ||
} | ||
|
||
if (is_callable($keysCallback)) { | ||
return array_combine(static::getKeys($keysCallback), $enums); | ||
if (is_callable($keysCallback) || $classPrefixed) { | ||
return array_combine( | ||
$classPrefixed | ||
? static::getClassPrefixedKeys($keysCallback, $namespaceSeparator) | ||
: static::getKeys($keysCallback), | ||
$enums | ||
); | ||
} | ||
|
||
return $enums; | ||
|
@@ -69,6 +84,30 @@ final public static function getKeys($callback = null) | |
return $keys; | ||
} | ||
|
||
/** | ||
* @param callable|null $callback A callable function compatible with array_map | ||
* @param string|null $namespaceSeparator Choose which character should replace namespaces separation. | ||
* Example: With Foo\BarMagic enum class with '.' separator, | ||
* it will be converted to foo.bar_magic.YOUR_KEY | ||
* | ||
* @return string[] | ||
*/ | ||
final public static function getClassPrefixedKeys($callback = null, $namespaceSeparator = null) | ||
{ | ||
$namespaceSeparator = $namespaceSeparator ?: static::$defaultNamespaceSeparator; | ||
$classKey = str_replace('\\', $namespaceSeparator, Inflector::tableize(static::class)); | ||
|
||
$keys = static::getKeys(function ($key) use ($namespaceSeparator, $classKey) { | ||
return $classKey.$namespaceSeparator.$key; | ||
}); | ||
|
||
if (is_callable($callback)) { | ||
return array_map($callback, $keys); | ||
} | ||
|
||
return $keys; | ||
} | ||
|
||
/** | ||
* Checks whether a constant with this name is defined. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Greg0ire\Enum\Bridge\Symfony\Bundle; | ||
|
||
use Greg0ire\Enum\Bridge\Symfony\DependencyInjection\Greg0ireEnumExtension; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class Greg0ireEnumBundle extends Bundle | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getContainerExtensionClass() | ||
{ | ||
return Greg0ireEnumExtension::class; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Bridge/Symfony/DependencyInjection/Greg0ireEnumExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Greg0ire\Enum\Bridge\Symfony\DependencyInjection; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Translation\Translator; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class Greg0ireEnumExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
|
||
if (class_exists(\Twig_Extension::class)) { | ||
$loader->load('twig.xml'); | ||
|
||
if (class_exists(Translator::class)) { | ||
$container->getDefinition('greg0ire_enum.twig.extension.enum') | ||
->addArgument(new Reference('translator.default')); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="greg0ire_enum.twig.extension.enum" class="Greg0ire\Enum\Bridge\Twig\Extension\EnumExtension"> | ||
<tag name="twig.extension"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Greg0ire\Enum\Bridge\Twig\Extension; | ||
|
||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class EnumExtension extends \Twig_Extension | ||
{ | ||
/** | ||
* @var TranslatorInterface | ||
*/ | ||
private $translator; | ||
|
||
/** | ||
* @param TranslatorInterface $translator | ||
*/ | ||
public function __construct(TranslatorInterface $translator = null) | ||
{ | ||
$this->translator = $translator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFilters() | ||
{ | ||
return [ | ||
new \Twig_SimpleFilter('enum_label', [$this, 'label']), | ||
]; | ||
} | ||
|
||
/** | ||
* Displays the label corresponding to a specific value of an enumeration. | ||
* | ||
* @param mixed $value Must exists in the enumeration class specified with $class | ||
* @param string $class The enum class name | ||
* @param string|bool $translationDomain The translation domain to use if the translator if available. | ||
* string: Use the specified one | ||
* null: Use the default one | ||
* false: Do not use the translator | ||
* @param bool $classPrefixed Prefix the label with the enum class. Defaults to true if the translator | ||
* is available and enabled, false otherwise. | ||
* @param string $namespaceSeparator Namespace separator to use with the class prefix. | ||
* This takes effect only if $classPrefixed is true. | ||
* | ||
* @return string | ||
*/ | ||
public function label($value, $class, $translationDomain = null, $classPrefixed = null, $namespaceSeparator = null) | ||
{ | ||
// Determine if the translator can be used or not. | ||
$useTranslation = $this->translator instanceof TranslatorInterface | ||
&& (is_null($translationDomain) || is_string($translationDomain)); | ||
|
||
// If not defined, guess the default behavior. | ||
if (is_null($classPrefixed)) { | ||
$classPrefixed = $useTranslation; | ||
} | ||
|
||
$label = array_search( | ||
$value, | ||
call_user_func([$class, 'getConstants'], 'strtolower', $classPrefixed, $namespaceSeparator) | ||
); | ||
|
||
if ($useTranslation) { | ||
$translatedLabel = $this->translator->trans($label, [], $translationDomain); | ||
|
||
return $translatedLabel ?: $label; | ||
} | ||
|
||
return $label; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'greg0ire_enum'; | ||
} | ||
} |
Oops, something went wrong.