Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

revoking earlier decision; setRule() should deal with one rule per fi…

…eld, not multiple rules
  • Loading branch information...
commit a13077a79cda6cae07ba18fc2f7bf2009f54c730 1 parent 7fdf46c
Paul M. Jones pmjones authored
Showing with 13 additions and 52 deletions.
  1. +13 −16 src/Aura/Input/Filter.php
  2. +0 −36 tests/Aura/Input/FilterTest.php
29 src/Aura/Input/Filter.php
View
@@ -36,7 +36,7 @@ class Filter implements FilterInterface
*/
public function setRule($field, $message, \Closure $closure)
{
- $this->rules[$field][] = [$message, $closure];
+ $this->rules[$field] = [$message, $closure];
}
/**
@@ -54,21 +54,18 @@ public function values(&$values)
$this->messages = [];
// go through each of the rules
- foreach ($this->rules as $field => $rules) {
- // A field can have more rules
- foreach ($rules as $rule) {
- // get the closure and message
- list($message, $closure) = $rule;
-
- // apply the closure to the data and get back the result
- $passed = $closure($values[$field]);
-
- // if the rule did not pass, retain a message for the field.
- // note that it is in an array, so that other implementations
- // can allow for multiple messages.
- if (! $passed) {
- $this->messages[$field][] = $message;
- }
+ foreach ($this->rules as $field => $rule) {
+ // get the message and closure
+ list($message, $closure) = $rule;
+
+ // apply the closure to the data and get back the result
+ $passed = $closure($values[$field]);
+
+ // if the rule did not pass, retain a message for the field.
+ // note that it is in an array, so that other implementations
+ // can allow for multiple messages.
+ if (! $passed) {
+ $this->messages[$field][] = $message;
}
}
36 tests/Aura/Input/FilterTest.php
View
@@ -14,11 +14,6 @@ protected function setUp()
return ctype_alpha($value);
});
- $this->filter->setRule('foo', 'Foo should be more than 7 characters', function ($value) {
- $length = strlen($value);
- return ($length > 7);
- });
-
// sanitize
$this->filter->setRule('bar', 'Remove non-alpha from bar', function (&$value) {
$value = preg_replace('/[^a-z]/i', '!', $value);
@@ -70,36 +65,5 @@ public function testAll()
$data['foo'] = 'foovalue';
$passed = $this->filter->values($data);
$this->assertTrue($passed);
-
- $data['foo'] = 'fooval';
- $passed = $this->filter->values($data);
- $this->assertFalse($passed);
- $expect = [
- 'Foo should be more than 7 characters',
- ];
- $this->assertSame($expect, $this->filter->getMessages('foo'));
-
- // Get more errors
- $values = [
- 'foo' => 'foo_va',
- 'bar' => 'bar_value',
- ];
-
- // do the values pass all filters?
- $passed = $this->filter->values($values);
-
- // 'foo' is invalid
- $this->assertFalse($passed);
-
- // get all messages
- $actual = $this->filter->getMessages();
- $expect = [
- 'foo' => [
- 'Foo should be alpha only',
- 'Foo should be more than 7 characters'
- ]
- ];
- $this->assertSame($expect, $actual);
-
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.