Skip to content

Commit 5017419

Browse files
committed
Allow nested input filter definitions.
1 parent 5ef9a82 commit 5017419

4 files changed

Lines changed: 102 additions & 11 deletions

File tree

phpstan-baseline.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ parameters:
1111
path: src/Extractor/OptionsExtractorFactory.php
1212

1313
-
14-
message: "#^Call to an undefined method Laminas\\\\InputFilter\\\\InputFilterInterface\\|Laminas\\\\InputFilter\\\\InputInterface\\:\\:getName\\(\\)\\.$#"
15-
count: 1
16-
path: src/Validator/ValidationResult.php
17-
18-
-
19-
message: "#^Parameter \\#3 \\$messages of class ZE\\\\ContentValidation\\\\Validator\\\\ValidationResult constructor expects array\\<string, array\\<string\\>\\>, array\\<string, array\\<array\\<string\\>\\|string\\>\\> given\\.$#"
14+
message: "#^Parameter \\#2 \\$values of class ZE\\\\ContentValidation\\\\Validator\\\\ValidationResult constructor expects array, mixed given\\.$#"
2015
count: 1
2116
path: src/Validator/ValidationResult.php

src/Validator/ValidationResult.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Laminas\InputFilter\InputFilterInterface;
1515

16+
/**
17+
* @phpstan-import-type MessagesArray from ValidationResultInterface
18+
*/
1619
final class ValidationResult implements ValidationResultInterface
1720
{
1821
/**
@@ -26,7 +29,7 @@ final class ValidationResult implements ValidationResultInterface
2629
private array $values;
2730

2831
/**
29-
* @var array<string, string[]>
32+
* @var MessagesArray
3033
*/
3134
private array $messages;
3235

@@ -40,7 +43,7 @@ final class ValidationResult implements ValidationResultInterface
4043
*
4144
* @param mixed[] $rawValues
4245
* @param mixed[] $values
43-
* @param array<string, string[]> $messages
46+
* @param MessagesArray $messages
4447
*/
4548
public function __construct(
4649
array $rawValues,
@@ -59,8 +62,8 @@ public static function buildFromInputFilter(InputFilterInterface $inputFilter, s
5962
$messages = [];
6063

6164
if (! $inputFilter->isValid()) {
62-
foreach ($inputFilter->getInvalidInput() as $message) {
63-
$messages[$message->getName()] = $message->getMessages();
65+
foreach ($inputFilter->getInvalidInput() as $name => $message) {
66+
$messages[$name] = $message->getMessages();
6467
}
6568
}
6669

@@ -78,16 +81,25 @@ public function isValid(): bool
7881
return (count($this->messages) === 0);
7982
}
8083

84+
/**
85+
* @return MessagesArray
86+
*/
8187
public function getMessages(): array
8288
{
8389
return $this->messages;
8490
}
8591

92+
/**
93+
* @return mixed[]
94+
*/
8695
public function getRawValues(): array
8796
{
8897
return $this->rawValues;
8998
}
9099

100+
/**
101+
* @return mixed[]
102+
*/
91103
public function getValues(): array
92104
{
93105
return $this->values;

src/Validator/ValidationResultInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace ZE\ContentValidation\Validator;
1313

14+
/**
15+
* @phpstan-type MessagesArray array<array<array<string[]|string>|string>>
16+
*/
1417
interface ValidationResultInterface
1518
{
1619
/**
@@ -23,7 +26,7 @@ public function isValid(): bool;
2326
/**
2427
* Get validation messages
2528
*
26-
* @return array<string, string[]>
29+
* @return MessagesArray
2730
*/
2831
public function getMessages(): array;
2932

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* ze-content-validation (https://github.com/func0der/ze-content-validation)
4+
*
5+
* @copyright Copyright (c) 2017 MVLabs(http://mvlabs.it)
6+
* @copyright Copyright (c) 2021 func0der
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace ZETest\ContentValidation\Extractor;
13+
14+
use Laminas\InputFilter\Input;
15+
use Laminas\InputFilter\InputFilter;
16+
use PHPUnit\Framework\TestCase;
17+
use Prophecy\PhpUnit\ProphecyTrait;
18+
use ZE\ContentValidation\Validator\ValidationResult;
19+
20+
class ValidationResultTest extends TestCase
21+
{
22+
use ProphecyTrait;
23+
24+
/**
25+
* @test
26+
*/
27+
public function itShouldProcessInvalidInputFiltersWithOnlyInputs(): void
28+
{
29+
$inputFilter = (new InputFilter())
30+
->add((new Input())->setRequired(true), 'foo')
31+
->add((new Input())->setRequired(true), 'bar');
32+
33+
$inputFilter->setData([]);
34+
35+
$validationResult = ValidationResult::buildFromInputFilter($inputFilter, 'dummy');
36+
37+
self::assertEquals(
38+
[
39+
'foo' => [
40+
'isEmpty' => 'Value is required and can\'t be empty',
41+
],
42+
'bar' => [
43+
'isEmpty' => 'Value is required and can\'t be empty',
44+
],
45+
],
46+
$validationResult->getMessages()
47+
);
48+
}
49+
50+
/**
51+
* @test
52+
*/
53+
public function itShouldProcessInvalidInputFiltersWithInputsAndInputFilters(): void
54+
{
55+
$inputFilter = (new InputFilter())
56+
->add((new Input())->setRequired(true), 'foo')
57+
->add(
58+
(new InputFilter())
59+
->add((new Input())->setRequired(true), 'nestedFooInBar'),
60+
'bar'
61+
);
62+
63+
$inputFilter->setData([]);
64+
65+
$validationResult = ValidationResult::buildFromInputFilter($inputFilter, 'dummy');
66+
67+
self::assertEquals(
68+
[
69+
'foo' => [
70+
'isEmpty' => 'Value is required and can\'t be empty',
71+
],
72+
'bar' => [
73+
'nestedFooInBar' => [
74+
'isEmpty' => 'Value is required and can\'t be empty',
75+
],
76+
],
77+
],
78+
$validationResult->getMessages()
79+
);
80+
}
81+
}

0 commit comments

Comments
 (0)