Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita84a01b

Browse files
[Config] Allow using an enum FQCN withEnumNode
1 parent74df71a commita84a01b

File tree

10 files changed

+103
-0
lines changed

10 files changed

+103
-0
lines changed

‎src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add`#[WhenNot]` attribute to prevent service from being registered in a specific environment
8+
* Allow using an enum FQCN with`EnumNode`
89

910
7.1
1011
---

‎src/Symfony/Component/Config/Definition/Builder/EnumNodeDefinition.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ public function values(array $values): static
3636
return$this;
3737
}
3838

39+
publicfunctionenumClass(string$enumClass):static
40+
{
41+
if (!enum_exists($enumClass)) {
42+
thrownew \InvalidArgumentException(sprintf('The enum class "%s" does not exist.',$enumClass));
43+
}
44+
45+
$this->values = [$enumClass];
46+
47+
return$this;
48+
}
49+
3950
/**
4051
* Instantiate a Node.
4152
*

‎src/Symfony/Component/Config/Definition/EnumNode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@
2121
class EnumNodeextends ScalarNode
2222
{
2323
privatearray$values;
24+
private ?string$enumFqcn =null;
2425

2526
publicfunction__construct(?string$name, ?NodeInterface$parent =null,array$values = [],string$pathSeparator = BaseNode::DEFAULT_PATH_SEPARATOR)
2627
{
2728
if (!$values) {
2829
thrownew \InvalidArgumentException('$values must contain at least one element.');
2930
}
3031

32+
if (1 ===\count($values) &&\is_string($values[0]) &&\enum_exists($enumClass =$values[0]) &&\is_a($enumClass, \BackedEnum::class,true)) {
33+
$values =$enumClass::cases();
34+
$this->enumFqcn =$enumClass;
35+
}
36+
3137
foreach ($valuesas$value) {
3238
if (null ===$value ||\is_scalar($value)) {
3339
continue;
@@ -78,6 +84,10 @@ protected function finalizeValue(mixed $value): mixed
7884
{
7985
$value =parent::finalizeValue($value);
8086

87+
if ($this->enumFqcn && !$valueinstanceof \BackedEnum) {
88+
$value =$this->enumFqcn::tryFrom($value);
89+
}
90+
8191
if (!\in_array($value,$this->values,true)) {
8292
$ex =newInvalidConfigurationException(\sprintf('The value %s is not allowed for path "%s". Permissible values: %s',json_encode($value),$this->getPath(),$this->getPermissibleValues(',')));
8393
$ex->setPath($this->getPath());

‎src/Symfony/Component/Config/Tests/Builder/Fixtures/PrimitiveTypes.php

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

1414
useSymfony\Component\Config\Definition\Builder\TreeBuilder;
1515
useSymfony\Component\Config\Definition\ConfigurationInterface;
16+
useSymfony\Component\Config\Tests\Fixtures\BackedTestEnum;
1617
useSymfony\Component\Config\Tests\Fixtures\TestEnum;
1718

1819
class PrimitiveTypesimplements ConfigurationInterface
@@ -25,6 +26,7 @@ public function getConfigTreeBuilder(): TreeBuilder
2526
->children()
2627
->booleanNode('boolean_node')->end()
2728
->enumNode('enum_node')->values(['foo','bar','baz', TestEnum::Bar])->end()
29+
->enumNode('fqcn_enum_node')->enumClass(BackedTestEnum::class)->end()
2830
->floatNode('float_node')->end()
2931
->integerNode('integer_node')->end()
3032
->scalarNode('scalar_node')->end()

‎src/Symfony/Component/Config/Tests/Builder/Fixtures/PrimitiveTypes/Symfony/Config/PrimitiveTypesConfig.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class PrimitiveTypesConfig implements \Symfony\Component\Config\Builder\ConfigBu
1212
{
1313
private$booleanNode;
1414
private$enumNode;
15+
private$fqcnEnumNode;
1516
private$floatNode;
1617
private$integerNode;
1718
private$scalarNode;
@@ -44,6 +45,19 @@ public function enumNode($value): static
4445
return$this;
4546
}
4647

48+
/**
49+
* @default null
50+
* @param ParamConfigurator|\Symfony\Component\Config\Tests\Fixtures\BackedTestEnum::Foo|\Symfony\Component\Config\Tests\Fixtures\BackedTestEnum::Bar $value
51+
* @return $this
52+
*/
53+
publicfunctionfqcnEnumNode($value):static
54+
{
55+
$this->_usedProperties['fqcnEnumNode'] =true;
56+
$this->fqcnEnumNode =$value;
57+
58+
return$this;
59+
}
60+
4761
/**
4862
* @default null
4963
* @param ParamConfigurator|float $value
@@ -115,6 +129,12 @@ public function __construct(array $value = [])
115129
unset($value['enum_node']);
116130
}
117131

132+
if (array_key_exists('fqcn_enum_node',$value)) {
133+
$this->_usedProperties['fqcnEnumNode'] =true;
134+
$this->fqcnEnumNode =$value['fqcn_enum_node'];
135+
unset($value['fqcn_enum_node']);
136+
}
137+
118138
if (array_key_exists('float_node',$value)) {
119139
$this->_usedProperties['floatNode'] =true;
120140
$this->floatNode =$value['float_node'];
@@ -153,6 +173,9 @@ public function toArray(): array
153173
if (isset($this->_usedProperties['enumNode'])) {
154174
$output['enum_node'] =$this->enumNode;
155175
}
176+
if (isset($this->_usedProperties['fqcnEnumNode'])) {
177+
$output['fqcn_enum_node'] =$this->fqcnEnumNode;
178+
}
156179
if (isset($this->_usedProperties['floatNode'])) {
157180
$output['float_node'] =$this->floatNode;
158181
}

‎src/Symfony/Component/Config/Tests/Definition/Dumper/XmlReferenceDumperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private function getConfigurationAsString()
4242
<!-- scalar-deprecated-with-message: Deprecated (Since vendor/package 1.1: Deprecation custom message for "scalar_deprecated_with_message" at "acme_root") -->
4343
<!-- enum-with-default: One of "this"; "that" -->
4444
<!-- enum: One of "this"; "that"; Symfony\Component\Config\Tests\Fixtures\TestEnum::Ccc -->
45+
<!-- enum-with-class: One of Symfony\Component\Config\Tests\Fixtures\BackedTestEnum::Foo; Symfony\Component\Config\Tests\Fixtures\BackedTestEnum::Bar -->
4546
<!-- variable: Example: foo, bar -->
4647
<config
4748
boolean="true"
@@ -58,6 +59,7 @@ private function getConfigurationAsString()
5859
node-with-a-looong-name=""
5960
enum-with-default="this"
6061
enum=""
62+
enum-with-class=""
6163
variable=""
6264
custom-node="true"
6365
>

‎src/Symfony/Component/Config/Tests/Definition/Dumper/YamlReferenceDumperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ private function getConfigurationAsString(): string
103103
node_with_a_looong_name: ~
104104
enum_with_default: this # One of "this"; "that"
105105
enum: ~ # One of "this"; "that"; Symfony\Component\Config\Tests\Fixtures\TestEnum::Ccc
106+
enum_with_class: ~ # One of Symfony\Component\Config\Tests\Fixtures\BackedTestEnum::Foo; Symfony\Component\Config\Tests\Fixtures\BackedTestEnum::Bar
106107
107108
# some info
108109
array:

‎src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
usePHPUnit\Framework\TestCase;
1515
useSymfony\Component\Config\Definition\EnumNode;
1616
useSymfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17+
useSymfony\Component\Config\Tests\Fixtures\BackedTestEnum;
1718
useSymfony\Component\Config\Tests\Fixtures\TestEnum;
1819
useSymfony\Component\Config\Tests\Fixtures\TestEnum2;
1920

@@ -61,6 +62,47 @@ public function testFinalizeWithInvalidValue()
6162
$node->finalize('foobar');
6263
}
6364

65+
publicfunctiontestFinalizeWithOnlyUnitEnumFqcnDoesntDoAnythingSpecial()
66+
{
67+
$node =newEnumNode('foo',null, [TestEnum::class]);
68+
69+
$this->expectException(InvalidConfigurationException::class);
70+
$this->expectExceptionMessage('The value "foobar" is not allowed for path "foo". Permissible values: "Symfony\\\\Component\\\\Config\\\\Tests\\\\Fixtures\\\\TestEnum"');
71+
72+
$node->finalize('foobar');
73+
}
74+
75+
publicfunctiontestFinalizeWithEnumFqcn()
76+
{
77+
$node =newEnumNode('foo',null, [BackedTestEnum::class]);
78+
79+
$this->assertSame(BackedTestEnum::Foo,$node->finalize(BackedTestEnum::Foo));
80+
}
81+
82+
publicfunctiontestFinalizeWithEnumFqcnAndAnotherScalar()
83+
{
84+
$node =newEnumNode('foo',null, [BackedTestEnum::class,'another_string']);
85+
86+
$this->assertSame(BackedTestEnum::class,$node->finalize(BackedTestEnum::class));
87+
}
88+
89+
publicfunctiontestFinalizeWithEnumFqcnWorksWithPlainString()
90+
{
91+
$node =newEnumNode('foo',null, [BackedTestEnum::class]);
92+
93+
$this->assertSame(BackedTestEnum::Foo,$node->finalize('foo'));
94+
}
95+
96+
publicfunctiontestFinalizeWithEnumFqcnWithWrongCase()
97+
{
98+
$node =newEnumNode('foo',null, [BackedTestEnum::class]);
99+
100+
$this->expectException(InvalidConfigurationException::class);
101+
$this->expectExceptionMessage('The value null is not allowed for path "foo". Permissible values: Symfony\Component\Config\Tests\Fixtures\BackedTestEnum::Foo, Symfony\Component\Config\Tests\Fixtures\BackedTestEnum::Bar');
102+
103+
$node->finalize('qux');
104+
}
105+
64106
publicfunctiontestWithPlaceHolderWithValidValue()
65107
{
66108
$node =newEnumNode('cookie_samesite',null, ['lax','strict','none']);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Config\Tests\Fixtures;
4+
5+
enum BackedTestEnum:string
6+
{
7+
case Foo ='foo';
8+
case Bar ='bar';
9+
}

‎src/Symfony/Component/Config/Tests/Fixtures/Configuration/ExampleConfiguration.php

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

1414
useSymfony\Component\Config\Definition\Builder\TreeBuilder;
1515
useSymfony\Component\Config\Definition\ConfigurationInterface;
16+
useSymfony\Component\Config\Tests\Fixtures\BackedTestEnum;
1617
useSymfony\Component\Config\Tests\Fixtures\TestEnum;
1718

1819
class ExampleConfigurationimplements ConfigurationInterface
@@ -40,6 +41,7 @@ public function getConfigTreeBuilder(): TreeBuilder
4041
->scalarNode('node_with_a_looong_name')->end()
4142
->enumNode('enum_with_default')->values(['this','that'])->defaultValue('this')->end()
4243
->enumNode('enum')->values(['this','that', TestEnum::Ccc])->end()
44+
->enumNode('enum_with_class')->enumClass(BackedTestEnum::class)->end()
4345
->arrayNode('array')
4446
->info('some info')
4547
->canBeUnset()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp