From cba2c332adb11f6f8e782440a7a5f84c6a10afea Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 20 Feb 2012 09:02:45 +0100 Subject: [PATCH] [Config] Improve error messages & extensibility --- .../Builder/ArrayNodeDefinition.php | 88 ++++++++++++------- 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php index d979a8fe40..4403384909 100644 --- a/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +++ b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php @@ -278,25 +278,10 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition protected function createNode() { if (null === $this->prototype) { - if (null !== $this->key) { - throw new InvalidDefinitionException( - sprintf('%s::useAttributeAsKey() is not applicable to concrete nodes.', __CLASS__) - ); - } - - if (true === $this->atLeastOne) { - throw new InvalidDefinitionException( - sprintf('%s::requiresAtLeastOneElement() is not applicable to concrete nodes.', __CLASS__) - ); - } - - if ($this->default) { - throw new InvalidDefinitionException( - sprintf('%s::defaultValue() is not applicable to concrete nodes.', __CLASS__) - ); - } - $node = new ArrayNode($this->name, $this->parent); + + $this->validateConcreteNode($node); + $node->setAddIfNotSet($this->addDefaults); foreach ($this->children as $child) { @@ -304,19 +289,10 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition $node->addChild($child->getNode()); } } else { - if ($this->addDefaults) { - throw new InvalidDefinitionException( - sprintf('%s::addDefaultsIfNotSet() is not applicable to prototype nodes.', __CLASS__) - ); - } - - if ($this->default && false !== $this->addDefaultChildren) { - throw new InvalidDefinitionException('A default value and default children might not be used together.'); - - } - $node = new PrototypedArrayNode($this->name, $this->parent); + $this->validatePrototypeNode($node); + if (null !== $this->key) { $node->setKeyAttribute($this->key, $this->removeKeyItem); } @@ -338,7 +314,6 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition $this->prototype->parent = $node; $node->setPrototype($this->prototype->getNode()); - } $node->setAllowNewKeys($this->allowNewKeys); @@ -366,4 +341,57 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition return $node; } + /** + * Validate the confifuration of a concrete node. + * + * @param NodeInterface $node The related node + * + * @throws InvalidDefinitionException When an error is detected in the configuration + */ + protected function validateConcreteNode(ArrayNode $node) + { + $path = $node->getPath(); + + if (null !== $this->key) { + throw new InvalidDefinitionException( + sprintf('->useAttributeAsKey() is not applicable to concrete nodes at path "%s"', $path) + ); + } + + if (true === $this->atLeastOne) { + throw new InvalidDefinitionException( + sprintf('->requiresAtLeastOneElement() is not applicable to concrete nodes at path "%s"', $path) + ); + } + + if ($this->default) { + throw new InvalidDefinitionException( + sprintf('->defaultValue() is not applicable to concrete nodes at path "%s"', $path) + ); + } + } + + /** + * Validate the configuration of a prototype node. + * + * @param NodeInterface $node The related node + * + * @throws InvalidDefinitionException When an error is detected in the configuration + */ + protected function validatePrototypeNode(PrototypedArrayNode $node) + { + $path = $node->getPath(); + + if ($this->addDefaults) { + throw new InvalidDefinitionException( + sprintf('->addDefaultsIfNotSet() is not applicable to prototype nodes at path "%s"', $path) + ); + } + + if ($this->default && false !== $this->addDefaultChildren) { + throw new InvalidDefinitionException( + sprintf('A default value and default children might not be used together at path "%s"', $path) + ); + } + } }