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

Commit 9759253

Browse files
committed
[Input] Add hasValue()/resetValue() methods
This methods provide a flag for distinguish when $value contains a real `null` or the PHP default property value. PHP gives to all properties a default value of `null` unless other default value is set.
1 parent bded51b commit 9759253

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/Input.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ class Input implements
6767
*/
6868
protected $value;
6969

70+
/**
71+
* Flag for distinguish when $value contains a real `null` or the PHP default property value.
72+
*
73+
* PHP gives to all properties a default value of `null` unless other default value is set.
74+
*
75+
* @var bool
76+
*/
77+
protected $hasValue = false;
78+
7079
/**
7180
* @var mixed
7281
*/
@@ -163,12 +172,36 @@ public function setValidatorChain(ValidatorChain $validatorChain)
163172
}
164173

165174
/**
175+
* Set the input value.
176+
*
177+
* If you want to remove/unset the current value use {@link Input::resetValue()}.
178+
*
179+
* @see Input::getValue() For retrieve the input value.
180+
* @see Input::hasValue() For to know if input value was set.
181+
* @see Input::resetValue() For reset the input value to the default state.
182+
*
166183
* @param mixed $value
167184
* @return Input
168185
*/
169186
public function setValue($value)
170187
{
171188
$this->value = $value;
189+
$this->hasValue = true;
190+
return $this;
191+
}
192+
193+
/**
194+
* Reset input value to the default state.
195+
*
196+
* @see Input::hasValue() For to know if input value was set.
197+
* @see Input::setValue() For set a new value.
198+
*
199+
* @return Input
200+
*/
201+
public function resetValue()
202+
{
203+
$this->value = null;
204+
$this->hasValue = false;
172205
return $this;
173206
}
174207

@@ -270,6 +303,23 @@ public function getValue()
270303
return $filter->filter($this->value);
271304
}
272305

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

test/InputTest.php

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

0 commit comments

Comments
 (0)