8000 Allow to define Enum nodes with 1 single element by javiereguiluz · Pull Request #15433 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Allow to define Enum nodes with 1 single element #15433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 20 additions & 0 deletions UPGRADE-2.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,23 @@ FrameworkBundle
session:
cookie_httponly: false
```

Config
------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed t 8000 o describe this comment to others. Learn more.

the UPGRADE file is about showing required/recommended changes because of BC breaks of deprecations.

this is just adding a new feature in a BC way, so a line in the changelog would be enough. Adding it in the UPGRADE file just makes the file less focused


The edge case of defining just one value for nodes of type Enum is now allowed:

```php
$rootNode
->children()
->enumNode('variable')
->values(array('value'))
->end()
->end()
;
```

Before: `InvalidArgumentException` (variable must contain at least two
distinct elements).
After: the code will work as expected and it will restrict the values of the
`variable` option to just `value`.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Config/Definition/EnumNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class EnumNode extends ScalarNode
public function __construct($name, NodeInterface $parent = null, array $values = array())
{
$values = array_unique($values);
if (count($values) <= 1) {
throw new \InvalidArgumentException('$values must contain at least two distinct elements.');
if (empty($values)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep a validation to forbid empty arrays (which does not make sense as it makes the config unusable)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Done in javiereguiluz@e75a983

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not 0 === count($values)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. What is the usual Symfony practice?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer empty here

throw new \InvalidArgumentException('$values must contain at least one element.');
}

parent::__construct($name, $parent);
Expand Down
15 changes: 14 additions & 1 deletion src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,23 @@ public function testFinalizeValue()

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage $values must contain at least one element.
*/
public function testConstructionWithNoValues()
{
new EnumNode('foo', null, array());
}

public function testConstructionWithOneValue()
{
new EnumNode('foo', null, array('foo', 'foo'));
$node = new EnumNode('foo', null, array('foo'));
$this->assertSame('foo', $node->finalize('foo'));
}

public function testConstructionWithOneDistinctValue()
{
$node = new EnumNode('foo', null, array('foo', 'foo'));
$this->assertSame('foo', $node->finalize('foo'));
}

/**
Expand Down
0