-
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.
Decouple label building from Twig extension (#138)
It can be reused from PHP
- Loading branch information
1 parent
0efe20b
commit ccd86f4
Showing
8 changed files
with
148 additions
and
33 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
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,14 @@ | ||
<?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> | ||
<defaults public="false" /> | ||
|
||
<service id="greg0ire_enum.symfony.translator.label" class="Greg0ire\Enum\Bridge\Symfony\Translator\Label" public="true"> | ||
<argument type="service" id="translator.default" /> | ||
</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,62 @@ | ||
<?php | ||
|
||
namespace Greg0ire\Enum\Bridge\Symfony\Translator; | ||
|
||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
final class GetLabel | ||
{ | ||
/** | ||
* @var TranslatorInterface | ||
*/ | ||
private $translator; | ||
|
||
public function __construct(TranslatorInterface $translator = null) | ||
{ | ||
$this->translator = $translator; | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public function __invoke( | ||
$value, | ||
string $class, | ||
$translationDomain = null, | ||
?bool $classPrefixed = null, | ||
?string $namespaceSeparator = null | ||
): string { | ||
// 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; | ||
} | ||
} |
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