Skip to content

Commit b8c72a6

Browse files
authored
Merge pull request #32 from yokai-php/enum-autowiring
Make ConstantExtractor a static class to easy enum autowiring
2 parents 3fb0d06 + 3c9a81e commit b8c72a6

9 files changed

+58
-75
lines changed

doc/declaring-enum.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ There is a lot of way to create and declare such classes.
88
> **Note :** If you wish to declare translation based enum,
99
> please see [dedicated documentation](declaring-translated-enum.md)
1010
11+
Services declarations assume that your services files has the following defaults section :
12+
13+
```yaml
14+
services:
15+
_defaults:
16+
public: false
17+
autowire: true
18+
autconfigure: true
19+
```
20+
1121
1222
The classic way
1323
---------------
@@ -35,14 +45,11 @@ class GenderEnum implements EnumInterface
3545
}
3646
```
3747

38-
Define an enum service for it.
48+
Define an enum service for it (optional if you are using the default Symfony services file).
3949

4050
```yaml
4151
services:
42-
enum.member.gender:
43-
class: 'App\Enum\GenderEnum'
44-
public: false
45-
tags: ['enum']
52+
App\Enum\GenderEnum: ~
4653
```
4754

4855

@@ -70,14 +77,11 @@ class GenderEnum implements EnumInterface
7077
}
7178
```
7279

73-
Define an enum service for it.
80+
Define an enum service for it (optional if you are using the default Symfony services file).
7481

7582
```yaml
7683
services:
77-
enum.member.gender:
78-
class: 'App\Enum\GenderEnum'
79-
public: false
80-
tags: ['enum']
84+
App\Enum\GenderEnum: ~
8185
```
8286

8387

@@ -89,13 +93,12 @@ No need for a class, just use the `ConfigurableEnum` class and define a new enum
8993
```yaml
9094
services:
9195
enum.member.gender:
92-
class: 'Yokai\EnumBundle\ConfigurableEnum'
93-
public: false
94-
tags: ['enum']
96+
class: Yokai\EnumBundle\ConfigurableEnum
9597
arguments:
96-
- "gender"
97-
- m: 'Male'
98-
f: 'Female'
98+
$name: 'gender'
99+
$choices:
100+
m: 'Male'
101+
f: 'Female'
99102
```
100103

101104

@@ -108,11 +111,8 @@ No need for a class, just use the `ConstantListEnum` class and define a new enum
108111
```yaml
109112
services:
110113
enum.member.gender:
111-
class: 'Yokai\EnumBundle\ConstantListEnum'
112-
public: false
113-
tags: ['enum']
114+
class: Yokai\EnumBundle\ConstantListEnum
114115
arguments:
115-
- '@enum.constant_extractor'
116-
- 'App\\Model\\Person::GENDER_*'
117-
- "gender"
116+
$constantsPattern: 'App\\Model\\Person::GENDER_*'
117+
$name: 'gender'
118118
```

doc/declaring-translated-enum.md

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ There is a lot of way to create and declare such classes.
88
> **Note :** It is pretty much like [declaring classic enums](declaring-enum.md),
99
> but with a dependency to `symfony/translator`.
1010
11+
Services declarations assume that your services files has the following defaults section :
12+
13+
```yaml
14+
services:
15+
_defaults:
16+
public: false
17+
autowire: true
18+
autconfigure: true
19+
```
20+
1121
1222
The classic way
1323
---------------
@@ -41,16 +51,11 @@ class GenderEnum extends AbstractTranslatedEnum
4151
}
4252
```
4353

44-
Define an enum service for it.
54+
Define an enum service for it (optional if you are using the default Symfony services file).
4555

4656
```yaml
4757
services:
48-
enum.member.gender:
49-
class: 'App\Enum\GenderEnum'
50-
public: false
51-
tags: ['enum']
52-
arguments:
53-
- "@translator"
58+
App\Enum\GenderEnum: ~
5459
```
5560

5661

@@ -84,16 +89,11 @@ class GenderEnum extends AbstractTranslatedEnum
8489
}
8590
```
8691

87-
Define an enum service for it.
92+
Define an enum service for it (optional if you are using the default Symfony services file).
8893

8994
```yaml
9095
services:
91-
enum.member.gender:
92-
class: 'App\Enum\GenderEnum'
93-
public: false
94-
tags: ['enum']
95-
arguments:
96-
- "@translator"
96+
App\Enum\GenderEnum: ~
9797
```
9898

9999

@@ -105,14 +105,11 @@ No need for a class, just use the `ConfigurableTranslatedEnum` class and define
105105
```yaml
106106
services:
107107
enum.member.gender:
108-
class: 'Yokai\EnumBundle\ConfigurableTranslatedEnum'
109-
public: false
110-
tags: ['enum']
108+
class: Yokai\EnumBundle\ConfigurableTranslatedEnum
111109
arguments:
112-
- "@translator"
113-
- "enum.gender.%s"
114-
- "gender"
115-
- ['m', 'f']
110+
$transPattern: 'enum.gender.%s'
111+
$name: 'gender'
112+
$values: ['m', 'f']
116113
```
117114

118115
The configurable way extracting constant list
@@ -124,13 +121,9 @@ No need for a class, just use the `ConstantListTranslatedEnum` class and define
124121
```yaml
125122
services:
126123
enum.member.gender:
127-
class: 'Yokai\EnumBundle\ConstantListTranslatedEnum'
128-
public: false
129-
tags: ['enum']
124+
class: Yokai\EnumBundle\ConstantListTranslatedEnum
130125
arguments:
131-
- '@yokai_enum.constant_extractor'
132-
- 'App\\Model\\Person::GENDER_*'
133-
- "@translator"
134-
- "enum.gender.%s"
135-
- "gender"
126+
$constantsPattern: 'App\\Model\\Person::GENDER_*'
127+
$transPattern: 'enum.gender.%s'
128+
$name: 'gender'
136129
```

src/ConstantExtractor.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
*/
1616
class ConstantExtractor
1717
{
18-
public function extract(string $pattern): array
18+
public static function extract(string $pattern): array
1919
{
20-
[$class, $patternRegex] = $this->explode($pattern);
20+
[$class, $patternRegex] = self::explode($pattern);
2121

22-
return $this->filter(
23-
$this->publicConstants($class),
22+
return self::filter(
23+
self::publicConstants($class),
2424
$patternRegex,
2525
$pattern
2626
);
2727
}
2828

29-
private function filter(array $constants, string $regexp, string $pattern): array
29+
private static function filter(array $constants, string $regexp, string $pattern): array
3030
{
3131
$matchingNames = preg_grep($regexp, array_keys($constants));
3232

@@ -37,7 +37,7 @@ private function filter(array $constants, string $regexp, string $pattern): arra
3737
return array_values(array_intersect_key($constants, array_flip($matchingNames)));
3838
}
3939

40-
private function publicConstants(string $class): array
40+
private static function publicConstants(string $class): array
4141
{
4242
try {
4343
$constants = (new ReflectionClass($class))->getReflectionConstants();
@@ -61,7 +61,7 @@ private function publicConstants(string $class): array
6161
return $list;
6262
}
6363

64-
private function explode(string $pattern): array
64+
private static function explode(string $pattern): array
6565
{
6666
if (substr_count($pattern, '::') !== 1) {
6767
throw CannotExtractConstantsException::invalidPattern($pattern);

src/ConstantListEnum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class ConstantListEnum extends ConfigurableEnum
1212
/**
1313
* @inheritDoc
1414
*/
15-
public function __construct(ConstantExtractor $extractor, string $constantsPattern, string $name)
15+
public function __construct(string $constantsPattern, string $name)
1616
{
17-
$values = $extractor->extract($constantsPattern);
17+
$values = ConstantExtractor::extract($constantsPattern);
1818
parent::__construct($name, array_combine($values, $values));
1919
}
2020
}

src/ConstantListTranslatedEnum.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ class ConstantListTranslatedEnum extends ConfigurableTranslatedEnum
1515
* @inheritDoc
1616
*/
1717
public function __construct(
18-
ConstantExtractor $extractor,
1918
string $constantsPattern,
2019
TranslatorInterface $translator,
2120
string $transPattern,
2221
string $name
2322
) {
24-
parent::__construct($translator, $transPattern, $name, $extractor->extract($constantsPattern));
23+
parent::__construct($translator, $transPattern, $name, ConstantExtractor::extract($constantsPattern));
2524
}
2625
}

src/Resources/config/enum.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<services>
88
<service id="Yokai\EnumBundle\EnumRegistry" alias="yokai_enum.enum_registry"/>
99

10-
<service id="yokai_enum.constant_extractor" class="Yokai\EnumBundle\ConstantExtractor" public="false"/>
1110
<service id="yokai_enum.enum_registry" class="Yokai\EnumBundle\EnumRegistry"/>
1211
<service id="yokai_enum.abstract_translated_enum" class="Yokai\EnumBundle\AbstractTranslatedEnum" abstract="true">
1312
<argument type="service" id="translator"/>

tests/ConstantExtractorTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
*/
1212
class ConstantExtractorTest extends TestCase
1313
{
14-
public function getExtractor(): ConstantExtractor
15-
{
16-
return new ConstantExtractor();
17-
}
18-
1914
/**
2015
* @dataProvider malformed
2116
*/
@@ -24,7 +19,7 @@ public function testExtractMalformedPattern(string $pattern, string $exceptionMe
2419
$this->expectException(CannotExtractConstantsException::class);
2520
$this->expectExceptionMessageMatches($exceptionMessage);
2621

27-
$this->getExtractor()->extract($pattern);
22+
ConstantExtractor::extract($pattern);
2823
}
2924

3025
/**
@@ -35,15 +30,15 @@ public function testExtractEmpty(string $pattern, string $exceptionMessage): voi
3530
$this->expectException(CannotExtractConstantsException::class);
3631
$this->expectExceptionMessageMatches($exceptionMessage);
3732

38-
$this->getExtractor()->extract($pattern);
33+
ConstantExtractor::extract($pattern);
3934
}
4035

4136
/**
4237
* @dataProvider successful
4338
*/
4439
public function testExtractSuccessful(string $pattern, array $expectedList): void
4540
{
46-
self::assertEquals($expectedList, $this->getExtractor()->extract($pattern));
41+
self::assertEquals($expectedList, ConstantExtractor::extract($pattern));
4742
}
4843

4944
public function empty(): Generator

tests/ConstantListEnumTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Yokai\EnumBundle\Tests;
44

5-
use Yokai\EnumBundle\ConstantExtractor;
65
use Yokai\EnumBundle\ConstantListEnum;
76
use Yokai\EnumBundle\Tests\Fixtures\Vehicle;
87

@@ -13,7 +12,7 @@ class ConstantListEnumTest extends TestCase
1312
{
1413
public function getEnum(string $pattern, string $name): ConstantListEnum
1514
{
16-
return new ConstantListEnum(new ConstantExtractor(), $pattern, $name);
15+
return new ConstantListEnum($pattern, $name);
1716
}
1817

1918
public function testVehicleEnums(): void

tests/ConstantListTranslatedEnumTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Prophecy\Prophecy\ObjectProphecy;
66
use Symfony\Contracts\Translation\TranslatorInterface;
7-
use Yokai\EnumBundle\ConstantExtractor;
87
use Yokai\EnumBundle\ConstantListTranslatedEnum;
98
use Yokai\EnumBundle\Tests\Fixtures\Vehicle;
109

@@ -26,7 +25,6 @@ protected function setUp(): void
2625
public function getEnum(string $pattern, string $name): ConstantListTranslatedEnum
2726
{
2827
return new ConstantListTranslatedEnum(
29-
new ConstantExtractor(),
3028
$pattern,
3129
$this->translator->reveal(),
3230
$name.'.%s',

0 commit comments

Comments
 (0)