Skip to content

Commit b189bb3

Browse files
authored
Allow Enum base class to deduce enum class using static::class (#45)
1 parent bbd1192 commit b189bb3

17 files changed

+95
-51
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class StatusEnum extends Enum
5656
{
5757
public function __construct()
5858
{
59-
parent::__construct(__CLASS__, ['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']);
59+
parent::__construct(['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']);
6060
}
6161
}
6262
```
@@ -153,7 +153,7 @@ class StatusEnum extends TranslatedEnum
153153
{
154154
public function __construct(TranslatorInterface $translator)
155155
{
156-
parent::__construct(__CLASS__, ['new', 'validated', 'disabled'], $translator, 'status.%s');
156+
parent::__construct(['new', 'validated', 'disabled'], $translator, 'status.%s');
157157
}
158158
}
159159
```

docs/creating-enum.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StatusEnum extends Enum
1717
{
1818
public function __construct()
1919
{
20-
parent::__construct(__CLASS__, ['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']);
20+
parent::__construct(['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']);
2121
}
2222
}
2323
```
@@ -41,7 +41,7 @@ class StatusEnum extends ConstantListEnum
4141
{
4242
public function __construct()
4343
{
44-
parent::__construct(Member::class . '::STATUS_*', __CLASS__);
44+
parent::__construct(Member::class . '::STATUS_*');
4545
}
4646
}
4747
```

docs/creating-translated-enum.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class StatusEnum extends TranslatedEnum
1818
{
1919
public function __construct(TranslatorInterface $translator)
2020
{
21-
parent::__construct(__CLASS__, ['new', 'validated', 'disabled'], $translator, 'status.%s');
21+
parent::__construct(['new', 'validated', 'disabled'], $translator, 'status.%s');
2222
}
2323
}
2424
```
@@ -43,7 +43,7 @@ class StatusEnum extends ConstantListTranslatedEnum
4343
{
4444
public function __construct(TranslatorInterface $translator)
4545
{
46-
parent::__construct(__CLASS__, Member::class . '::STATUS_*', $translator, 'status.%s');
46+
parent::__construct(Member::class . '::STATUS_*', $translator, 'status.%s');
4747
}
4848
}
4949
```

docs/migrating-from-symfony-standard.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class MemberStatusEnum extends ConstantListTranslatedEnum
144144
{
145145
public function __construct(TranslatorInterface $translator)
146146
{
147-
parent::__construct(__CLASS__, Member::class . '::STATUS_*', $translator, 'member.status.%s');
147+
parent::__construct(Member::class . '::STATUS_*', $translator, 'member.status.%s');
148148
}
149149
}
150150
```
@@ -162,7 +162,7 @@ class MemberSubscriptionEnum extends ConstantListTranslatedEnum
162162
{
163163
public function __construct(TranslatorInterface $translator)
164164
{
165-
parent::__construct(__CLASS__, Member::class . '::SUBSCRIBE_*', $translator, 'member.subscription.%s');
165+
parent::__construct(Member::class . '::SUBSCRIBE_*', $translator, 'member.subscription.%s');
166166
}
167167
}
168168
```

src/ConstantListEnum.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
class ConstantListEnum extends Enum
1111
{
1212
/**
13-
* @param string $constantsPattern
14-
* @param string $name
13+
* @param string $constantsPattern
14+
* @param string|null $name
1515
*/
16-
public function __construct(string $constantsPattern, string $name)
16+
public function __construct(string $constantsPattern, ?string $name = null)
1717
{
1818
$values = ConstantExtractor::extract($constantsPattern);
19-
parent::__construct($name, array_combine($values, $values));
19+
parent::__construct(array_combine($values, $values), $name);
2020
}
2121
}

src/ConstantListTranslatedEnum.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@
1212
class ConstantListTranslatedEnum extends TranslatedEnum
1313
{
1414
/**
15-
* @param string $name
1615
* @param string $constantsPattern
1716
* @param TranslatorInterface $translator
1817
* @param string $transPattern
1918
* @param string $transDomain
19+
* @param string|null $name
2020
*/
2121
public function __construct(
22-
string $name,
2322
string $constantsPattern,
2423
TranslatorInterface $translator,
2524
string $transPattern,
26-
string $transDomain = 'messages'
25+
string $transDomain = 'messages',
26+
?string $name = null
2727
) {
2828
parent::__construct(
29-
$name,
3029
ConstantExtractor::extract($constantsPattern),
3130
$translator,
3231
$transPattern,
33-
$transDomain
32+
$transDomain,
33+
$name
3434
);
3535
}
3636
}

src/Enum.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Yokai\EnumBundle;
66

7-
use LogicException;
87
use Yokai\EnumBundle\Exception\InvalidArgumentException;
8+
use Yokai\EnumBundle\Exception\LogicException;
99

1010
/**
1111
* @author Yann Eugoné <[email protected]>
@@ -23,19 +23,34 @@ class Enum implements EnumInterface
2323
private $choices;
2424

2525
/**
26-
* @param string $name
27-
* @param array<string, mixed>|null $choices
26+
* @param array<string, mixed>|null $choices Allowed to be null if you are extending this class
27+
* and you have overridden the "Enum::build" method.
28+
* @param string|null $name Allowed to be null if you are extending this class
29+
* and you want the FQCN to be the name.
2830
*/
29-
public function __construct(string $name, ?array $choices)
31+
public function __construct(?array $choices, ?string $name = null)
3032
{
3133
if (__CLASS__ === static::class && $choices === null) {
3234
throw new LogicException(
33-
'When using ' . __CLASS__ . ' directly, $choices argument in __construct method cannot be null'
35+
'When using ' . __CLASS__ . ' directly, $choices argument in ' . __FUNCTION__ . ' method cannot be null'
3436
);
3537
}
3638

37-
$this->name = $name;
3839
$this->choices = $choices;
40+
41+
if ($name === null) {
42+
$name = static::class;
43+
if (
44+
\strpos($name, 'Yokai\\EnumBundle\\') === 0 // using FQCN as name is only allowed for other namespaces
45+
&& \strpos($name, 'Yokai\\EnumBundle\\Tests\\') !== 0 // except for our tests
46+
) {
47+
throw new LogicException(
48+
'When using ' . static::class . ', $name argument in ' . __METHOD__ . ' method cannot be null'
49+
);
50+
}
51+
}
52+
53+
$this->name = $name;
3954
}
4055

4156
/**
@@ -86,7 +101,7 @@ public function getName(): string
86101
*/
87102
protected function build(): array
88103
{
89-
throw new \LogicException(static::class . '::' . __FUNCTION__ . ' should have been overridden.');
104+
throw new LogicException(static::class . '::' . __FUNCTION__ . ' should have been overridden.');
90105
}
91106

92107
private function init(): void

src/TranslatedEnum.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ class TranslatedEnum extends Enum
3333
private $transDomain;
3434

3535
/**
36-
* @param string $name
3736
* @param array<int|string, mixed> $values
3837
* @param TranslatorInterface $translator
3938
* @param string $transPattern
4039
* @param string $transDomain
40+
* @param string|null $name
4141
*
4242
* @throws LogicException
4343
*/
4444
public function __construct(
45-
string $name,
4645
array $values,
4746
TranslatorInterface $translator,
4847
string $transPattern,
49-
string $transDomain = 'messages'
48+
string $transDomain = 'messages',
49+
?string $name = null
5050
) {
5151
if (false === strpos($transPattern, '%s')) {
5252
throw LogicException::placeholderRequired($transPattern);
@@ -57,7 +57,7 @@ public function __construct(
5757
$this->transPattern = $transPattern;
5858
$this->transDomain = $transDomain;
5959

60-
parent::__construct($name, null);
60+
parent::__construct(null, $name);
6161
}
6262

6363
/**

tests/ConstantListEnumTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Yokai\EnumBundle\ConstantListEnum;
99
use Yokai\EnumBundle\Exception\InvalidArgumentException;
10+
use Yokai\EnumBundle\Exception\LogicException;
1011
use Yokai\EnumBundle\Tests\Fixtures\Vehicle;
1112

1213
/**
@@ -52,6 +53,12 @@ public function testVehicleEnums(): void
5253
self::assertSame('toyota', $brand->getLabel('toyota'));
5354
}
5455

56+
public function testEnumMustHaveName(): void
57+
{
58+
$this->expectException(LogicException::class);
59+
new ConstantListEnum(Vehicle::class . '::TYPE_*', null);
60+
}
61+
5562
public function testLabelNotFound(): void
5663
{
5764
$this->expectException(InvalidArgumentException::class);

tests/ConstantListTranslatedEnumTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Contracts\Translation\TranslatorInterface;
99
use Yokai\EnumBundle\ConstantListTranslatedEnum;
1010
use Yokai\EnumBundle\Exception\InvalidArgumentException;
11+
use Yokai\EnumBundle\Exception\LogicException;
1112
use Yokai\EnumBundle\Tests\Fixtures\Vehicle;
1213

1314
/**
@@ -17,7 +18,7 @@ class ConstantListTranslatedEnumTest extends TestCase
1718
{
1819
public function getEnum(string $pattern, string $name, TranslatorInterface $translator): ConstantListTranslatedEnum
1920
{
20-
return new ConstantListTranslatedEnum($name, $pattern, $translator, $name . '.%s');
21+
return new ConstantListTranslatedEnum($pattern, $translator, $name . '.%s', 'messages', $name);
2122
}
2223

2324
public function testVehicleEnums(): void
@@ -64,6 +65,12 @@ public function testVehicleEnums(): void
6465
self::assertSame('Toyota', $brand->getLabel('toyota'));
6566
}
6667

68+
public function testEnumMustHaveName(): void
69+
{
70+
$this->expectException(LogicException::class);
71+
new ConstantListTranslatedEnum(Vehicle::class . '::TYPE_*', new Translator([]), 'vehicle.type.%s');
72+
}
73+
6774
public function testLabelNotFound(): void
6875
{
6976
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)