Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Subfilter support #156

Open
wants to merge 9 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Failure/Failure.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getMessage(): string
* Returns the arguments passed to the rule specification.
*
*
* @return mixed[]
* @return array<string, string>
*/
public function getArgs(): array
{
Expand Down
29 changes: 25 additions & 4 deletions src/Failure/FailureCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function newFailure(string $field, string $message, array $args = arra
* @param string $field The field name.
*
*
* @return mixed[]
* @return array<int, Failure>
*/
public function forField(string $field): array
{
Expand All @@ -123,7 +123,7 @@ public function forField(string $field): array
* Returns all failure messages for all fields.
*
*
* @return mixed[][]
* @return array<string, array<int, string>>
*/
public function getMessages(): array
{
Expand All @@ -140,6 +140,8 @@ public function getMessages(): array
*
* @param string $field The field name.
*
* @return array<int, string>
*
*/
public function getMessagesForField(string $field): array
{
Expand All @@ -148,9 +150,15 @@ public function getMessagesForField(string $field): array
}

$messages = array();
foreach ($this[$field] as $failure) {
$messages[] = $failure->getMessage();

if ($this[$field] instanceof FailureCollection) {
$messages = $this[$field]->getMessages();
} else {
foreach ($this[$field] as $failure) {
$messages[] = $failure->getMessage();
}
}

return $messages;
}

Expand Down Expand Up @@ -193,4 +201,17 @@ public function getMessagesForFieldAsString(string $field, string $prefix = ''):
}
return $string;
}

public function addSubfieldFailures($field, $spec)
{
$failure_collection = new FailureCollection();

foreach ($spec->getMessage() as $f => $messages) {
foreach ($messages as $message) {
$failure_collection->add($f, $message, $spec->getArgs());
}
}

$this[$field] = $failure_collection;
}
}
2 changes: 1 addition & 1 deletion src/Spec/Spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public function getField()
*
* Returns the failure message for this rule specification.
*
* @return string
* @return string|array<string, array<int, string>>
*
*/
public function getMessage()
Expand Down
3 changes: 2 additions & 1 deletion src/Spec/SubSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function __construct(SubjectFilter $filter)
public function __invoke($subject)
{
$field = $this->field;
$values =& $subject->$field;
$field_values = isset($subject->$field) ? $subject->$field : [];
$values =& $field_values;
return $this->filter->apply($values);
}

Expand Down
18 changes: 13 additions & 5 deletions src/SubjectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function sanitize(string $field): Spec
*
*
*/
public function subfilter(string $field, $class = \Aura\Filter\SubjectFilter::class): Spec
public function subFilter(string $field, $class = \Aura\Filter\SubjectFilter::class): Spec
{
$spec = $this->sub_spec_factory->newSubSpec($class);
return $this->addSpec($spec, $field);
Expand Down Expand Up @@ -363,21 +363,29 @@ protected function applySpec(Spec $spec, object $subject): bool
*
* @param Spec $spec The failed rule specification.
*
*
* @return void
*/
protected function failed(Spec $spec): Failure
protected function failed(Spec $spec)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one place I dislike.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is return void sufficient? The caller does not use the return value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier it was returning the Failure. As it currently changed this now can return Failure or FailureCollection. ie why return is removed. I will look once again if this returned value has some value inside the caller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is to add a failure. It seems no return value is needed.

{
$field = $spec->getField();

if ($spec->isHardRule()) {
$this->skip[$field] = true;
}

if ($spec instanceof SubSpec) {
$this->failures->addSubfieldFailures($field, $spec);

return;
}

if (isset($this->field_messages[$field])) {
return $this->failures->set($field, $this->field_messages[$field]);
$this->failures->set($field, $this->field_messages[$field]);

return;
}

return $this->failures->add($field, $spec->getMessage(), $spec->getArgs());
$this->failures->add($field, $spec->getMessage(), $spec->getArgs());
}

/**
Expand Down
65 changes: 65 additions & 0 deletions tests/Spec/SubspecTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace Aura\Filter\Spec;

use Aura\Filter\FilterFactory;
use Aura\Filter\SubjectFilter;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

class SubspecTest extends TestCase
{
/** @var SubjectFilter */
protected $filter;

protected function set_up()
{
$filter_factory = new FilterFactory();
$this->filter = $filter_factory->newSubjectFilter();
}

public function testMultidimensionalSupport()
{
$data = [
'id' => 'asd',
'user' => [
'name' => 'Foo',
'age' => 'asd',
],
// 'url' => 'http://example.com'
];

$this->filter->validate('id')->is('int');
$this->filter->validate('url')->is('url');

$user_spec = $this->filter->subFilter('user'); // add a "SubSpec"
$user_filter = $user_spec->filter(); // Get the "SubSpec" SubjectFilter

$user_filter->validate('given-name')->isNotBlank();
$user_filter->validate('age')->is('int');
$user_filter->validate('gender')->is('strlen', 1);

$subject = (object) $data;
$result = $this->filter->apply($subject);
$this->assertFalse($result);
$expect = [
'id' => [
'id should have validated as int'
],
'url' => [
'url should have validated as url',
],
'user' => [
'given-name' => [
'given-name should not have been blank',
],
'age' => [
'age should have validated as int'
],
'gender' => [
'gender should have validated as strlen(1)'
]
]
];
$actual = $this->filter->getFailures()->getMessages();
$this->assertSame($expect, $actual);
}
}