-
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.
Add an optional Twig integration to have an extension for enum labels.
- Loading branch information
1 parent
601012e
commit b60793a
Showing
5 changed files
with
103 additions
and
1 deletion.
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
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
<?php | ||
|
||
namespace Greg0ire\Enum\Bridge\Symfony\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
|
||
/** | ||
* @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'); | ||
} | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace Greg0ire\Enum\Bridge\Twig\Extension; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class EnumExtension extends \Twig_Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFilters() | ||
{ | ||
return [ | ||
new \Twig_SimpleFilter('enum_label', [$this, 'label']), | ||
]; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param string $class The enum class name | ||
* @param bool $prefixWithClass Prefix key with class name if true | ||
* | ||
* @return string | ||
*/ | ||
public function label($value, $class, $prefixWithClass = false) | ||
{ | ||
$constants = call_user_func([$class, 'getConstants'], 'strtolower', $prefixWithClass); | ||
|
||
return array_search($value, $constants); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'greg0ire_enum'; | ||
} | ||
} |