MessageCollection::__construct()   A
last analyzed

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 1
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 10 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
use WFV\Abstraction\Collectable;
5
/**
6
 *
7
 *
8
 * @since 0.11.0
9
 */
10
class MessageCollection extends Collectable {
11
12
	/**
13
	 * __construct
14
	 *
15
	 * @since 0.11.0
16
	 *
17
	 * @param array $form Config array
18
	 */
19
	public function __construct( array $form ) {
20
		$this->set_messages( $form );
21
	}
22
23
	/**
24
	 * Return the message array
25
	 *
26
	 * @since 0.11.0
27
	 *
28
	 * @return array
29
	 */
30
	public function get_array() {
31
		return $this->data;
32
	}
33
34
	/**
35
	 * Return a custom error message for a field's rule
36
	 *
37
	 * @since 0.11.0
38
	 *
39
	 * @param string $field
40
	 * @param string $rule
41
	 * @return array|null
42
	 */
43
	public function get_msg( $field, $rule ) {
44
		return ( isset( $this->data[ $field ][ $rule ] ) )
45
			? $this->data[ $field ][ $rule ]
46
			: null;
47
	}
48
49
	/**
50
	 * Filter out fields that do not have custom error messages
51
	 *  from the config array
52
	 *
53
	 * @since 0.11.0
54
	 * @access protected
55
	 *
56
	 * @param array $form
57
	 * @return array
58
	 */
59
	protected function filter_config( array $form ) {
60
		return array_filter( $form, function( $item ) {
61
			if ( array_key_exists('messages', $item ) ) {
62
				return !empty($item['messages']);
63
			}
64
		});
65
	}
66
67
	/**
68
	 *
69
	 *
70
	 * @since 0.11.0
71
	 * @access protected
72
	 *
73
	 * @param array
74
	 */
75
	protected function make_array( array $filtered ) {
76
		$messages = array();
77
		foreach( $filtered as $field => $options ) {
78
			$messages[ $field ] = $options['messages'];
79
		}
80
		return $messages;
81
	}
82
83
	/**
84
	 *
85
	 *
86
	 * @since 0.11.0
87
	 * @access protected
88
	 *
89
	 * @param array $form
90
	 */
91
	protected function set_messages( array $form ) {
92
		$filtered = $this->filter_config( $form );
93
		$this->data = $this->make_array( $filtered );
94
	}
95
}
96