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

Commit 08d16f0

Browse files
committed
Merge branch 'hotfix/30' into develop
Forward port #30
2 parents c2b8351 + e5c641f commit 08d16f0

8 files changed

+232
-181
lines changed

src/ArrayInput.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ public function getValue()
6060
*/
6161
public function isValid($context = null)
6262
{
63+
$hasValue = $this->hasValue();
64+
$required = $this->isRequired();
65+
$hasFallback = $this->hasFallback();
66+
67+
if (! $hasValue && $hasFallback) {
68+
$this->setValue($this->getFallbackValue());
69+
return true;
70+
}
71+
72+
if (! $hasValue && $required) {
73+
$this->setErrorMessage('Value is required');
74+
return false;
75+
}
76+
6377
if (!$this->continueIfEmpty() && !$this->allowEmpty()) {
6478
$this->injectNotEmptyValidator();
6579
}
@@ -74,9 +88,9 @@ public function isValid($context = null)
7488
}
7589
$result = $validator->isValid($value, $context);
7690
if (!$result) {
77-
if ($this->hasFallback()) {
91+
if ($hasFallback) {
7892
$this->setValue($this->getFallbackValue());
79-
$result = true;
93+
return true;
8094
}
8195
break;
8296
}

src/BaseInputFilter.php

+6-31
Original file line numberDiff line numberDiff line change
@@ -254,43 +254,13 @@ protected function validateInputs(array $inputs, $data = [], $context = null)
254254
continue;
255255
}
256256

257-
$hasFallback = ($input instanceof Input && $input->hasFallback());
258-
259257
// If input is optional (not required), and value is not set, then ignore.
260258
if (!array_key_exists($name, $data)
261259
&& !$input->isRequired()
262260
) {
263261
continue;
264262
}
265263

266-
// If the value is required, not present in the data set, and
267-
// has no fallback, validation fails.
268-
if (!array_key_exists($name, $data)
269-
&& $input->isRequired()
270-
&& !$hasFallback
271-
) {
272-
$input->setErrorMessage('Value is required');
273-
$this->invalidInputs[$name] = $input;
274-
275-
if ($input->breakOnFailure()) {
276-
return false;
277-
}
278-
279-
$valid = false;
280-
continue;
281-
}
282-
283-
// If the value is required, not present in the data set, and
284-
// has a fallback, validation passes, and we set the input
285-
// value to the fallback.
286-
if (!array_key_exists($name, $data)
287-
&& $input->isRequired()
288-
&& $hasFallback
289-
) {
290-
$input->setValue($input->getFallbackValue());
291-
continue;
292-
}
293-
294264
// Make sure we have a value (empty) for validation of context
295265
if (!array_key_exists($name, $data)) {
296266
$data[$name] = null;
@@ -545,7 +515,7 @@ protected function populate()
545515
$input->clearRawValues();
546516
}
547517

548-
if (!isset($this->data[$name])) {
518+
if (!array_key_exists($name, $this->data)) {
549519
// No value; clear value in this input
550520
if ($input instanceof InputFilterInterface) {
551521
$input->setData([]);
@@ -557,6 +527,11 @@ protected function populate()
557527
continue;
558528
}
559529

530+
if ($input instanceof Input) {
531+
$input->resetValue();
532+
continue;
533+
}
534+
560535
$input->setValue(null);
561536
continue;
562537
}

src/FileInput.php

+6
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,17 @@ public function isEmptyFile($rawValue)
113113
public function isValid($context = null)
114114
{
115115
$rawValue = $this->getRawValue();
116+
$hasValue = $this->hasValue();
116117
$empty = $this->isEmptyFile($rawValue);
117118
$required = $this->isRequired();
118119
$allowEmpty = $this->allowEmpty();
119120
$continueIfEmpty = $this->continueIfEmpty();
120121

122+
if (! $hasValue && $required && ! $this->hasFallback()) {
123+
$this->setErrorMessage('Value is required');
124+
return false;
125+
}
126+
121127
if ($empty && ! $required && ! $continueIfEmpty) {
122128
return true;
123129
}

src/Input.php

+11
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,22 @@ public function merge(InputInterface $input)
368368
public function isValid($context = null)
369369
{
370370
$value = $this->getValue();
371+
$hasValue = $this->hasValue();
371372
$empty = ($value === null || $value === '' || $value === []);
372373
$required = $this->isRequired();
373374
$allowEmpty = $this->allowEmpty();
374375
$continueIfEmpty = $this->continueIfEmpty();
375376

377+
if (! $hasValue && $this->hasFallback()) {
378+
$this->setValue($this->getFallbackValue());
379+
return true;
380+
}
381+
382+
if (! $hasValue && $required) {
383+
$this->setErrorMessage('Value is required');
384+
return false;
385+
}
386+
376387
if ($empty && ! $required && ! $continueIfEmpty) {
377388
return true;
378389
}

test/ArrayInputTest.php

+12-33
Original file line numberDiff line numberDiff line change
@@ -187,39 +187,6 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
187187
$this->assertEquals($notEmptyMock, $validators[1]['instance']);
188188
}
189189

190-
public function dataFallbackValue()
191-
{
192-
return [
193-
[
194-
'fallbackValue' => []
195-
],
196-
[
197-
'fallbackValue' => [''],
198-
],
199-
[
200-
'fallbackValue' => [null],
201-
],
202-
[
203-
'fallbackValue' => ['some value'],
204-
],
205-
];
206-
}
207-
208-
/**
209-
* @dataProvider dataFallbackValue
210-
*/
211-
public function testFallbackValue($fallbackValue)
212-
{
213-
$this->input->setFallbackValue($fallbackValue);
214-
$validator = new Validator\Date();
215-
$this->input->getValidatorChain()->attach($validator);
216-
$this->input->setValue(['123']); // not a date
217-
218-
$this->assertTrue($this->input->isValid());
219-
$this->assertEmpty($this->input->getMessages());
220-
$this->assertSame($fallbackValue, $this->input->getValue());
221-
}
222-
223190
public function emptyValuesProvider()
224191
{
225192
return [
@@ -246,4 +213,16 @@ public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
246213
}));
247214
$this->assertTrue($this->input->isValid());
248215
}
216+
217+
public function fallbackValueVsIsValidProvider()
218+
{
219+
$dataSets = parent::fallbackValueVsIsValidProvider();
220+
array_walk($dataSets, function (&$set) {
221+
$set[1] = [$set[1]]; // Wrap fallback value into an array.
222+
$set[2] = [$set[2]]; // Wrap value into an array.
223+
$set[4] = [$set[4]]; // Wrap expected value into an array.
224+
});
225+
226+
return $dataSets;
227+
}
249228
}

test/BaseInputFilterTest.php

-80
Original file line numberDiff line numberDiff line change
@@ -1035,86 +1035,6 @@ public function testEmptyValuePassedForRequiredButAllowedEmptyInputShouldMarkInp
10351035
$this->assertTrue($filter->isValid(), 'Empty value should mark input filter as valid');
10361036
}
10371037

1038-
/**
1039-
* @group 10
1040-
*/
1041-
public function testMissingRequiredWithFallbackShouldMarkInputValid()
1042-
{
1043-
$foo = new Input('foo');
1044-
$foo->setRequired(true);
1045-
$foo->setAllowEmpty(false);
1046-
1047-
$bar = new Input('bar');
1048-
$bar->setRequired(true);
1049-
$bar->setFallbackValue('baz');
1050-
1051-
$filter = new InputFilter();
1052-
$filter->add($foo);
1053-
$filter->add($bar);
1054-
1055-
$filter->setData(['foo' => 'xyz']);
1056-
$this->assertTrue($filter->isValid(), 'Missing input with fallback value should mark input filter as valid');
1057-
$data = $filter->getValues();
1058-
$this->assertArrayHasKey('bar', $data);
1059-
$this->assertEquals($bar->getFallbackValue(), $data['bar']);
1060-
}
1061-
1062-
/**
1063-
* @group 10
1064-
*/
1065-
public function testMissingRequiredThatAllowsEmptyWithFallbackShouldMarkInputValid()
1066-
{
1067-
$foo = new Input('foo');
1068-
$foo->setRequired(true);
1069-
$foo->setAllowEmpty(false);
1070-
1071-
$bar = new Input('bar');
1072-
$bar->setRequired(true);
1073-
$bar->setAllowEmpty(true);
1074-
$bar->setFallbackValue('baz');
1075-
1076-
$filter = new InputFilter();
1077-
$filter->add($foo);
1078-
$filter->add($bar);
1079-
1080-
$filter->setData(['foo' => 'xyz']);
1081-
$this->assertTrue($filter->isValid(), 'Missing input with fallback value should mark input filter as valid');
1082-
$data = $filter->getValues();
1083-
$this->assertArrayHasKey('bar', $data);
1084-
$this->assertEquals($bar->getFallbackValue(), $data['bar']);
1085-
$this->assertArrayNotHasKey('bar', $filter->getValidInput());
1086-
$this->assertArrayNotHasKey('bar', $filter->getInvalidInput());
1087-
}
1088-
1089-
/**
1090-
* @group 10
1091-
*/
1092-
public function testEmptyRequiredValueWithFallbackShouldMarkInputValid()
1093-
{
1094-
$foo = new Input('foo');
1095-
$foo->setRequired(true);
1096-
$foo->setAllowEmpty(false);
1097-
1098-
$bar = new Input('bar');
1099-
$bar->setRequired(true);
1100-
$bar->setFallbackValue('baz');
1101-
1102-
$filter = new InputFilter();
1103-
$filter->add($foo);
1104-
$filter->add($bar);
1105-
1106-
$filter->setData([
1107-
'foo' => 'xyz',
1108-
'bar' => null,
1109-
]);
1110-
$this->assertTrue($filter->isValid(), 'Empty input with fallback value should mark input filter as valid');
1111-
$data = $filter->getValues();
1112-
$this->assertArrayHasKey('bar', $data);
1113-
$this->assertEquals($bar->getFallbackValue(), $data['bar']);
1114-
$this->assertArrayHasKey('bar', $filter->getValidInput());
1115-
$this->assertArrayNotHasKey('bar', $filter->getInvalidInput());
1116-
}
1117-
11181038
/**
11191039
* @group 15
11201040
*/

test/FileInputTest.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,27 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists()
311311
$this->markTestSkipped('Test is not enabled in FileInputTest');
312312
}
313313

314+
public function testFallbackValueVsIsValidRules(
315+
$required = null,
316+
$fallbackValue = null,
317+
$originalValue = null,
318+
$isValid = null,
319+
$expectedValue = null
320+
) {
321+
$this->markTestSkipped('Input::setFallbackValue is not implemented on FileInput');
322+
}
323+
324+
325+
public function testFallbackValueVsIsValidRulesWhenValueNotSet(
326+
$required = null,
327+
$fallbackValue = null,
328+
$originalValue = null,
329+
$isValid = null,
330+
$expectedValue = null
331+
) {
332+
$this->markTestSkipped('Input::setFallbackValue is not implemented on FileInput');
333+
}
334+
314335
public function testMerge()
315336
{
316337
$value = ['tmp_name' => 'bar'];
@@ -340,11 +361,6 @@ public function testMerge()
340361
$this->assertInstanceOf('Zend\Filter\StringTrim', $filters[0]);
341362
}
342363

343-
public function testFallbackValue($fallbackValue = null)
344-
{
345-
$this->markTestSkipped('Not use fallback value');
346-
}
347-
348364
public function testIsEmptyFileNotArray()
349365
{
350366
$rawValue = 'file';

0 commit comments

Comments
 (0)