Skip to content

Commit

Permalink
Merge pull request #440 from AntonShevchuk/master
Browse files Browse the repository at this point in the history
Removed `ValidatorFormException`
  • Loading branch information
Anton committed Sep 22, 2017
2 parents 4568b13 + 9ee44f0 commit 9a246b8
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 91 deletions.
18 changes: 9 additions & 9 deletions .scrutinizer.yml
@@ -1,10 +1,10 @@
checks:
php:
psr2_switch_declaration: true
psr2_class_declaration: true
no_short_open_tag: true
deprecated_code_usage: true
code_rating: true
psr2_switch_declaration: true
psr2_class_declaration: true
no_short_open_tag: true
deprecated_code_usage: true
code_rating: true
filter:
excluded_paths:
- vendor/
Expand All @@ -17,11 +17,11 @@ tools:
config:
standard: PSR2
php_cpd:
enabled: true
excluded_dirs: [vendor, tests]
enabled: true
excluded_dirs: [vendor, tests]
php_loc:
enabled: true
excluded_dirs: [vendor, tests]
enabled: true
excluded_dirs: [vendor, tests]
php_mess_detector: true
php_pdepend: true
sensiolabs_security_checker: true
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -6,7 +6,6 @@ php:
- master
matrix:
allow_failures:
- php: 7.2
- php: master
env:
global:
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -7,16 +7,16 @@
"ext-PDO": "*",
"ext-pdo_mysql": "*",
"ext-json": "*",
"cache/cache": "~0.3",
"cache/cache": "~1.0",
"psr/log": "~1.0",
"zendframework/zend-diactoros": "~1.3"
"zendframework/zend-diactoros": "~1.6"
},
"require-dev": {
"codeception/codeception": "~2.3",
"satooshi/php-coveralls": "~1.0",
"squizlabs/php_codesniffer": "~2.3",
"phploc/phploc": "*",
"phpmd/phpmd": "*"
"phploc/phploc": "~4.0",
"phpmd/phpmd": "~2.6"
},
"suggest": {
"ext-gettext": "required by Bluz\\Translator (https://github.com/bluzphp/framework/wiki/Translator)",
Expand Down
6 changes: 6 additions & 0 deletions src/Crud/AbstractCrud.php
Expand Up @@ -129,11 +129,17 @@ public function deleteSet($data)
throw new NotImplementedException;
}

/**
* @return array
*/
public function getFields(): array
{
return $this->fields;
}

/**
* @param array $fields
*/
public function setFields(array $fields)
{
$this->fields = $fields;
Expand Down
44 changes: 44 additions & 0 deletions src/Validator/Exception/ValidatorException.php
Expand Up @@ -24,4 +24,48 @@ class ValidatorException extends BadRequestException
* @var string exception message
*/
protected $message = 'Invalid Arguments';

/**
* @var array of error's messages
*/
protected $errors = [];

/**
* @return array
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* @param array $errors
*/
public function setErrors(array $errors)
{
$this->errors = $errors;
}

/**
* Add Error by field name
*
* @param string $name
* @param string $message
*
* @return void
*/
public function setError($name, $message)
{
$this->errors[$name] = $message;
}

/**
* Has errors?
*
* @return bool
*/
public function hasErrors() : bool
{
return (bool)count($this->errors);
}
}
64 changes: 0 additions & 64 deletions src/Validator/Exception/ValidatorFormException.php

This file was deleted.

9 changes: 4 additions & 5 deletions src/Validator/ValidatorChain.php
Expand Up @@ -161,7 +161,8 @@ public function validate($input) : bool
$this->error = null; // clean
foreach ($this->rules as $rule) {
if (!$rule->validate($input)) {
$this->setError($rule->getDescription());
// apply custom description or use description from rule
$this->setError($this->description ?? $rule->getDescription());
return false;
}
}
Expand Down Expand Up @@ -198,17 +199,13 @@ public function __toString() : string
return implode("\n", $this->getDescription());
}


/**
* Get error message
*
* @return null|string
*/
public function getError()
{
if ($this->description) {
return $this->description;
}
return $this->error;
}

Expand All @@ -232,10 +229,12 @@ protected function setError($message) : ValidatorChain
*/
public function getDescription() : array
{
// apply custom description
if ($this->description) {
return [$this->description];
}

// eject description from rules
return array_map('strval', $this->rules);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Validator/ValidatorForm.php
Expand Up @@ -10,7 +10,7 @@

namespace Bluz\Validator;

use Bluz\Validator\Exception\ValidatorFormException;
use Bluz\Validator\Exception\ValidatorException;

/**
* Validator Builder
Expand All @@ -36,7 +36,7 @@ class ValidatorForm implements ValidatorInterface
* ['foo'] => "some field error"
* ['bar'] => "some field error"
*
* @var ValidatorFormException
* @var ValidatorException
*/
protected $exception;

Expand All @@ -62,7 +62,7 @@ public function add($name) : ValidatorChain
*/
public function validate($input) : bool
{
$this->exception = new ValidatorFormException();
$this->exception = new ValidatorException();

// run chains
foreach ($this->validators as $key => $validators) {
Expand Down Expand Up @@ -98,7 +98,7 @@ protected function validateItem($key, $value): bool
* @param mixed $input
*
* @return void
* @throws ValidatorFormException
* @throws ValidatorException
*/
public function assert($input)
{
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Validator/ValidatorFormTest.php
Expand Up @@ -7,7 +7,7 @@
namespace Bluz\Tests\Validator;

use Bluz\Tests;
use Bluz\Validator\Exception\ValidatorFormException;
use Bluz\Validator\Exception\ValidatorException;
use Bluz\Validator\ValidatorForm;

/**
Expand All @@ -17,15 +17,15 @@
*/
class ValidatorFormTest extends Tests\FrameworkTestCase
{
public function testSetCustomDescriptionOfValidatorChainShouldBeInValidatorFormException()
public function testSetCustomDescriptionOfValidatorChainShouldBeInValidatorException()
{
$validator = new ValidatorForm();
try {
$validator->add('some')
->callback('is_int')
->setDescription('is not numeric');
$validator->assert(['some' => 'something']);
} catch (ValidatorFormException $e) {
} catch (ValidatorException $e) {
self::assertEquals('Invalid Arguments', $e->getMessage());
self::assertArrayHasKey('some', $e->getErrors());
self::assertEquals('is not numeric', $e->getErrors()['some']);
Expand All @@ -40,7 +40,7 @@ public function testAssertInvalidDataShouldRaiseExceptionWithErrorsMessages()
$validator->add('bar')->required()->callback('is_int');
$validator->add('quz')->required()->callback('is_int');
$validator->assert(['foo' => 42, 'bar' => 'something']);
} catch (ValidatorFormException $e) {
} catch (ValidatorException $e) {
self::assertEquals('Invalid Arguments', $e->getMessage());

$errors = $validator->getErrors();
Expand Down

0 comments on commit 9a246b8

Please sign in to comment.