Completed
Push — master ( 6773cf...cb49b4 )
by Maciej
15s
created

FormArtisan::validator()   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 0
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 21 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\Artisan;
3
defined( 'ABSPATH' ) || die();
4
5
use WFV\Agent\InspectionAgent;
6
use WFV\Contract\ArtisanInterface;
7
use WFV\Collection\ErrorCollection;
8
use WFV\Collection\InputCollection;
9
use WFV\Collection\MessageCollection;
10
use WFV\Collection\RuleCollection;
11
12
use WFV\RuleFactory;
13
use WFV\FormComposite;
14
use WFV\Validator;
15
16
/**
17
 *
18
 *
19
 * @since 0.10.0
20
 */
21
class FormArtisan implements ArtisanInterface {
22
23
	/**
24
	 *
25
	 *
26
	 * @since 0.10.0
27
	 * @var array
28
	 */
29
	public $collection = array();
30
31
	/**
32
	 *
33
	 *
34
	 * @since 0.11.2
35
	 * @var \WFV\RuleFactory
36
	 */
37
	public $factory;
38
39
	/**
40
	 *
41
	 *
42
	 * @since 0.11.0
43
	 * @var \WFV\Validator
44
	 */
45
	public $validator;
46
47
	/**
48
	 *
49
	 *
50
	 * @since 0.10.0
51
	 * @access protected
52
	 * @var array
53
	 */
54
	protected $config = array();
55
56
	/**
57
	 *
58
	 *
59
	 * @since 0.10.0
60
	 * @access protected
61
	 * @var \WFV\FormComposite
62
	 */
63
	protected $form;
64
65
	/**
66
	 *
67
	 *
68
	 * @since 0.11.0
69
	 *
70
	 * @param array $config
71
	 */
72
	public function __construct( array $config ) {
73
		$this->config = $config;
74
	}
75
76
	/**
77
	 * Return the final Form instance
78
	 *
79
	 * @since 0.10.0
80
	 *
81
	 * @return WFV\FormComposite
82
	 */
83
	public function actualize() {
84
		return $this->form;
85
	}
86
87
	/**
88
	 * Creates the instance of FormComposite
89
	 *
90
	 * @since 0.10.0
91
	 *
92
	 * @param string $action
93
	 * @return WFV\Artisan\FormArtisan
94
	 */
95
	public function create( $action ) {
96
		$this->form = new FormComposite( $this, $action );
97
		return $this;
98
	}
99
100
	/**
101
	 * Create instance of ErrorCollection
102
	 * Save it in $collection array property
103
	 *
104
	 * @since 0.10.0
105
	 *
106
	 * @return WFV\Artisan\FormArtisan
107
	 */
108
	public function errors() {
109
		$this->collection['errors'] = new ErrorCollection( $this->labels() );
110
		return $this;
111
	}
112
113
	/**
114
	 *
115
	 *
116
	 * @since 0.11.2
117
	 *
118
	 * @return WFV\Artisan\FormArtisan
119
	 */
120
	public function factory() {
121
		$this->factory = new RuleFactory();
122
		return $this;
123
	}
124
125
	/**
126
	 * Create instance of InputCollection
127
	 * Save it in $collection array property
128
	 *
129
	 * @since 0.10.0
130
	 *
131
	 * @param InspectionAgent $guard
132
	 * @return WFV\Artisan\FormArtisan
133
	 */
134
	public function input( InspectionAgent $guard ) {
135
		$this->collection['input'] = new InputCollection( $guard );
136
		return $this;
137
	}
138
139
	/**
140
	 * Create instance of RuleCollection
141
	 * Save it in $collection array property
142
	 *
143
	 * @since 0.10.0
144
	 *
145
	 * @return WFV\Artisan\FormArtisan
146
	 */
147
	public function rules() {
148
		$rules = array();
149
		foreach( $this->config as $field => $options ) {
150
			$rules[ $field ] = $options['rules'];
151
		}
152
		$this->collection['rules'] = new RuleCollection( $rules );
153
		return $this;
154
	}
155
156
	/**
157
	 * Create instance of WFV\Validator
158
	 * Save it in $validator property
159
	 *
160
	 * @since 0.11.0
161
	 *
162
	 * @return WFV\Artisan\FormArtisan
163
	 */
164
	public function validator() {
165
		$this->validator = new Validator( new MessageCollection( $this->config ) );
166
		return $this;
167
	}
168
169
	/**
170
	 * Returns an array of human friendly field labels
171
	 *  as defined in the config array
172
	 *
173
	 * @since 0.11.0
174
	 * @access protected
175
	 *
176
	 * @return array
177
	 */
178
	protected function labels() {
179
		return array_map( function( $item ) {
180
			return $item['label'];
181
		}, $this->config);
182
	}
183
}
184