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