Skip to content

[Config] Add StringNode #58428

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

Merged
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ CHANGELOG
* Generate a meta file in JSON format for resource tracking
* Add `SkippingResourceChecker`
* Add support for `defaultNull()` on `BooleanNode`
* Add `StringNode` and `StringNodeDefinition`
* Add `ArrayNodeDefinition::stringPrototype()` method
* Add `NodeBuilder::stringNode()` method

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public function scalarPrototype(): ScalarNodeDefinition
return $this->prototype('scalar');
}

public function stringPrototype(): StringNodeDefinition
{
return $this->prototype('string');
}

public function booleanPrototype(): BooleanNodeDefinition
{
return $this->prototype('boolean');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct()
'float' => FloatNodeDefinition::class,
'array' => ArrayNodeDefinition::class,
'enum' => EnumNodeDefinition::class,
'string' => StringNodeDefinition::class,
];
}

Expand Down Expand Up @@ -102,6 +103,14 @@ public function variableNode(string $name): VariableNodeDefinition
return $this->node($name, 'variable');
}

/**
* Creates a child string node.
*/
public function stringNode(string $name): StringNodeDefinition
{
return $this->node($name, 'string');
}

/**
* Returns the parent node.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Definition\Builder;

use Symfony\Component\Config\Definition\StringNode;

/**
* This class provides a fluent interface for defining a node.
*
* @author Raffaele Carelle <raffaele.carelle@gmail.com>
*/
class StringNodeDefinition extends ScalarNodeDefinition
{
public function __construct(?string $name, ?NodeParentInterface $parent = null)
{
parent::__construct($name, $parent);

$this->nullEquivalent = '';
}

protected function instantiateNode(): StringNode
{
return new StringNode($this->name, $this->parent, $this->pathSeparator);
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/Config/Definition/StringNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Definition;

use Symfony\Component\Config\Definition\Exception\InvalidTypeException;

/**
* This node represents a String value in the config tree.
*
* @author Raffaele Carelle <raffaele.carelle@gmail.com>
*/
class StringNode extends ScalarNode
{
protected function validateType(mixed $value): void
{
if (!\is_string($value)) {
$ex = new InvalidTypeException(\sprintf('Invalid type for path "%s". Expected "string", but got "%s".', $this->getPath(), get_debug_type($value)));
if ($hint = $this->getInfo()) {
$ex->addHint($hint);
}
$ex->setPath($this->getPath());

throw $ex;
}
}

protected function getValidPlaceholderTypes(): array
{
return ['string'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ public function testPrototypeBoolean()
$this->assertEquals($node->prototype('boolean'), $node->booleanPrototype());
}

public function testPrototypeString()
{
$node = new ArrayNodeDefinition('root');
$this->assertEquals($node->prototype('string'), $node->stringPrototype());
}

public function testPrototypeInteger()
{
$node = new ArrayNodeDefinition('root');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Config\Definition\Builder\FloatNodeDefinition;
use Symfony\Component\Config\Definition\Builder\IntegerNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder as BaseNodeBuilder;
use Symfony\Component\Config\Definition\Builder\StringNodeDefinition;
use Symfony\Component\Config\Definition\Builder\VariableNodeDefinition as BaseVariableNodeDefinition;

class NodeBuilderTest extends TestCase
Expand Down Expand Up @@ -86,6 +87,14 @@ public function testNumericNodeCreation()
$node = $builder->floatNode('bar')->min(3.0)->max(5.0);
$this->assertInstanceOf(FloatNodeDefinition::class, $node);
}

public function testStringNodeCreation()
{
$builder = new BaseNodeBuilder();

$node = $builder->stringNode('foo bar');
$this->assertInstanceOf(StringNodeDefinition::class, $node);
}
}

class SomeNodeDefinition extends BaseVariableNodeDefinition
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Component/Config/Tests/Definition/StringNodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Tests\Definition;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
use Symfony\Component\Config\Definition\StringNode;

class StringNodeTest extends TestCase
{
/**
* @testWith [""]
* ["valid string"]
*/
public function testNormalize(string $value)
{
$node = new StringNode('test');
$this->assertSame($value, $node->normalize($value));
}

/**
* @testWith [null]
* [false]
* [true]
* [0]
* [1]
* [0.0]
* [0.1]
* [{}]
* [{"foo": "bar"}]
*/
public function testNormalizeThrowsExceptionOnInvalidValues($value)
{
$node = new StringNode('test');

$this->expectException(InvalidTypeException::class);

$node->normalize($value);
}
}