Completed
Push — master ( e0ab63...a85d1e )
by Maciej
11s
created

RuleFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
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 \Respect\Validation\Validator as RespectValidator;
6
use WFV\Rules;
7
8
/**
9
 * Flyweight factory for rules
10
 *
11
 * @since 0.11.0
12
 */
13
class RuleFactory {
14
15
	/**
16
	 * Container holds unique rules
17
	 *
18
	 * @since 0.11.0
19
	 * @access protected
20
	 * @var array
21
	 */
22
	protected $pool = array();
23
24
	/**
25
	 * Returns a rule instance
26
	 *
27
	 * @since 0.11.0
28
	 *
29
	 * @param string $rule
30
	 * @return ValidateInterface|bool
31
	 */
32
	public function get( $rule ) {
33
		if ( !isset( $this->pool[ $rule ] ) ) {
34
			$this->pool[ $rule ] = $this->create( $rule );
35
		}
36
		return $this->pool[ $rule ];
37
	}
38
39
	/**
40
	 * Returns a rule's class name
41
	 *
42
	 * @since 0.11.0
43
	 * @access protected
44
	 *
45
	 * @param string $rule
46
	 * @return string
47
	 */
48
	protected function class_name( $rule ){
49
		$name = str_replace(' ', '', ucwords( str_replace('_', ' ', $rule ) ) );
50
		return 'WFV\Rules\\'.$name;
51
	}
52
53
	/**
54
	 * Create a ValidationInterface for a rule
55
	 *
56
	 * @since 0.12.1
57
	 *
58
	 * @param string $rule
59
	 * @return ValidateInterface
60
	 */
61
	protected function create( $rule ) {
62
		$class = $this->class_name( $rule );
63
		return new $class( new RespectValidator() );
64
	}
65
}
66