Skip to content

Commit

Permalink
Allow nested input filter definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
func0der committed Jul 26, 2023
1 parent 5ef9a82 commit 5017419
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 11 deletions.
7 changes: 1 addition & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ parameters:
path: src/Extractor/OptionsExtractorFactory.php

-
message: "#^Call to an undefined method Laminas\\\\InputFilter\\\\InputFilterInterface\\|Laminas\\\\InputFilter\\\\InputInterface\\:\\:getName\\(\\)\\.$#"
count: 1
path: src/Validator/ValidationResult.php

-
message: "#^Parameter \\#3 \\$messages of class ZE\\\\ContentValidation\\\\Validator\\\\ValidationResult constructor expects array\\<string, array\\<string\\>\\>, array\\<string, array\\<array\\<string\\>\\|string\\>\\> given\\.$#"
message: "#^Parameter \\#2 \\$values of class ZE\\\\ContentValidation\\\\Validator\\\\ValidationResult constructor expects array, mixed given\\.$#"
count: 1
path: src/Validator/ValidationResult.php
20 changes: 16 additions & 4 deletions src/Validator/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Laminas\InputFilter\InputFilterInterface;

/**
* @phpstan-import-type MessagesArray from ValidationResultInterface
*/
final class ValidationResult implements ValidationResultInterface
{
/**
Expand All @@ -26,7 +29,7 @@ final class ValidationResult implements ValidationResultInterface
private array $values;

/**
* @var array<string, string[]>
* @var MessagesArray
*/
private array $messages;

Expand All @@ -40,7 +43,7 @@ final class ValidationResult implements ValidationResultInterface
*
* @param mixed[] $rawValues
* @param mixed[] $values
* @param array<string, string[]> $messages
* @param MessagesArray $messages
*/
public function __construct(
array $rawValues,
Expand All @@ -59,8 +62,8 @@ public static function buildFromInputFilter(InputFilterInterface $inputFilter, s
$messages = [];

if (! $inputFilter->isValid()) {
foreach ($inputFilter->getInvalidInput() as $message) {
$messages[$message->getName()] = $message->getMessages();
foreach ($inputFilter->getInvalidInput() as $name => $message) {
$messages[$name] = $message->getMessages();
}
}

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

/**
* @return MessagesArray
*/
public function getMessages(): array
{
return $this->messages;
}

/**
* @return mixed[]
*/
public function getRawValues(): array
{
return $this->rawValues;
}

/**
* @return mixed[]
*/
public function getValues(): array
{
return $this->values;
Expand Down
5 changes: 4 additions & 1 deletion src/Validator/ValidationResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace ZE\ContentValidation\Validator;

/**
* @phpstan-type MessagesArray array<array<array<string[]|string>|string>>
*/
interface ValidationResultInterface
{
/**
Expand All @@ -23,7 +26,7 @@ public function isValid(): bool;
/**
* Get validation messages
*
* @return array<string, string[]>
* @return MessagesArray
*/
public function getMessages(): array;

Expand Down
81 changes: 81 additions & 0 deletions test/Extractor/ValidationResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* ze-content-validation (https://github.com/func0der/ze-content-validation)
*
* @copyright Copyright (c) 2017 MVLabs(http://mvlabs.it)
* @copyright Copyright (c) 2021 func0der
* @license MIT
*/

declare(strict_types=1);

namespace ZETest\ContentValidation\Extractor;

use Laminas\InputFilter\Input;
use Laminas\InputFilter\InputFilter;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use ZE\ContentValidation\Validator\ValidationResult;

class ValidationResultTest extends TestCase
{
use ProphecyTrait;

/**
* @test
*/
public function itShouldProcessInvalidInputFiltersWithOnlyInputs(): void
{
$inputFilter = (new InputFilter())
->add((new Input())->setRequired(true), 'foo')
->add((new Input())->setRequired(true), 'bar');

$inputFilter->setData([]);

$validationResult = ValidationResult::buildFromInputFilter($inputFilter, 'dummy');

self::assertEquals(
[
'foo' => [
'isEmpty' => 'Value is required and can\'t be empty',
],
'bar' => [
'isEmpty' => 'Value is required and can\'t be empty',
],
],
$validationResult->getMessages()
);
}

/**
* @test
*/
public function itShouldProcessInvalidInputFiltersWithInputsAndInputFilters(): void
{
$inputFilter = (new InputFilter())
->add((new Input())->setRequired(true), 'foo')
->add(
(new InputFilter())
->add((new Input())->setRequired(true), 'nestedFooInBar'),
'bar'
);

$inputFilter->setData([]);

$validationResult = ValidationResult::buildFromInputFilter($inputFilter, 'dummy');

self::assertEquals(
[
'foo' => [
'isEmpty' => 'Value is required and can\'t be empty',
],
'bar' => [
'nestedFooInBar' => [
'isEmpty' => 'Value is required and can\'t be empty',
],
],
],
$validationResult->getMessages()
);
}
}

0 comments on commit 5017419

Please sign in to comment.