|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace WFV\Collection; |
|
3
|
|
|
defined( 'ABSPATH' ) || die(); |
|
4
|
|
|
|
|
5
|
|
|
use WFV\Abstraction\Collectable; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
* |
|
10
|
|
|
* @since 0.10.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class ErrorCollection extends Collectable { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Human friendly labels for the fields |
|
16
|
|
|
* |
|
17
|
|
|
* @since 0.11.0 |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $labels = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* |
|
25
|
|
|
* @since 0.10.0 |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $labels Human friendly labels for the fields |
|
28
|
|
|
*/ |
|
29
|
|
|
function __construct( array $labels = [] ) { |
|
|
|
|
|
|
30
|
|
|
$this->labels = $labels; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Returns the first error message for a field |
|
35
|
|
|
* |
|
36
|
|
|
* @since 0.10.0 |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $field |
|
39
|
|
|
* @return string|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public function first( $field ) { |
|
42
|
|
|
return ( isset( $this->data[ $field ] ) ) |
|
43
|
|
|
? array_values( $this->data[ $field ] )[0] |
|
44
|
|
|
: null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns all errors for given field |
|
49
|
|
|
* |
|
50
|
|
|
* @since 0.11.0 |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $field |
|
53
|
|
|
* @return array|null |
|
54
|
|
|
*/ |
|
55
|
|
|
public function get( $field ) { |
|
56
|
|
|
return ( isset( $this->data[ $field ] ) ) |
|
57
|
|
|
? $this->data[ $field ] |
|
58
|
|
|
: null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Populates the collection if it's empty with given errors |
|
63
|
|
|
* Does nothing if collection is populated |
|
64
|
|
|
* |
|
65
|
|
|
* @since 0.10.0 |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $errors |
|
68
|
|
|
*/ |
|
69
|
|
|
public function set_errors( array $errors = [] ) { |
|
70
|
|
|
$this->data = ( $this->is_populated() ) |
|
71
|
|
|
? $this->data |
|
72
|
|
|
: $this->with_labels( $errors ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return the defined label for a field |
|
77
|
|
|
* If label is undefined, returns field name |
|
78
|
|
|
* |
|
79
|
|
|
* @since 0.11.0 |
|
80
|
|
|
* @access protected |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $field |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function label( $field ) { |
|
86
|
|
|
return ( isset( $this->labels[ $field ] ) ) |
|
87
|
|
|
? $this->labels[ $field ] |
|
88
|
|
|
: $field; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Replaces {label} in error msg strings with a field label |
|
93
|
|
|
* |
|
94
|
|
|
* @since 0.11.0 |
|
95
|
|
|
* @access protected |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $errors |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function with_labels( $errors ) { |
|
101
|
|
|
$labeled = array(); |
|
102
|
|
|
foreach( $errors as $field => $messages ) { |
|
103
|
|
|
$label = $this->label( $field ); |
|
104
|
|
|
$labeled[ $field ] = str_replace( '{label}', $label, $errors[ $field ] ); |
|
105
|
|
|
} |
|
106
|
|
|
return $labeled; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
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.