Completed
Push — master ( afa8c3...2c7ee4 )
by Maciej
14s
created

ErrorCollection::with_labels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 9.4285
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 12 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\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 = [] ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

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