Skip to content

Instantly share code, notes, and snippets.

@drm
Created December 31, 2012 14:56
Show Gist options
  • Save drm/4420349 to your computer and use it in GitHub Desktop.
Save drm/4420349 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Gerard van Helden <gerard@zicht.nl>
* @copyright Zicht Online <http://zicht.nl>
*/
require_once 'vendor/autoload.php';
class MyConf implements \Symfony\Component\Config\Definition\ConfigurationInterface
{
/**
* Generates the configuration tree builder.
*
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new \Symfony\Component\Config\Definition\Builder\TreeBuilder();
$root = $treeBuilder->root('task');
$root
->children()
->arrayNode('tasks')
->prototype('array')
// ->children()->scalarNode('type')->end()->end()
->prototype('variable')
->end()
->validate()
->ifTrue(function($v) {
$params = $v;
unset($params['type']);
$isValid = false;
switch($v['type']) {
case 'a':
$isValid = isset($params['param_valid_for_a']) && count($params) == 1;
break;
case 'b':
$isValid = isset($params['param_valid_for_b']) && count($params) == 1;
break;
}
return !$isValid;
})
->thenInvalid('Fail')
->end()
->end()
->end()
->end()
;
return $treeBuilder;
}
}
$yml = <<<EOYML
tasks:
-
type: "a"
param_valid_for_a: "value1"
-
type: "a"
param_valid_for_a: "value2"
# param_valid_for_b: "value2"
-
type: "b"
param_valid_for_b: "value"
EOYML;
$processor = new \Symfony\Component\Config\Definition\Processor();
$configs = array();
$configs[]= \Symfony\Component\Yaml\Yaml::parse($yml);
$result = $processor->processConfiguration(new MyConf(), $configs);
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment