-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e59caa
commit 0cd83b2
Showing
8 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |