-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce EnumInterface::getLabel (#39)
* Introduce EnumInterface::getLabel to not rely on getChoices from the outside * Store translated enum choices in a private property to avoid calling translation more than once per enum * Normalize assert method usage to static calls * Fixed doc use statements * #37 $choices assignment refacto + comment getLabel in EnumInterface for next major (#40) Co-authored-by: Mathieu Ducrot <[email protected]>
- Loading branch information
1 parent
89143e5
commit b0c3944
Showing
22 changed files
with
234 additions
and
59 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 |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
*/ | ||
class ConfigurableEnum implements EnumInterface | ||
{ | ||
use EnumLabelTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
|
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 |
---|---|---|
|
@@ -6,16 +6,31 @@ | |
|
||
/** | ||
* @author Yann Eugoné <[email protected]> | ||
* | ||
* NEXT_MAJOR: Add all these methods to the interface by uncommenting them. | ||
* | ||
* @method string getLabel(string $value) | ||
*/ | ||
interface EnumInterface | ||
{ | ||
/** | ||
* Returns enum choices (value as keys, labels as values) | ||
* | ||
* @return array | ||
*/ | ||
public function getChoices(): array; | ||
|
||
/** | ||
* Returns enum identifier (must be unique across your app). | ||
* | ||
* @return string | ||
*/ | ||
public function getName(): string; | ||
|
||
/** | ||
* NEXT_MAJOR: uncomment this method | ||
* | ||
* @return string | ||
*/ | ||
// public function getLabel(string $value): string; | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Yokai\EnumBundle; | ||
|
||
use Yokai\EnumBundle\Exception\InvalidEnumValueException; | ||
|
||
trait EnumLabelTrait | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getLabel(string $value): string | ||
{ | ||
$choices = $this->getChoices(); | ||
if (!isset($choices[$value])) { | ||
throw InvalidEnumValueException::create($this, $value); | ||
} | ||
|
||
return $choices[$value]; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
/** | ||
* @author Yann Eugoné <[email protected]> | ||
*/ | ||
class CannotExtractConstantsException extends InvalidArgumentException | ||
class CannotExtractConstantsException extends InvalidArgumentException implements ExceptionInterface | ||
{ | ||
public static function invalidPattern(string $pattern): self | ||
{ | ||
|
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
/** | ||
* @author Yann Eugoné <[email protected]> | ||
*/ | ||
class DuplicatedEnumException extends BadMethodCallException | ||
class DuplicatedEnumException extends BadMethodCallException implements ExceptionInterface | ||
{ | ||
/** | ||
* @param string $name | ||
|
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,9 @@ | ||
<?php | ||
|
||
namespace Yokai\EnumBundle\Exception; | ||
|
||
use Throwable; | ||
|
||
interface ExceptionInterface extends Throwable | ||
{ | ||
} |
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
/** | ||
* @author Yann Eugoné <[email protected]> | ||
*/ | ||
class InvalidEnumException extends DomainException | ||
class InvalidEnumException extends DomainException implements ExceptionInterface | ||
{ | ||
/** | ||
* @param string $name | ||
|
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,16 @@ | ||
<?php | ||
|
||
namespace Yokai\EnumBundle\Exception; | ||
|
||
use InvalidArgumentException; | ||
use Yokai\EnumBundle\EnumInterface; | ||
|
||
final class InvalidEnumValueException extends InvalidArgumentException implements ExceptionInterface | ||
{ | ||
public static function create(EnumInterface $enum, string $value): self | ||
{ | ||
return new self( | ||
\sprintf('Enum "%s" does not have "%s" value.', $enum->getName(), $value) | ||
); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
/** | ||
* @author Yann Eugoné <[email protected]> | ||
*/ | ||
class InvalidTranslatePatternException extends InvalidArgumentException | ||
class InvalidTranslatePatternException extends InvalidArgumentException implements ExceptionInterface | ||
{ | ||
/** | ||
* @param string $transPattern | ||
|
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use Twig\TwigFilter; | ||
use Twig\TwigFunction; | ||
use Yokai\EnumBundle\EnumRegistry; | ||
use Yokai\EnumBundle\Exception\InvalidEnumValueException; | ||
|
||
/** | ||
* @author Yann Eugoné <[email protected]> | ||
|
@@ -56,7 +57,16 @@ public function getFilters(): array | |
*/ | ||
public function getLabel(string $value, string $enum): string | ||
{ | ||
return $this->getChoices($enum)[$value] ?? $value; | ||
$enum = $this->registry->get($enum); | ||
if (\method_exists($enum, 'getLabel')) { | ||
try { | ||
return $enum->getLabel($value); | ||
} catch (InvalidEnumValueException $exception) { | ||
return $value; | ||
} | ||
} | ||
|
||
return $enum->getChoices()[$value] ?? $value; | ||
} | ||
|
||
/** | ||
|
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace Yokai\EnumBundle\Tests; | ||
|
||
use Yokai\EnumBundle\ConfigurableEnum; | ||
use Yokai\EnumBundle\Exception\InvalidEnumValueException; | ||
|
||
/** | ||
* @author Yann Eugoné <[email protected]> | ||
|
@@ -12,7 +13,16 @@ class ConfigurableEnumTest extends TestCase | |
public function testConfigurability(): void | ||
{ | ||
$fooEnum = new ConfigurableEnum('foo', ['foo' => 'FOO', 'bar' => 'BAR']); | ||
$this->assertSame('foo', $fooEnum->getName()); | ||
$this->assertSame(['foo' => 'FOO', 'bar' => 'BAR'], $fooEnum->getChoices()); | ||
self::assertSame('foo', $fooEnum->getName()); | ||
self::assertSame(['foo' => 'FOO', 'bar' => 'BAR'], $fooEnum->getChoices()); | ||
self::assertSame('FOO', $fooEnum->getLabel('foo')); | ||
self::assertSame('BAR', $fooEnum->getLabel('bar')); | ||
} | ||
|
||
public function testLabelNotFound(): void | ||
{ | ||
$this->expectException(InvalidEnumValueException::class); | ||
$fooEnum = new ConfigurableEnum('foo', ['foo' => 'FOO', 'bar' => 'BAR']); | ||
$fooEnum->getLabel('unknown enum value'); | ||
} | ||
} |
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
Oops, something went wrong.