Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f074903

Browse files
committed
[Config] Changes after code review
1 parent ccfede1 commit f074903

File tree

5 files changed

+80
-166
lines changed

5 files changed

+80
-166
lines changed

src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,32 @@ public function getChildNodeDefinitions()
523523
{
524524
return $this->children;
525525
}
526+
527+
/**
528+
* Finds a node defined by the given $nodePath.
529+
*
530+
* @param string $nodePath The path of the node to find. e.g "doctrine.orm.mappings"
531+
*
532+
* @return NodeDefinition
533+
*/
534+
public function find(string $nodePath): NodeDefinition
535+
{
536+
if ($nodePath === $this->name) {
537+
return $this;
538+
}
539+
540+
$firstPathSegment = strtok($nodePath, $this->pathSeparator);
541+
542+
if (!\array_key_exists($firstPathSegment, $this->getChildNodeDefinitions())) {
543+
throw new \RuntimeException(sprintf('Node with name "%s" does not exist in the current node "%s"!', $firstPathSegment, $this->name));
544+
}
545+
546+
$node = $this->getChildNodeDefinitions()[$firstPathSegment];
547+
548+
if (0 === substr_count($nodePath, $this->pathSeparator)) {
549+
return $node;
550+
}
551+
552+
return $node->find(substr($nodePath, strpos($nodePath, $this->pathSeparator) + 1));
553+
}
526554
}

src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public function __construct(?string $name, NodeParentInterface $parent = null)
4444
$this->name = $name;
4545
}
4646

47-
public function getName(): string
48-
{
49-
return $this->name;
50-
}
51-
5247
/**
5348
* Sets the parent node.
5449
*

src/Symfony/Component/Config/Definition/Builder/NodeFinder.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16+
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition;
17+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
1618
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
1719
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
1820
use Symfony\Component\Config\Definition\Processor;
@@ -357,6 +359,56 @@ public function testCannotBeEmptyOnConcreteNode()
357359
$node->getNode()->finalize([]);
358360
}
359361

362+
public function testFindShouldThrowExceptionIfNodeDoesNotExistInRootNode()
363+
{
364+
$this->expectException(\RuntimeException::class);
365+
$this->expectExceptionMessage('Node with name "child" does not exist in the current node "root"!');
366+
367+
$rootNode = new ArrayNodeDefinition('root');
368+
$rootNode
369+
->children()
370+
->arrayNode('social_media_channels')->end()
371+
->end()
372+
;
373+
374+
$rootNode->find('child');
375+
}
376+
377+
public function testFindShouldHandleComplexConfigurationProbably()
378+
{
379+
$rootNode = new ArrayNodeDefinition('root');
380+
$rootNode
381+
->children()
382+
->arrayNode('social_media_channels')
383+
->children()
384+
->booleanNode('enable')->end()
385+
->arrayNode('twitter')->end()
386+
->arrayNode('facebook')->end()
387+
->arrayNode('instagram')
388+
->children()
389+
->booleanNode('enable')->end()
390+
->arrayNode('accounts')->end()
391+
->end()
392+
->end()
393+
->end()
394+
->end()
395+
;
396+
397+
$this->assertNode('social_media_channels', ArrayNodeDefinition::class, $rootNode->find('social_media_channels'));
398+
$this->assertNode('enable', BooleanNodeDefinition::class, $rootNode->find('social_media_channels.enable'));
399+
$this->assertNode('twitter', ArrayNodeDefinition::class, $rootNode->find('social_media_channels.twitter'));
400+
$this->assertNode('facebook', ArrayNodeDefinition::class, $rootNode->find('social_media_channels.facebook'));
401+
$this->assertNode('instagram', ArrayNodeDefinition::class, $rootNode->find('social_media_channels.instagram'));
402+
$this->assertNode('enable', BooleanNodeDefinition::class, $rootNode->find('social_media_channels.instagram.enable'));
403+
$this->assertNode('accounts', ArrayNodeDefinition::class, $rootNode->find('social_media_channels.instagram.accounts'));
404+
}
405+
406+
protected function assertNode(string $expectedName, string $expectedType, NodeDefinition $actualNode): void
407+
{
408+
$this->assertInstanceOf($expectedType, $actualNode);
409+
$this->assertSame($expectedName, $this->getField($actualNode, 'name'));
410+
}
411+
360412
protected function getField($object, $field)
361413
{
362414
$reflection = new \ReflectionProperty($object, $field);

src/Symfony/Component/Config/Tests/Definition/Builder/NodeFinderTest.php

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)