Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 876fd9b

Browse files
committed
[OptionsResolver] Implemented fluid interface
1 parent 95454f5 commit 876fd9b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ public function __construct()
6363
*
6464
* - function (Options $options)
6565
* - function (Options $options, $previousValue)
66+
*
67+
* @return OptionsResolver The resolver instance.
6668
*/
6769
public function setDefaults(array $defaultValues)
6870
{
6971
foreach ($defaultValues as $option => $value) {
7072
$this->defaultOptions[$option] = $value;
7173
$this->knownOptions[$option] = true;
7274
}
75+
76+
return $this;
7377
}
7478

7579
/**
@@ -85,6 +89,8 @@ public function setDefaults(array $defaultValues)
8589
* of the following signature:
8690
*
8791
* - function (Options $options)
92+
*
93+
* @return OptionsResolver The resolver instance.
8894
*/
8995
public function replaceDefaults(array $defaultValues)
9096
{
@@ -93,6 +99,8 @@ public function replaceDefaults(array $defaultValues)
9399
$this->defaultOptions[$option] = $value;
94100
$this->knownOptions[$option] = true;
95101
}
102+
103+
return $this;
96104
}
97105

98106
/**
@@ -105,6 +113,8 @@ public function replaceDefaults(array $defaultValues)
105113
*
106114
* @param array $optionNames A list of option names.
107115
*
116+
* @return OptionsResolver The resolver instance.
117+
*
108118
* @throws OptionDefinitionException When trying to pass default values.
109119
*/
110120
public function setOptional(array $optionNames)
@@ -116,6 +126,8 @@ public function setOptional(array $optionNames)
116126

117127
$this->knownOptions[$option] = true;
118128
}
129+
130+
return $this;
119131
}
120132

121133
/**
@@ -125,6 +137,8 @@ public function setOptional(array $optionNames)
125137
*
126138
* @param array $optionNames A list of option names.
127139
*
140+
* @return OptionsResolver The resolver instance.
141+
*
128142
* @throws OptionDefinitionException When trying to pass default values.
129143
*/
130144
public function setRequired(array $optionNames)
@@ -137,6 +151,8 @@ public function setRequired(array $optionNames)
137151
$this->knownOptions[$option] = true;
138152
$this->requiredOptions[$option] = true;
139153
}
154+
155+
return $this;
140156
}
141157

142158
/**
@@ -146,6 +162,8 @@ public function setRequired(array $optionNames)
146162
* with values acceptable for that option as
147163
* values.
148164
*
165+
* @return OptionsResolver The resolver instance.
166+
*
149167
* @throws InvalidOptionsException If an option has not been defined for
150168
* which an allowed value is set.
151169
*/
@@ -154,6 +172,8 @@ public function setAllowedValues(array $allowedValues)
154172
$this->validateOptionNames(array_keys($allowedValues));
155173

156174
$this->allowedValues = array_replace($this->allowedValues, $allowedValues);
175+
176+
return $this;
157177
}
158178

159179
/**
@@ -165,6 +185,8 @@ public function setAllowedValues(array $allowedValues)
165185
* with values acceptable for that option as
166186
* values.
167187
*
188+
* @return OptionsResolver The resolver instance.
189+
*
168190
* @throws InvalidOptionsException If an option has not been defined for
169191
* which an allowed value is set.
170192
*/
@@ -173,6 +195,8 @@ public function addAllowedValues(array $allowedValues)
173195
$this->validateOptionNames(array_keys($allowedValues));
174196

175197
$this->allowedValues = array_merge_recursive($this->allowedValues, $allowedValues);
198+
199+
return $this;
176200
}
177201

178202
/**

src/Symfony/Component/OptionsResolver/Tests/OptionResolverTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,23 @@ public function testSetOptionalFailsIfDefaultIsPassed()
292292
'one' => '1',
293293
));
294294
}
295+
296+
public function testFluidInterface()
297+
{
298+
$this->resolver->setDefaults(array('one' => '1'))
299+
->replaceDefaults(array('one' => '2'))
300+
->setAllowedValues(array('one' => array('1', '2')))
301+
->addAllowedValues(array('one' => array('3')))
302+
->setRequired(array('two'))
303+
->setOptional(array('three'));
304+
305+
$options = array(
306+
'two' => '2',
307+
);
308+
309+
$this->assertEquals(array(
310+
'one' => '2',
311+
'two' => '2',
312+
), $this->resolver->resolve($options));
313+
}
295314
}

0 commit comments

Comments
 (0)