ScalarNode example

$children = $prototype->getChildren();

            if ($prototype instanceof PrototypedArrayNode && $prototype->getKeyAttribute()) {
                $children = $this->getPrototypeChildren($prototype);
            }

            // add children             foreach ($children as $childNode) {
                $keyNode->addChild($childNode);
            }
        } else {
            $keyNode = new ScalarNode($key$node);
        }

        $info = 'Prototype';
        if (null !== $prototype->getInfo()) {
            $info .= ': '.$prototype->getInfo();
        }
        $keyNode->setInfo($info);

        return [$key => $keyNode];
    }
}
$this->assertEquals(['test']$node->getDefaultValue());
    }

    // a remapped key (e.g. "mapping" -> "mappings") should be unset after being used     public function testRemappedKeysAreUnset()
    {
        $node = new ArrayNode('root');
        $mappingsNode = new PrototypedArrayNode('mappings');
        $node->addChild($mappingsNode);

        // each item under mappings is just a scalar         $prototype = new ScalarNode(null, $mappingsNode);
        $mappingsNode->setPrototype($prototype);

        $remappings = [];
        $remappings[] = ['mapping', 'mappings'];
        $node->setXmlRemappings($remappings);

        $normalized = $node->normalize(['mapping' => ['foo', 'bar']]);
        $this->assertEquals(['mappings' => ['foo', 'bar']]$normalized);
    }

    /** * Tests that when a key attribute is mapped, that key is removed from the array. * * <things> * <option id="option1" value="foo"> * <option id="option2" value="bar"> * </things> * * The above should finally be mapped to an array that looks like this * (because "id" is the key attribute). * * [ * 'things' => [ * 'option1' => 'foo', * 'option2' => 'bar', * ] * ] */
'foo-bar' => null, 'foo_bar' => 'foo'],
            ],
        ];
    }

    /** * @dataProvider getZeroNamedNodeExamplesData */
    public function testNodeNameCanBeZero(array $denormalized, array $normalized)
    {
        $zeroNode = new ArrayNode(0);
        $zeroNode->addChild(new ScalarNode('name'));
        $fiveNode = new ArrayNode(5);
        $fiveNode->addChild(new ScalarNode(0));
        $fiveNode->addChild(new ScalarNode('new_key'));
        $rootNode = new ArrayNode('root');
        $rootNode->addChild($zeroNode);
        $rootNode->addChild($fiveNode);
        $rootNode->addChild(new ScalarNode('string_key'));
        $r = new \ReflectionMethod($rootNode, 'normalizeValue');

        $this->assertSame($normalized$r->invoke($rootNode$denormalized));
    }

    

class ScalarNodeDefinition extends VariableNodeDefinition
{
    /** * Instantiate a Node. */
    protected function instantiateNode(): ScalarNode
    {
        return new ScalarNode($this->name, $this->parent, $this->pathSeparator);
    }
}
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
use Symfony\Component\Config\Definition\ScalarNode;

class ScalarNodeTest extends TestCase
{
    /** * @dataProvider getValidValues */
    public function testNormalize($value)
    {
        $node = new ScalarNode('test');
        $this->assertSame($value$node->normalize($value));
    }

    public static function getValidValues(): array
    {
        return [
            [false],
            [true],
            [null],
            [''],
            ['foo'],
            [
Home | Imprint | This part of the site doesn't use cookies.