user2340612
user2340612

Reputation: 10704

Symfony2 configuration force array type

I am developing a Symfony2 application and I want to be able to use a configuration file like this:

my_config:
    values: ['val1', 'val2']

So I created the following configuration file:

class Configuration implements ConfigurationInterface {
    public function getConfigTreeBuilder() {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('my_config');

        $rootNode
            ->children()
                ->arrayNode('values')
                    ->prototype('scalar')->end()
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

This configuration, however, lets me add something like:

my_config:
    values: ['val1', 123, false]

Is there a way to enforce array values to be of type String (e.g. something like prototype('string'))?

Upvotes: 0

Views: 64

Answers (1)

FZE
FZE

Reputation: 1627

There is no string. Check the file https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Config/Definition/Builder/NodeBuilder.php#L27

Node mapping just expect these values

$this->nodeMapping = array(
    'variable' => __NAMESPACE__.'\\VariableNodeDefinition',
    'scalar' => __NAMESPACE__.'\\ScalarNodeDefinition',
    'boolean' => __NAMESPACE__.'\\BooleanNodeDefinition',
    'integer' => __NAMESPACE__.'\\IntegerNodeDefinition',
    'float' => __NAMESPACE__.'\\FloatNodeDefinition',
    'array' => __NAMESPACE__.'\\ArrayNodeDefinition',
    'enum' => __NAMESPACE__.'\\EnumNodeDefinition',
);

You can check manually in your Conifguration class. Check http://symfony.com/doc/current/cookbook/bundles/configuration.html#processing-the-configs-array this section for further information.

Upvotes: 2

Related Questions