This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Configuration/BaseNode.php
Johannes Schmitt b484763a7a [DependencyInjection] added first version of the config normalizer
This is mainly intended for complex configurations to ease the work you
have with normalizing different configuration formats (YAML, XML, and PHP).

First, you have to set-up a config tree:

    $treeBuilder = new TreeBuilder();
    $tree = $treeBuilder
        ->root('security_config', 'array')
            ->node('access_denied_url', 'scalar')->end()
            ->normalize('encoder')
            ->node('encoders', 'array')
                ->key('class')
                ->prototype('array')
                    ->before()->ifString()->then(function($v) { return array('algorithm' => $v); })->end()
                    ->node('algorithm', 'scalar')->end()
                    ->node('encode_as_base64', 'scalar')->end()
                    ->node('iterations', 'scalar')->end()
                ->end()
            ->end()
        ->end()
        ->buildTree()
    ;

This tree and the metadata attached to the different nodes is then used
to intelligently transform the passed config array:

    $normalizedConfig = $tree->normalize($config);
2011-02-01 16:07:04 +01:00

64 lines
1.6 KiB
PHP

<?php
namespace Symfony\Component\DependencyInjection\Configuration;
abstract class BaseNode implements NodeInterface
{
protected $name;
protected $parent;
protected $beforeTransformations;
protected $afterTransformations;
protected $nodeFactory;
public function __construct($name, NodeInterface $parent = null, $beforeTransformations = array(), $afterTransformations = array())
{
if (false !== strpos($name, '.')) {
throw new \InvalidArgumentException('The name must not contain ".".');
}
$this->name = $name;
$this->parent = $parent;
$this->beforeTransformations = $beforeTransformations;
$this->afterTransformations = $afterTransformations;
}
public function getName()
{
return $this->name;
}
public function getPath()
{
$path = $this->name;
if (null !== $this->parent) {
$path = $this->parent->getPath().'.'.$path;
}
return $path;
}
public final function normalize($value)
{
// run before transformations
foreach ($this->beforeTransformations as $transformation) {
$value = $transformation($value);
}
// validate type
$this->validateType($value);
// normalize value
$value = $this->normalizeValue($value);
// run after transformations
foreach ($this->afterTransformations as $transformation) {
$value = $transformation($value);
}
return $value;
}
abstract protected function validateType($value);
abstract protected function normalizeValue($value);
}