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 4 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
23 changes: 21 additions & 2 deletions src/Failure/FailureCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,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 +199,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
14 changes: 9 additions & 5 deletions src/SubjectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,23 @@ protected function applySpec(Spec $spec, object $subject): bool
*
*
*/
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 (isset($this->field_messages[$field])) {
return $this->failures->set($field, $this->field_messages[$field]);
}
if ($spec instanceof SubSpec) {
return $this->failures->addSubfieldFailures($field, $spec);
} else {
if (isset($this->field_messages[$field])) {
return $this->failures->set($field, $this->field_messages[$field]);
}

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

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

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

class SubspecTest extends TestCase
{
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"
harikt marked this conversation as resolved.
Show resolved Hide resolved
$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);
}
}
kenjis marked this conversation as resolved.
Show resolved Hide resolved
harikt marked this conversation as resolved.
Show resolved Hide resolved