Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit c2b8351

Browse files
committed
Merge branch 'hotfix/29' into develop
Forward port #29
2 parents ace26d6 + c1ea9e0 commit c2b8351

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/ArrayInput.php

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public function setValue($value)
3131
return parent::setValue($value);
3232
}
3333

34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function resetValue()
38+
{
39+
$this->value = [];
40+
$this->hasValue = false;
41+
return $this;
42+
}
43+
3444
/**
3545
* @return array
3646
*/

src/Input.php

+47
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ class Input implements
6767
*/
6868
protected $value;
6969

70+
/**
71+
* Flag for distinguish when $value contains the value previously set or the default one.
72+
*
73+
* @var bool
74+
*/
75+
protected $hasValue = false;
76+
7077
/**
7178
* @var mixed
7279
*/
@@ -163,12 +170,36 @@ public function setValidatorChain(ValidatorChain $validatorChain)
163170
}
164171

165172
/**
173+
* Set the input value.
174+
*
175+
* If you want to remove/unset the current value use {@link Input::resetValue()}.
176+
*
177+
* @see Input::getValue() For retrieve the input value.
178+
* @see Input::hasValue() For to know if input value was set.
179+
* @see Input::resetValue() For reset the input value to the default state.
180+
*
166181
* @param mixed $value
167182
* @return Input
168183
*/
169184
public function setValue($value)
170185
{
171186
$this->value = $value;
187+
$this->hasValue = true;
188+
return $this;
189+
}
190+
191+
/**
192+
* Reset input value to the default state.
193+
*
194+
* @see Input::hasValue() For to know if input value was set.
195+
* @see Input::setValue() For set a new value.
196+
*
197+
* @return Input
198+
*/
199+
public function resetValue()
200+
{
201+
$this->value = null;
202+
$this->hasValue = false;
172203
return $this;
173204
}
174205

@@ -270,6 +301,22 @@ public function getValue()
270301
return $filter->filter($this->value);
271302
}
272303

304+
/**
305+
* Flag for inform if input value was set.
306+
*
307+
* This flag used for distinguish when {@link Input::getValue()} will return the value previously set or the default.
308+
*
309+
* @see Input::getValue() For retrieve the input value.
310+
* @see Input::setValue() For set a new value.
311+
* @see Input::resetValue() For reset the input value to the default state.
312+
*
313+
* @return bool
314+
*/
315+
public function hasValue()
316+
{
317+
return $this->hasValue;
318+
}
319+
273320
/**
274321
* @return mixed
275322
*/

test/InputTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -854,4 +854,33 @@ public function testWhenNotRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsA
854854
$input->setValue($value);
855855
$this->{$assertion}($input->isValid());
856856
}
857+
858+
/**
859+
* @dataProvider emptyValuesProvider
860+
*/
861+
public function testSetValuePutInputInTheDesiredState($value)
862+
{
863+
$input = $this->input;
864+
$this->assertFalse($input->hasValue(), 'Input should not have value by default');
865+
866+
$input->setValue($value);
867+
$this->assertTrue($input->hasValue(), "hasValue() didn't return true when value was set");
868+
}
869+
870+
/**
871+
* @dataProvider emptyValuesProvider
872+
*/
873+
public function testResetValueReturnsInputValueToDefaultValue($value)
874+
{
875+
$input = $this->input;
876+
$originalInput = clone $input;
877+
$this->assertFalse($input->hasValue(), 'Input should not have value by default');
878+
879+
$input->setValue($value);
880+
$this->assertTrue($input->hasValue(), "hasValue() didn't return true when value was set");
881+
882+
$return = $input->resetValue();
883+
$this->assertSame($input, $return, 'resetValue() must return itself');
884+
$this->assertEquals($originalInput, $input, 'Input was not reset to the default value state');
885+
}
857886
}

0 commit comments

Comments
 (0)