Completed
Push — master ( 362609...afa8c3 )
by Maciej
15s
created

Validator::errors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 3.

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.

Loading history...
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 );
0 ignored issues
show
Unused Code introduced by
The call to ValidateInterface::validate() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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