Skip to content

Commit

Permalink
馃憣 IMPROVE: Add imploded errors #5
Browse files Browse the repository at this point in the history
  • Loading branch information
elie29 committed Feb 21, 2019
1 parent 5652f8f commit e30db63
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Changelog

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## V1.0.3 - TBD
## V1.0.3 - 2019-02-21

### Added

- Nothing.
- [#5](https://github.com/elie29/validator/issues/5) Add imploded errors.

### Changed

Expand Down
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ class XXXRule extends AbstractRule
return RuleInterface::VALID;
}
}

```

## Assertion Integration

Instead of using assertion key by key, you can validate the whole context and than use [Assertion](https://github.com/beberlei/assert) or [Assert](https://github.com/webmozart/assert) as follow:

```php
<?php

use Assert\Assertion;
use Elie\Validator\Rule\EmailRule;
use Elie\Validator\Rule\NumericRule;
use Elie\Validator\Rule\RuleConstInterface;
use Elie\Validator\Rule\StringRule;
use Elie\Validator\Validator;
use Webmozart\Assert\Assert;

$rules =[
['age', NumericRule::class, RuleConstInterface::MAX => 60],
['name', StringRule::class, RuleConstInterface::MIN => 1, RuleConstInterface::REQUIRED => true],
['email', EmailRule::class, RuleConstInterface::REQUIRED => true],
];

$validator = new Validator($_POST, $rules);

Assert::true($validator->validate(), $validator->getImplodedErrors());

// OR

Assertion::true($validator->validate(), $validator->getImplodedErrors());
```
5 changes: 5 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function getErrors(): array
return $this->errors;
}

public function getImplodedErrors(string $separator = '<br/>'): string
{
return implode($separator, $this->errors);
}

public function get(string $key)
{
return $this->context[$key] ?? null;
Expand Down
7 changes: 7 additions & 0 deletions src/ValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public function getRules(): array;
*/
public function getErrors(): array;

/**
* Return all errors found.
*
* @return string
*/
public function getImplodedErrors(string $separator = '<br/>'): string;

/**
* Retrieves the value of a requested key from the {@link context}.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Elie\Validator;

use Elie\Validator\Rule\ArrayRule;
use Elie\Validator\Rule\BooleanRule;
use Elie\Validator\Rule\NumericRule;
use Elie\Validator\Rule\StringRule;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -55,6 +57,25 @@ public function testValidatorStopOnError(): void
assertThat($validator->shouldStopOnError(), is(true));
}

public function testValidatorGetImplodedErrors(): void
{
$validator = new Validator(['name' => 'Ben'], [
['name', NumericRule::class],
['name', ArrayRule::class],
['name', BooleanRule::class],
]);

$res = $validator->validate();
assertThat($res, is(false));

// value should not exist on error
$validatedContext = $validator->getValidatedContext();
assertThat($validatedContext, emptyArray());

$expected = 'name: Ben is not numeric,name does not have an array value: Ben,name: Ben is not a valid boolean';
assertThat($validator->getImplodedErrors(','), is($expected));
}

/**
* @dataProvider getValidatorProvider
*/
Expand Down

0 comments on commit e30db63

Please sign in to comment.