Skip to content

Commit 5b3a2dc

Browse files
committed
Rewrite configuration mechanic
Reason: * to make autocompletion of IDEs possible. * give more keys filtering options
1 parent d1a83e9 commit 5b3a2dc

23 files changed

+330
-347
lines changed

CHANGELOG.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## [v2.0.0] - Unreleased
88

99
### Added
10-
11-
- Dynamic manipulation with Validator rules are possible now.
12-
- New methods:
13-
* Validator::getRule - get configurations of specific rule
14-
* Validator::setRule - set configuration of specific rule
15-
* Validator::addRule - append additional configuration to specific rule
10+
- A possibility to more precisely specify the list of fields for a rule.
1611

1712
### Changed
18-
13+
- Changed the way how to configure the validator
14+
- Validation state is not saved in Validator object anymore
1915
- Vendor renamed to fresh-advance
20-
- Improved readme with installation instructions
16+
- Improved readme with installation instructions and new way to configure
2117

2218
## [v1.0.0] - 2018-05-24
2319

24-
Tested for a while, stable version.
20+
Very basic implementation. Tested for a while, stable version.
2521

2622
### Added
27-
2823
- Implemented rules (See README.md for more information):
2924
* Required
3025
* Expression

Source/Keys/Collection.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public function __construct(string ...$list)
1313

1414
public function filter(array $keys): array
1515
{
16-
$intersection = array_intersect($keys, $this->fieldsList);
17-
return array_values($intersection);
16+
return $this->fieldsList;
1817
}
1918
}

Source/Rule/AbstractRule.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,7 @@
22

33
namespace Sieg\ArrayValidator\Rule;
44

5-
use Sieg\ArrayValidator\Exception\RuleFailed;
6-
7-
abstract class AbstractRule
5+
abstract class AbstractRule implements RuleInterface
86
{
97
public const MESSAGE = 'VALIDATOR_RULE_MESSAGE';
10-
11-
/**
12-
* @var mixed[]
13-
*/
14-
protected $config = [
15-
'message' => '',
16-
'fields' => []
17-
];
18-
19-
/**
20-
* AbstractRule constructor.
21-
*
22-
* @param mixed[] $config
23-
*/
24-
public function __construct($config = [])
25-
{
26-
$this->config = array_merge($this->config, $config);
27-
}
28-
29-
/**
30-
* @param mixed[] $data
31-
*
32-
* @throws RuleFailed
33-
*/
34-
abstract public function process(string $key, array $data): bool;
358
}

Source/Rule/Callback.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ class Callback extends AbstractRule
88
{
99
public const MESSAGE = 'VALIDATOR_RULE_CALLBACK_FAILED';
1010

11+
private $callback;
12+
13+
public function __construct($callback)
14+
{
15+
$this->callback = $callback;
16+
}
17+
1118
/**
1219
* @param mixed[] $data
1320
*/
1421
public function process(string $key, array $data): bool
1522
{
16-
$message = $this->config['message'] ?: self::MESSAGE;
17-
$function = $this->config['callback'];
23+
$function = $this->callback;
1824

1925
if (!$function($key, $data)) {
20-
throw new RuleFailed($message);
26+
throw new RuleFailed(self::MESSAGE);
2127
}
2228

2329
return true;

Source/Rule/Equals.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@ class Equals extends AbstractRule
88
{
99
public const MESSAGE = 'VALIDATOR_RULE_EQUALS_MATCH_FAILED';
1010

11+
private $value;
12+
private $key;
13+
14+
public function __construct($value = null, string $key = null)
15+
{
16+
$this->value = $value;
17+
$this->key = $key;
18+
}
19+
1120
/**
1221
* @param mixed[] $data
1322
*/
1423
public function process(string $key, array $data): bool
1524
{
16-
$message = $this->config['message'] ?: self::MESSAGE;
17-
18-
$this->checkKeyOption($key, $data, $message);
19-
$this->checkValueOption($key, $data, $message);
25+
$this->checkKeyOption($key, $data);
26+
$this->checkValueOption($key, $data);
2027

2128
return true;
2229
}
@@ -26,13 +33,13 @@ public function process(string $key, array $data): bool
2633
*
2734
* @throws RuleFailed
2835
*/
29-
protected function checkKeyOption(string $key, array $data, string $message): void
36+
protected function checkKeyOption(string $key, array $data): void
3037
{
3138
if (
32-
isset($this->config['key']) &&
33-
(!isset($data[$this->config['key']]) || $data[$key] !== $data[$this->config['key']])
39+
!is_null($this->key) &&
40+
(!isset($data[$this->key]) || $data[$key] !== $data[$this->key])
3441
) {
35-
throw new RuleFailed($message);
42+
throw new RuleFailed(self::MESSAGE);
3643
}
3744
}
3845

@@ -41,10 +48,10 @@ protected function checkKeyOption(string $key, array $data, string $message): vo
4148
*
4249
* @throws RuleFailed
4350
*/
44-
protected function checkValueOption(string $key, array $data, string $message): void
51+
protected function checkValueOption(string $key, array $data): void
4552
{
46-
if (isset($this->config['value']) && $data[$key] !== $this->config['value']) {
47-
throw new RuleFailed($message);
53+
if (!is_null($this->value) && $data[$key] !== $this->value) {
54+
throw new RuleFailed(self::MESSAGE);
4855
}
4956
}
5057
}

Source/Rule/Expression.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ class Expression extends AbstractRule
88
{
99
public const MESSAGE = 'VALIDATOR_RULE_EXPRESSION_MATCH_FAILED';
1010

11+
private string $expression;
12+
13+
public function __construct(string $expression)
14+
{
15+
$this->expression = $expression;
16+
}
17+
1118
/**
1219
* @param mixed[] $data
1320
*/
1421
public function process(string $key, array $data): bool
1522
{
16-
$message = $this->config['message'] ?: self::MESSAGE;
17-
18-
if (!isset($data[$key]) || !preg_match($this->config['pattern'], $data[$key])) {
19-
throw new RuleFailed($message);
23+
if (!isset($data[$key]) || !preg_match($this->expression, $data[$key])) {
24+
throw new RuleFailed(self::MESSAGE);
2025
}
2126

2227
return true;

Source/Rule/Filter.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ class Filter extends AbstractRule
88
{
99
public const MESSAGE = 'VALIDATOR_RULE_FILTER_FAILED';
1010

11+
private string $rule;
12+
private $options;
13+
14+
public function __construct(string $rule, array $options = null)
15+
{
16+
$this->rule = $rule;
17+
$this->options = $options;
18+
}
19+
1120
/**
1221
* @param mixed[] $data
1322
*/
1423
public function process(string $key, array $data): bool
1524
{
16-
$message = $this->config['message'] ?: self::MESSAGE;
17-
$options = isset($this->config['options']) ? $this->config['options'] : null;
18-
19-
if (!isset($data[$key]) || false === filter_var($data[$key], $this->config['rule'], $options)) {
20-
throw new RuleFailed($message);
25+
if (!isset($data[$key]) || false === filter_var($data[$key], $this->rule, $this->options)) {
26+
throw new RuleFailed(self::MESSAGE);
2127
}
2228

2329
return true;

Source/Rule/Length.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ class Length extends AbstractRule
88
{
99
public const MESSAGE = 'VALIDATOR_RULE_LENGTH_FAILED';
1010

11+
private $min;
12+
private $max;
13+
private $actual;
14+
15+
public function __construct(int $min = null, int $max = null, int $actual = null)
16+
{
17+
$this->min = $min;
18+
$this->max = $max;
19+
$this->actual = $actual;
20+
}
21+
1122
/**
1223
* @param mixed[] $data
1324
*/
@@ -27,8 +38,8 @@ public function process(string $key, array $data): bool
2738
*/
2839
protected function checkMinOption($item): void
2940
{
30-
if (isset($this->config['min']) && strlen($item) < $this->config['min']) {
31-
throw new RuleFailed($this->getMessage());
41+
if (!is_null($this->min) && strlen($item) < $this->min) {
42+
throw new RuleFailed(self::MESSAGE);
3243
}
3344
}
3445

@@ -39,8 +50,8 @@ protected function checkMinOption($item): void
3950
*/
4051
protected function checkMaxOption($item): void
4152
{
42-
if (isset($this->config['max']) && strlen($item) > $this->config['max']) {
43-
throw new RuleFailed($this->getMessage());
53+
if (!is_null($this->max) && strlen($item) > $this->max) {
54+
throw new RuleFailed(self::MESSAGE);
4455
}
4556
}
4657

@@ -51,13 +62,8 @@ protected function checkMaxOption($item): void
5162
*/
5263
protected function checkActualOption($item): void
5364
{
54-
if (isset($this->config['actual']) && strlen($item) !== $this->config['actual']) {
55-
throw new RuleFailed($this->getMessage());
65+
if (!is_null($this->actual) && strlen($item) !== $this->actual) {
66+
throw new RuleFailed(self::MESSAGE);
5667
}
5768
}
58-
59-
protected function getMessage(): string
60-
{
61-
return $this->config['message'] ?: self::MESSAGE;
62-
}
6369
}

Source/Rule/Required.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ class Required extends AbstractRule
1313
*/
1414
public function process(string $key, array $data): bool
1515
{
16-
$message = $this->config['message'] ?: self::MESSAGE;
17-
1816
if (!isset($data[$key]) || is_null($data[$key]) || $data[$key] === "") {
19-
throw new RuleFailed($message);
17+
throw new RuleFailed(self::MESSAGE);
2018
}
2119

2220
return true;

Source/Rule/RuleInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Sieg\ArrayValidator\Rule;
4+
5+
use Sieg\ArrayValidator\Exception\RuleFailed;
6+
7+
interface RuleInterface
8+
{
9+
/**
10+
* @param mixed[] $data
11+
*
12+
* @throws RuleFailed
13+
*/
14+
public function process(string $key, array $data): bool;
15+
}

0 commit comments

Comments
 (0)