FormBuilder.php
3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Builders;
use YahnisElsts\AdminMenuEditor\Customizable\Controls;
use YahnisElsts\AdminMenuEditor\Customizable\Rendering\Renderer;
use YahnisElsts\AdminMenuEditor\Customizable\SettingsForm;
use YahnisElsts\AdminMenuEditor\Customizable\UpdateRequestHandler;
class FormBuilder {
protected $params = array();
public function structure(Controls\InterfaceStructure $structure) {
$this->params['structure'] = $structure;
return $this;
}
/**
* @param array<string,\YahnisElsts\AdminMenuEditor\Customizable\Settings\AbstractSetting> $settings
* @return $this
*/
public function settings($settings) {
$this->params['settings'] = $settings;
return $this;
}
public function renderer(Renderer $renderer) {
$this->params['renderer'] = $renderer;
return $this;
}
/**
* Set the action name that will be used for nonce generation, hooks,
* and the hidden "action" field.
*
* Not to be confused with the "action" attribute of a form element.
* Use the submitUrl() method to set that.
*
* @param string $actionName
* @return $this
*/
public function actionName($actionName) {
$this->params['action'] = $actionName;
return $this;
}
/**
* @param string $httpMethod Either 'get' or 'post'.
* @return $this
*/
public function method($httpMethod) {
$httpMethod = trim(strtolower($httpMethod));
if ( ($httpMethod !== 'get') && ($httpMethod !== 'post') ) {
throw new \InvalidArgumentException(sprintf(
'Invalid HTTP method "%s" for a settings form. Must be "get" or "post".',
$httpMethod
));
}
$this->params['method'] = $httpMethod;
return $this;
}
public function submitUrl($url) {
$this->params['submitUrl'] = $url;
return $this;
}
public function requiredCapability($capability) {
$this->params['requiredCapability'] = $capability;
return $this;
}
/**
* @param callable $callback
* @return $this
*/
public function permissionCallback($callback) {
$this->params['permissionCallback'] = $callback;
return $this;
}
public function id($id) {
$this->params['id'] = $id;
return $this;
}
/**
* @param bool $shouldAddButton
* @return $this
*/
public function addDefaultSubmitButton($shouldAddButton = true) {
$this->params['defaultSubmitButtonEnabled'] = $shouldAddButton;
return $this;
}
public function redirectAfterSaving($url, $successParams = array('updated' => 1)) {
$this->params['redirectUrl'] = $url;
$this->params['successParams'] = $successParams;
return $this;
}
public function passThroughParams($params) {
$this->params['passThroughParams'] = $params;
return $this;
}
public function dieOnError() {
$this->params['errorReporting'] = UpdateRequestHandler::DIE_ON_ERRORS;
return $this;
}
public function storeErrors($transientName = null) {
$this->params['errorReporting'] = UpdateRequestHandler::STORE_ERRORS;
$this->params['errorTransientName'] = $transientName;
return $this;
}
public function postProcessSettings($callback) {
$this->params['postProcessingCallback'] = $callback;
return $this;
}
public function skipMissingFields() {
$this->params['missingFieldHandling'] = UpdateRequestHandler::SKIP_MISSING_FIELDS;
return $this;
}
public function treatMissingFieldsAsEmpty() {
$this->params['missingFieldHandling'] = UpdateRequestHandler::TREAT_MISSING_FIELDS_AS_EMPTY;
return $this;
}
public function allowPartialUpdates() {
$this->params['partialUpdatesAllowed'] = true;
return $this;
}
public function forbidPartialUpdates() {
$this->params['partialUpdatesAllowed'] = false;
return $this;
}
public function stopOnFirstValidationError() {
$this->params['$stopOnFirstError'] = true;
return $this;
}
public function build() {
return new SettingsForm($this->params);
}
}