Skip to content

Commit b0c3944

Browse files
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]>
1 parent 89143e5 commit b0c3944

22 files changed

+234
-59
lines changed

doc/declaring-enum.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Create a new class, implement both `getName` & `getChoices` methods.
3030
namespace App\Enum;
3131
3232
use Yokai\EnumBundle\EnumInterface;
33+
use Yokai\EnumBundle\Exception\InvalidEnumValueException;
3334
3435
class GenderEnum implements EnumInterface
3536
{
@@ -42,6 +43,15 @@ class GenderEnum implements EnumInterface
4243
{
4344
return ['m' => 'Male', 'f' => 'Female'];
4445
}
46+
47+
public function getLabel(string $value): string
48+
{
49+
if (!isset($this->getChoices()[$value])) {
50+
throw InvalidEnumValueException::create($this, $value);
51+
}
52+
53+
return $this->getChoices()[$value];
54+
}
4555
}
4656
```
4757

@@ -64,11 +74,13 @@ Create a new class, use `EnumWithClassAsNameTrait` trait and implement `getChoic
6474
namespace App\Enum;
6575
6676
use Yokai\EnumBundle\EnumInterface;
77+
use Yokai\EnumBundle\EnumLabelTrait;
6778
use Yokai\EnumBundle\EnumWithClassAsNameTrait;
6879
6980
class GenderEnum implements EnumInterface
7081
{
7182
use EnumWithClassAsNameTrait;
83+
use EnumLabelTrait;
7284
7385
public function getChoices(): array
7486
{

src/AbstractTranslatedEnum.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
abstract class AbstractTranslatedEnum implements EnumInterface
1414
{
15+
use EnumLabelTrait;
16+
1517
/**
1618
* @var TranslatorInterface
1719
*/
@@ -27,6 +29,11 @@ abstract class AbstractTranslatedEnum implements EnumInterface
2729
*/
2830
private $transDomain = 'messages';
2931

32+
/**
33+
* @var array|null
34+
*/
35+
private $choices = null;
36+
3037
/**
3138
* @param TranslatorInterface $translator
3239
* @param string $transPattern
@@ -46,19 +53,23 @@ public function __construct(TranslatorInterface $translator, string $transPatter
4653
*/
4754
public function getChoices(): array
4855
{
49-
return array_combine(
50-
$this->getValues(),
51-
array_map(
52-
function (string $value): string {
53-
return $this->translator->trans(
54-
sprintf($this->transPattern, $value),
55-
[],
56-
$this->transDomain
57-
);
58-
},
59-
$this->getValues()
60-
)
61-
);
56+
if ($this->choices === null) {
57+
$this->choices = array_combine(
58+
$this->getValues(),
59+
array_map(
60+
function (string $value): string {
61+
return $this->translator->trans(
62+
sprintf($this->transPattern, $value),
63+
[],
64+
$this->transDomain
65+
);
66+
},
67+
$this->getValues()
68+
)
69+
);
70+
}
71+
72+
return $this->choices;
6273
}
6374

6475
/**

src/ConfigurableEnum.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
class ConfigurableEnum implements EnumInterface
1111
{
12+
use EnumLabelTrait;
13+
1214
/**
1315
* @var string
1416
*/

src/EnumInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,31 @@
66

77
/**
88
* @author Yann Eugoné <[email protected]>
9+
*
10+
* NEXT_MAJOR: Add all these methods to the interface by uncommenting them.
11+
*
12+
* @method string getLabel(string $value)
913
*/
1014
interface EnumInterface
1115
{
1216
/**
17+
* Returns enum choices (value as keys, labels as values)
18+
*
1319
* @return array
1420
*/
1521
public function getChoices(): array;
1622

1723
/**
24+
* Returns enum identifier (must be unique across your app).
25+
*
1826
* @return string
1927
*/
2028
public function getName(): string;
29+
30+
/**
31+
* NEXT_MAJOR: uncomment this method
32+
*
33+
* @return string
34+
*/
35+
// public function getLabel(string $value): string;
2136
}

src/EnumLabelTrait.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle;
4+
5+
use Yokai\EnumBundle\Exception\InvalidEnumValueException;
6+
7+
trait EnumLabelTrait
8+
{
9+
/**
10+
* @inheritdoc
11+
*/
12+
public function getLabel(string $value): string
13+
{
14+
$choices = $this->getChoices();
15+
if (!isset($choices[$value])) {
16+
throw InvalidEnumValueException::create($this, $value);
17+
}
18+
19+
return $choices[$value];
20+
}
21+
}

src/Exception/CannotExtractConstantsException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @author Yann Eugoné <[email protected]>
1111
*/
12-
class CannotExtractConstantsException extends InvalidArgumentException
12+
class CannotExtractConstantsException extends InvalidArgumentException implements ExceptionInterface
1313
{
1414
public static function invalidPattern(string $pattern): self
1515
{

src/Exception/DuplicatedEnumException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @author Yann Eugoné <[email protected]>
1111
*/
12-
class DuplicatedEnumException extends BadMethodCallException
12+
class DuplicatedEnumException extends BadMethodCallException implements ExceptionInterface
1313
{
1414
/**
1515
* @param string $name

src/Exception/ExceptionInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Exception;
4+
5+
use Throwable;
6+
7+
interface ExceptionInterface extends Throwable
8+
{
9+
}

src/Exception/InvalidEnumException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @author Yann Eugoné <[email protected]>
1111
*/
12-
class InvalidEnumException extends DomainException
12+
class InvalidEnumException extends DomainException implements ExceptionInterface
1313
{
1414
/**
1515
* @param string $name
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Exception;
4+
5+
use InvalidArgumentException;
6+
use Yokai\EnumBundle\EnumInterface;
7+
8+
final class InvalidEnumValueException extends InvalidArgumentException implements ExceptionInterface
9+
{
10+
public static function create(EnumInterface $enum, string $value): self
11+
{
12+
return new self(
13+
\sprintf('Enum "%s" does not have "%s" value.', $enum->getName(), $value)
14+
);
15+
}
16+
}

0 commit comments

Comments
 (0)