[Config] Improve error messages & extensibility

This commit is contained in:
Victor Berchet 2012-02-20 09:02:45 +01:00
parent bca2b0edf3
commit cba2c332ad
1 changed files with 58 additions and 30 deletions

View File

@ -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)
);
}
}
}