Skip to content

Commit

Permalink
[Config] Add StringNode
Browse files Browse the repository at this point in the history
  • Loading branch information
raffaelecarelle authored and nicolas-grekas committed Oct 23, 2024
1 parent 4e59caa commit 0cd83b2
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 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
5 changes: 5 additions & 0 deletions Definition/Builder/ArrayNodeDefinition.php
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
9 changes: 9 additions & 0 deletions Definition/Builder/NodeBuilder.php
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
34 changes: 34 additions & 0 deletions Definition/Builder/StringNodeDefinition.php
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 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'];
}
}
6 changes: 6 additions & 0 deletions Tests/Definition/Builder/ArrayNodeDefinitionTest.php
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
9 changes: 9 additions & 0 deletions Tests/Definition/Builder/NodeBuilderTest.php
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 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);
}
}

0 comments on commit 0cd83b2

Please sign in to comment.