Skip to content

Commit 7acb329

Browse files
committed
[Config] replaced setInfo(), setExample() with more generic attributes
1 parent 0a5f711 commit 7acb329

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

Definition/BaseNode.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ abstract class BaseNode implements NodeInterface
2929
protected $allowOverwrite;
3030
protected $required;
3131
protected $equivalentValues;
32-
protected $info;
33-
protected $example;
32+
protected $attributes = array();
3433

3534
/**
3635
* Constructor.
@@ -55,14 +54,44 @@ public function __construct($name, NodeInterface $parent = null)
5554
$this->equivalentValues = array();
5655
}
5756

57+
public function setAttribute($key, $value)
58+
{
59+
$this->attributes[$key] = $value;
60+
}
61+
62+
public function getAttribute($key, $default = null)
63+
{
64+
return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
65+
}
66+
67+
public function hasAttribute($key)
68+
{
69+
return isset($this->attributes[$key]);
70+
}
71+
72+
public function getAttributes()
73+
{
74+
return $this->attributes;
75+
}
76+
77+
public function setAttributes(array $attributes)
78+
{
79+
$this->attributes = $attributes;
80+
}
81+
82+
public function removeAttribute($key)
83+
{
84+
unset($this->attributes[$key]);
85+
}
86+
5887
/**
59-
* Sets info message.
88+
* Sets an info message.
6089
*
61-
* @param string $info The info text
90+
* @param string $info
6291
*/
6392
public function setInfo($info)
6493
{
65-
$this->info = $info;
94+
$this->setAttribute('info', $info);
6695
}
6796

6897
/**
@@ -72,7 +101,7 @@ public function setInfo($info)
72101
*/
73102
public function getInfo()
74103
{
75-
return $this->info;
104+
return $this->getAttribute('info');
76105
}
77106

78107
/**
@@ -82,7 +111,7 @@ public function getInfo()
82111
*/
83112
public function setExample($example)
84113
{
85-
$this->example = $example;
114+
$this->setAttribute('example', $example);
86115
}
87116

88117
/**
@@ -92,7 +121,7 @@ public function setExample($example)
92121
*/
93122
public function getExample()
94123
{
95-
return $this->example;
124+
return $this->getAttribute('example');
96125
}
97126

98127
/**

Definition/Builder/NodeDefinition.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ abstract class NodeDefinition implements NodeParentInterface
3232
protected $trueEquivalent;
3333
protected $falseEquivalent;
3434
protected $parent;
35-
protected $info;
36-
protected $example;
35+
protected $attributes = array();
3736

3837
/**
3938
* Constructor
@@ -72,11 +71,9 @@ public function setParent(NodeParentInterface $parent)
7271
*
7372
* @return NodeDefinition
7473
*/
75-
public function setInfo($info)
74+
public function info($info)
7675
{
77-
$this->info = $info;
78-
79-
return $this;
76+
return $this->attribute('info', $info);
8077
}
8178

8279
/**
@@ -86,9 +83,22 @@ public function setInfo($info)
8683
*
8784
* @return NodeDefinition
8885
*/
89-
public function setExample($example)
86+
public function example($example)
87+
{
88+
return $this->attribute('example', $example);
89+
}
90+
91+
/**
92+
* Sets an attribute on the node.
93+
*
94+
* @param string $key
95+
* @param mixed $value
96+
*
97+
* @return NodeDefinition
98+
*/
99+
public function attribute($key, $value)
90100
{
91-
$this->example = $example;
101+
$this->attributes[$key] = $value;
92102

93103
return $this;
94104
}
@@ -125,9 +135,7 @@ public function getNode($forceRootNode = false)
125135
}
126136

127137
$node = $this->createNode();
128-
129-
$node->setInfo($this->info);
130-
$node->setExample($this->example);
138+
$node->setAttributes($this->attributes);
131139

132140
return $node;
133141
}

Tests/Definition/Builder/TreeBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public function testDefinitionInfoGetsTransferedToNode()
9494
{
9595
$builder = new TreeBuilder();
9696

97-
$builder->root('test')->setInfo('root info')
97+
$builder->root('test')->info('root info')
9898
->children()
99-
->node('child', 'variable')->setInfo('child info')->defaultValue('default')
99+
->node('child', 'variable')->info('child info')->defaultValue('default')
100100
->end()
101101
->end();
102102

@@ -112,9 +112,9 @@ public function testDefinitionExampleGetsTransferedToNode()
112112
$builder = new TreeBuilder();
113113

114114
$builder->root('test')
115-
->setExample(array('key' => 'value'))
115+
->example(array('key' => 'value'))
116116
->children()
117-
->node('child', 'variable')->setInfo('child info')->defaultValue('default')->setExample('example')
117+
->node('child', 'variable')->info('child info')->defaultValue('default')->example('example')
118118
->end()
119119
->end();
120120

0 commit comments

Comments
 (0)