Completed
Pull Request — master (#124)
by Maciej
02:05
created

RuleCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get_array() 0 3 1
A extract_name() 0 3 1
A extract_params() 0 3 1
A has_parameters() 0 3 1
A parse_rules() 0 17 3
A split_rules() 0 6 1
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 RuleCollection extends Collectable {
13
14
	/**
15
	 *
16
	 *
17
	 * @since 0.10.0
18
	 *
19
	 * @param array $rules
20
	 */
21
	public function __construct( array $rules ) {
22
		$this->data = $this->parse_rules( $rules );
23
	}
24
25
26
	/**
27
	 * Get rules array
28
	 *
29
	 * @since 0.10.0
30
	 *
31
	 * @return array
32
	 */
33
	public function get_array() {
34
		return $this->data;
35
	}
36
37
	/**
38
	 * Extract rule name from a rule string
39
	 *
40
	 * @since 0.11.0
41
	 * @access protected
42
	 *
43
	 * @param string $rule
44
	 * @return string
45
	 */
46
	protected function extract_name( $rule ) {
47
		return strstr( $rule, ':', true );
48
	}
49
50
	/**
51
	 * Extract rule parameters from a rule string
52
	 *
53
	 * @since 0.11.0
54
	 * @access protected
55
	 *
56
	 * @param string $rule
57
	 * @return string
58
	 */
59
	protected function extract_params( $rule ) {
60
		return ltrim( strstr($rule, ':'), ':');
61
	}
62
63
	/**
64
	 * Checks if a rule string has parameters
65
	 *
66
	 * @since 0.11.0
67
	 * @access protected
68
	 *
69
	 * @param string $rule
70
	 * @return bool
71
	 */
72
	protected function has_parameters( $rule ) {
73
		return strpos( $rule, ':' );
74
	}
75
76
	/**
77
	 * Split each string ruleset from config array
78
	 *  into a machine friendly multi-dimensional array
79
	 *
80
	 * @since 0.11.0
81
	 * @access protected
82
	 *
83
	 * @param array $rules
84
	 * @return array
85
	 */
86
	protected function parse_rules( array $rules ) {
87
		// WIP - works, but confusing - simplify or breakdown into small methods
88
		$parsed = array();
89
		$this->split_rules( $rules );
90
		foreach( $rules as $field => $ruleset ) {
91
			$parsed[ $field ] = array_map( function( $rule ) {
92
				if ( $this->has_parameters( $rule ) ) {
93
					return array(
94
						'rule' => $this->extract_name( $rule ),
95
						'params' => explode( ',', $this->extract_params( $rule ) )
96
					);
97
				}
98
				return $rule;
99
			}, $ruleset );
100
		}
101
		return $parsed;
102
	}
103
104
	/**
105
	 * Converts string ruleset to index array
106
	 *
107
	 * @since 0.11.0
108
	 * @access protected
109
	 *
110
	 * @param array $rules
111
	 */
112
	protected function split_rules( array &$rules ) {
113
		// perhaps the $rules array structure should be validated here?...
114
		$rules = array_map( function( $item ) {
115
			return explode( '|', $item );
116
		}, $rules );
117
	}
118
}
119