|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace WFV; |
|
3
|
|
|
defined( 'ABSPATH' ) || die(); |
|
4
|
|
|
|
|
5
|
|
|
use WFV\Collection\MessageCollection; |
|
6
|
|
|
use WFV\Contract\ValidateInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Validates field/rule pairs using provided strategy classes |
|
10
|
|
|
* |
|
11
|
|
|
* @since 0.11.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
class Validator { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Container for error messages for rule/field pairs |
|
17
|
|
|
* Only contains messages for validations that failed |
|
18
|
|
|
* |
|
19
|
|
|
* @since 0.11.0 |
|
20
|
|
|
* @access protected |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $errors = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* |
|
28
|
|
|
* @since 0.11.0 |
|
29
|
|
|
* @access protected |
|
30
|
|
|
* @var MessageCollection |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $messages; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* |
|
37
|
|
|
* @since 0.11.0 |
|
38
|
|
|
* |
|
39
|
|
|
* @param |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( MessageCollection $messages ) { |
|
42
|
|
|
$this->messages = $messages; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns the array of error messages |
|
47
|
|
|
* |
|
48
|
|
|
* @since 0.11.0 |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function errors() { |
|
53
|
|
|
return $this->errors; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Did the full validation cycle pass or fail? |
|
58
|
|
|
* |
|
59
|
|
|
* @since 0.11.0 |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function is_valid() { |
|
64
|
|
|
return empty( $this->errors ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Validate a single input using provided rule (strategy) |
|
69
|
|
|
* |
|
70
|
|
|
* @since 0.11.0 |
|
71
|
|
|
* |
|
72
|
|
|
* @param ValidateInterface $rule |
|
73
|
|
|
* @param string $field |
|
74
|
|
|
* @param string|array $value |
|
75
|
|
|
* @param bool $optional |
|
76
|
|
|
* @param array (optional) $params |
|
77
|
|
|
*/ |
|
78
|
|
|
public function validate( ValidateInterface $rule, $field, $value, $optional, $params = false ) { |
|
79
|
|
|
$params[] = ( $params ) ? $field : false; |
|
80
|
|
|
$valid = $rule->validate( $value, $optional, $params ); |
|
|
|
|
|
|
81
|
|
|
if( !$valid ){ |
|
82
|
|
|
$this->add_error( $field, $rule->template() ); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Add a single error msg for a field's rule if it failed validating |
|
88
|
|
|
* |
|
89
|
|
|
* @since 0.11.0 |
|
90
|
|
|
* @access protected |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $field |
|
93
|
|
|
* @param array $template |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function add_error( $field, array $template ) { |
|
96
|
|
|
$message = ( $this->messages->has( $field ) ) |
|
97
|
|
|
? $this->messages->get_msg( $field, $template['name'] ) |
|
98
|
|
|
: $template['message']; |
|
99
|
|
|
$this->errors[ $field ][ $template['name'] ] = $message; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.