Skip to content

Commit

Permalink
Implement remainder.
Browse files Browse the repository at this point in the history
  • Loading branch information
romeOz committed Mar 23, 2015
1 parent 1eeea8f commit 59eafa8
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 40 deletions.
49 changes: 49 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Rules
* [v::notOf()](#vnotofv-v)
* [v::oneOf()](#voneofv-v)
* [v::when()](#vwhenv-if-v-then-v-else--null)
* [remainder](#remainder)

### [Comparing Values](#comparing-values-1)

Expand Down Expand Up @@ -309,6 +310,54 @@ output:
In the sample above, if `$input` is an integer, then it must be positive.
If `$input` is not an integer, then it must not me empty.

####remainder

Default label `*`.

```php
use rock\validate\Validate as v;

$input = [
'#' => 5,
'email' => 'tom@site',
'name' => 'Tom',
'age' => 15
];

$validate = v::attributes(
[
'#' => Validate::int(),
'email' => Validate::required()->email(),
'*' => Validate::required()->string(),
]
);
$validate->validate($input); // output: false

/*
output:
[
'age' => [
'string' => 'value must be string',
],
'email' => [
'email' => 'email must be valid',
],
]
*/
```

Change default label:

```php
$validate = v::labelRemainder('_remainder')
->attributes(
[
'#' => Validate::int(),
'email' => Validate::required()->email(),
'_remainder' => Validate::required()->string(),
]
);
```

### Comparing Values

Expand Down
17 changes: 17 additions & 0 deletions src/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use rock\base\ObjectInterface;
use rock\base\ObjectTrait;
use rock\helpers\ArrayHelper;


class Attributes implements ObjectInterface
Expand All @@ -14,6 +15,7 @@ class Attributes implements ObjectInterface
public $attributes = [];
public $valid = true;
public $one = false;
public $remainder = '*';
protected $errors = [];

public function validate($input)
Expand All @@ -28,6 +30,10 @@ public function validate($input)
if (!$validate instanceof Validate) {
throw new ValidateException("`{$attribute}` is not `".Validate::className()."`");
}
if ($attribute === $this->remainder) {
$this->remainder($validate, $input);
continue;
}
if (!isset($input[$attribute])) {
$input[$attribute] = null;
}
Expand Down Expand Up @@ -59,4 +65,15 @@ protected function each($input)
$this->attributes[$key] = $validate;
}
}

protected function remainder(Validate $validate, $input)
{
$input = ArrayHelper::diffByKeys($input, array_keys($this->attributes));
$config = [
'remainder' => $this->remainder,
'attributes' => $validate
];
$this->setProperties($config);
$this->validate($input);
}
}
87 changes: 47 additions & 40 deletions src/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* @method static Validate when(Validate $if, Validate $then, Validate $else = null)
* @method static Validate locale(string $locale)
* @method static Validate skipEmpty(bool $skip = true)
* @method static Validate labelRemainder(string $label = '*')
*
* @method static Validate alnum(string $additionalChars = null)
* @method static Validate alpha(string $additionalChars = null)
Expand Down Expand Up @@ -175,6 +176,7 @@ class Validate implements ObjectInterface
* is null or an empty string.
*/
public $skipEmpty = true;
public $remainder = '*';
/** @var array */
protected $_rules = [];
/**
Expand Down Expand Up @@ -260,45 +262,44 @@ public function validate($input)
foreach($this->_rules as $rules){

list($ruleName, $rule) = $rules;
// if (!is_array($rules)) {
// $rules = [$rules];
// }
// foreach($rules as $rule) {

// notOf or oneOf
if ($rule instanceof Validate) {
$rule->validate($input);
$this->errors = array_merge($this->errors, $rule->getErrors());
continue;
}

if ($rule instanceof When) {
$rule->valid = $this->valid;
$rule->validate($input);
$this->errors = array_merge($this->errors, $rule->getErrors());
continue;
}

if ($rule instanceof Attributes) {
$rule->one = $this->one;
$rule->valid = $this->valid;
$rule->validate($input);
$this->errors = $rule->getErrors();
break;
}

if ($this->skipEmpty && $rule->skipEmpty && $this->isEmpty($input, $rule)) {
continue;
}

if ($rule->validate($input) === $this->valid) {
continue;
}
$this->errors[$ruleName] = $this->error($ruleName, $rule);
if ($this->one === true) {
break;
}
//}

// notOf or oneOf
if ($rule instanceof Validate) {
$rule->validate($input);
$this->errors = array_merge($this->errors, $rule->getErrors());
continue;
}

if ($rule instanceof When) {
$rule->valid = $this->valid;
$rule->validate($input);
$this->errors = array_merge($this->errors, $rule->getErrors());
continue;
}

if ($rule instanceof Attributes) {
$config = [
'one' => $this->one,
'valid' => $this->valid,
'remainder' => $this->remainder
];
$rule->setProperties($config);
$rule->validate($input);
$this->errors = $rule->getErrors();
break;
}

if ($this->skipEmpty && $rule->skipEmpty && $this->isEmpty($input, $rule)) {
continue;
}

if ($rule->validate($input) === $this->valid) {
continue;
}
$this->errors[$ruleName] = $this->error($ruleName, $rule);
if ($this->one === true) {
break;
}
}
return empty($this->errors);
}
Expand Down Expand Up @@ -414,7 +415,7 @@ protected function isEmpty($value, Rule $rule)
protected function attributesInternal($attributes)
{
$this->_rules = [];
$this->_rules[] = ['attributes', new Attributes(['attributes' => $attributes, 'valid' => $this->valid])];
$this->_rules[] = ['attributes', new Attributes(['attributes' => $attributes, 'one' => $this->one, 'valid' => $this->valid, 'remainder' => $this->remainder])];
return $this;
}

Expand Down Expand Up @@ -485,6 +486,12 @@ protected function SkipEmptyInternal($skip = true)
return $this;
}

protected function labelRemainderInternal($label = '*')
{
$this->remainder = $label;
return $this;
}

/**
* Returns instance.
*
Expand Down
69 changes: 69 additions & 0 deletions tests/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,75 @@ public function testSkipEmpty()
'email' => 'email must be valid',
), $v->getErrors());
}

public function testRemainder()
{
$input = [
'#' => 5,
'email' => 'tom@site',
'name' => 'Tom',
'age' => 15
];
$validate = Validate::attributes(
[
'*' => Validate::required()->string(),
'#' => Validate::int(),
'email' => Validate::required()->email(),

]
);

$this->assertFalse($validate->validate($input));
$expected = [
'age' =>
[
'string' => 'value must be string',
],
'email' =>
[
'email' => 'email must be valid',
],
];
$this->assertEquals($expected, $validate->getErrors());

$validate = Validate::attributes(
[
'#' => Validate::int(),
'email' => Validate::required()->email(),
'_rem' => Validate::required()->string(),

]
);
$this->assertFalse($validate->labelRemainder('_rem')->validate($input));
$expected = [
'email' =>
[
'email' => 'email must be valid',
],
'age' =>
[
'string' => 'value must be string',
],
];
$this->assertEquals($expected, $validate->getErrors());

$input = [
'#' => 5,
'email' => 'tom@site.com',
'name' => 'Tom',
'age' => '15'
];

$validate = Validate::attributes(
[
'#' => Validate::int(),
'email' => Validate::required()->email(),
'_rem' => Validate::required()->string(),

]
);
$this->assertTrue($validate->labelRemainder('_rem')->validate($input));
}
}


Expand Down

0 comments on commit 59eafa8

Please sign in to comment.