Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Symfony/Component/Config/Definition/EnumNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ public function getEnumFqcn(): ?string
public function getPermissibleValues(string $separator, bool $trim = true): string
{
if (is_subclass_of($this->enumFqcn, \BackedEnum::class)) {
return implode($separator, array_column($this->enumFqcn::cases(), 'value'));
if (!$trim) {
return 'value-of<\\'.$this->enumFqcn.'>'.$separator.'\\'.$this->enumFqcn;
}

$values = array_column($this->enumFqcn::cases(), 'value');

return implode($separator, array_map(static fn ($value) => json_encode($value, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE), $values));
}

return implode($separator, array_unique(array_map(static function ($value) use ($trim) {
if (!$value instanceof \UnitEnum) {
return json_encode($value);
return json_encode($value, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_PRESERVE_ZERO_FRACTION);
}

return $trim ? ltrim(var_export($value, true), '\\') : var_export($value, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function enumNode($value): static

/**
* @default null
* @param ParamConfigurator|\Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum::Foo|\Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum::Bar $value
* @param ParamConfigurator|\Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum::Foo|\Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum::BarBaz $value
* @return $this
* @deprecated since Symfony 7.4
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Config\Definition\StringNode;
use Symfony\Component\Config\Definition\VariableNode;
use Symfony\Component\Config\Tests\Fixtures\IntegerBackedTestEnum;
use Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum;

class ArrayShapeGeneratorTest extends TestCase
{
Expand Down Expand Up @@ -52,6 +54,8 @@ public static function provideNodes(): iterable

yield [$nullableBooleanNode, 'bool|null'];
yield [new EnumNode('node', values: ['a', 'b']), '"a"|"b"'];
yield [new EnumNode('node', enumFqcn: StringBackedTestEnum::class), 'value-of<\Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum>|\Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum'];
yield [new EnumNode('node', enumFqcn: IntegerBackedTestEnum::class), 'value-of<\Symfony\Component\Config\Tests\Fixtures\IntegerBackedTestEnum>|\Symfony\Component\Config\Tests\Fixtures\IntegerBackedTestEnum'];
yield [new ScalarNode('node'), 'scalar|null'];
yield [new VariableNode('node'), 'mixed'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private function getConfigurationAsString()
<!-- scalar-deprecated-with-message: Deprecated (Since vendor/package 1.1: Deprecation custom message for "scalar_deprecated_with_message" at "acme_root") -->
<!-- enum-with-default: One of "this"; "that" -->
<!-- enum: One of "this"; "that"; Symfony\Component\Config\Tests\Fixtures\TestEnum::Ccc -->
<!-- enum-with-class: One of foo; bar -->
<!-- enum-with-class: One of "foo"; "bar baz" -->
<!-- unit-enum-with-class: One of Symfony\Component\Config\Tests\Fixtures\TestEnum::Foo; Symfony\Component\Config\Tests\Fixtures\TestEnum::Bar; Symfony\Component\Config\Tests\Fixtures\TestEnum::Ccc -->
<!-- variable: Example: foo, bar -->
<config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private function getConfigurationAsString(): string
node_with_a_looong_name: ~
enum_with_default: this # One of "this"; "that"
enum: ~ # One of "this"; "that"; Symfony\Component\Config\Tests\Fixtures\TestEnum::Ccc
enum_with_class: ~ # One of foo; bar
enum_with_class: ~ # One of "foo"; "bar baz"
unit_enum_with_class: ~ # One of Symfony\Component\Config\Tests\Fixtures\TestEnum::Foo; Symfony\Component\Config\Tests\Fixtures\TestEnum::Bar; Symfony\Component\Config\Tests\Fixtures\TestEnum::Ccc

# some info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testFinalizeWithStringEnumFqcnWithWrongCase()
$node = new EnumNode('foo', null, enumFqcn: StringBackedTestEnum::class);

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('The value "qux" is not allowed for path "foo". Permissible values: foo, bar (cases of the "Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum" enum)');
$this->expectExceptionMessage('The value "qux" is not allowed for path "foo". Permissible values: "foo", "bar baz" (cases of the "Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum" enum)');

$node->finalize('qux');
}
Expand All @@ -148,7 +148,7 @@ public function testFinalizeWithStringEnumFqcnWithIntegerCase()
$node = new EnumNode('foo', null, enumFqcn: StringBackedTestEnum::class);

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('The value 1 is not allowed for path "foo". Permissible values: foo, bar (cases of the "Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum" enum).');
$this->expectExceptionMessage('The value 1 is not allowed for path "foo". Permissible values: "foo", "bar baz" (cases of the "Symfony\Component\Config\Tests\Fixtures\StringBackedTestEnum" enum).');

$node->finalize(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
enum StringBackedTestEnum: string
{
case Foo = 'foo';
case Bar = 'bar';
case BarBaz = 'bar baz';
}
Loading