Completed
Push — master ( 1a0349...c60a32 )
by Ben
09:05
created
src/Former/Traits/Framework.php 1 patch
Indentation   +318 added lines, -318 removed lines patch added patch discarded remove patch
@@ -9,322 +9,322 @@
 block discarded – undo
9 9
  */
10 10
 abstract class Framework implements FrameworkInterface
11 11
 {
12
-	/**
13
-	 * The Container
14
-	 *
15
-	 * @var Container
16
-	 */
17
-	protected $app;
18
-
19
-	/**
20
-	 * Form types that trigger special styling
21
-	 *
22
-	 * @var array
23
-	 */
24
-	protected $availableTypes = array();
25
-
26
-	/**
27
-	 * The field states available
28
-	 *
29
-	 * @var array
30
-	 */
31
-	protected $states = array();
32
-
33
-	/**
34
-	 * The default label width (for horizontal forms)
35
-	 *
36
-	 * @var string
37
-	 */
38
-	protected $labelWidth;
39
-
40
-	/**
41
-	 * The default field width (for horizontal forms)
42
-	 *
43
-	 * @var string
44
-	 */
45
-	protected $fieldWidth;
46
-
47
-	/**
48
-	 * The default offset for fields (for horizontal form fields
49
-	 * with no label, so usually equal to the default label width)
50
-	 *
51
-	 * @var string
52
-	 */
53
-	protected $fieldOffset;
54
-
55
-	/**
56
-	 * The default HTML tag used for icons
57
-	 *
58
-	 * @var string
59
-	 */
60
-	protected $iconTag;
61
-
62
-	/**
63
-	 * The default set for icon fonts
64
-	 *
65
-	 * @var string
66
-	 */
67
-	protected $iconSet;
68
-
69
-	/**
70
-	 * The default prefix icon names
71
-	 *
72
-	 * @var string
73
-	 */
74
-	protected $iconPrefix;
75
-
76
-	////////////////////////////////////////////////////////////////////
77
-	//////////////////////// CURRENT FRAMEWORK /////////////////////////
78
-	////////////////////////////////////////////////////////////////////
79
-
80
-	/**
81
-	 * Get the name of the current framework
82
-	 *
83
-	 * @return string
84
-	 */
85
-	public function current()
86
-	{
87
-		return basename(str_replace('\\', '/', get_class($this)));
88
-	}
89
-
90
-	/**
91
-	 * Check if the current framework matches something
92
-	 *
93
-	 * @param  string $framework
94
-	 *
95
-	 * @return boolean
96
-	 */
97
-	public function is($framework)
98
-	{
99
-		return $framework == $this->current();
100
-	}
101
-
102
-	/**
103
-	 * Check if the current framework doesn't match something
104
-	 *
105
-	 * @param  string $framework
106
-	 *
107
-	 * @return boolean
108
-	 */
109
-	public function isnt($framework)
110
-	{
111
-		return $framework != $this->current();
112
-	}
113
-
114
-	////////////////////////////////////////////////////////////////////
115
-	/////////////////////////// COMMON METHODS /////////////////////////
116
-	////////////////////////////////////////////////////////////////////
117
-
118
-	/**
119
-	 * List form types triggered special styling form current framework
120
-	 *
121
-	 * @return array
122
-	 */
123
-	public function availableTypes()
124
-	{
125
-		return $this->availableTypes;
126
-	}
127
-
128
-	/**
129
-	 * Filter a field state
130
-	 *
131
-	 * @param string $state
132
-	 *
133
-	 * @return string
134
-	 */
135
-	public function filterState($state)
136
-	{
137
-		// Filter out wrong states
138
-		return in_array($state, $this->states) ? $state : null;
139
-	}
140
-
141
-	/**
142
-	 * Framework error state
143
-	 *
144
-	 * @return string
145
-	 */
146
-	public function errorState()
147
-	{
148
-		return 'error';
149
-	}
150
-
151
-	/**
152
-	 * Returns corresponding inline class of a field
153
-	 *
154
-	 * @param Field $field
155
-	 *
156
-	 * @return string
157
-	 */
158
-	public function getInlineLabelClass($field)
159
-	{
160
-		return 'inline';
161
-	}
162
-
163
-	/**
164
-	 * Set framework defaults from its config file
165
-	 */
166
-	protected function setFrameworkDefaults()
167
-	{
168
-		$this->setFieldWidths($this->getFrameworkOption('labelWidths'));
169
-		$this->setIconDefaults();
170
-	}
171
-
172
-	/**
173
-	 * @param string $widths
174
-	 */
175
-	protected function setFieldWidths($widths)
176
-	{
177
-	}
178
-
179
-	/**
180
-	 * Override framework defaults for icons with config values where set
181
-	 */
182
-	protected function setIconDefaults()
183
-	{
184
-		$this->iconTag    = $this->getFrameworkOption('icon.tag');
185
-		$this->iconSet    = $this->getFrameworkOption('icon.set');
186
-		$this->iconPrefix = $this->getFrameworkOption('icon.prefix');
187
-	}
188
-
189
-	/**
190
-	 * Render an icon
191
-	 *
192
-	 * @param array $attributes Its general attributes
193
-	 *
194
-	 * @return string
195
-	 */
196
-	public function createIcon($iconType, $attributes = array(), $iconSettings = array())
197
-	{
198
-		// Check for empty icons
199
-		if (!$iconType) {
200
-			return false;
201
-		}
202
-
203
-		// icon settings can be overridden for a specific icon
204
-		$tag    = array_get($iconSettings, 'tag', $this->iconTag);
205
-		$set    = array_get($iconSettings, 'set', $this->iconSet);
206
-		$prefix = array_get($iconSettings, 'prefix', $this->iconPrefix);
207
-
208
-		return Element::create($tag, null, $attributes)->addClass("$set $prefix-$iconType");
209
-	}
210
-
211
-	////////////////////////////////////////////////////////////////////
212
-	///////////////////////////// HELPERS //////////////////////////////
213
-	////////////////////////////////////////////////////////////////////
214
-
215
-	/**
216
-	 * Add classes to a field
217
-	 *
218
-	 * @param Field $field
219
-	 * @param array $classes
220
-	 *
221
-	 * @return \Former\Traits\Field
222
-	 */
223
-	protected function addClassesToField($field, $classes)
224
-	{
225
-		// If we found any class, add them
226
-		if ($classes) {
227
-			$field->addClass(implode(' ', $classes));
228
-		}
229
-
230
-		return $field;
231
-	}
232
-
233
-	/**
234
-	 * Prepend an array of classes with a string
235
-	 *
236
-	 * @param array  $classes The classes to prepend
237
-	 * @param string $with    The string to prepend them with
238
-	 *
239
-	 * @return array A prepended array
240
-	 */
241
-	protected function prependWith($classes, $with)
242
-	{
243
-		return array_map(function ($class) use ($with) {
244
-			return $with.$class;
245
-		}, $classes);
246
-	}
247
-
248
-	/**
249
-	 * Create a label for a field
250
-	 *
251
-	 * @param Field   $field
252
-	 * @param Element $label The field label if non provided
253
-	 *
254
-	 * @return string A label
255
-	 */
256
-	public function createLabelOf(Field $field, Element $label = null)
257
-	{
258
-		// Get the label and its informations
259
-		if (!$label) {
260
-			$label = $field->getLabel();
261
-		}
262
-
263
-		// Get label "for"
264
-		$for = $field->id ?: $field->getName();
265
-
266
-		// Get label text
267
-		$text = $label->getValue();
268
-		if (!$text) {
269
-			return false;
270
-		}
271
-
272
-		// Append required text
273
-		if ($field->isRequired()) {
274
-			$text .= $this->app['former']->getOption('required_text');
275
-		}
276
-
277
-		// Render plain label if checkable, else a classic one
278
-		$label->setValue($text);
279
-		if (!$field->isCheckable()) {
280
-			$label->for($for);
281
-		}
282
-
283
-		return $label;
284
-	}
285
-
286
-	/**
287
-	 * Get an option for the current framework
288
-	 *
289
-	 * @param string $option
290
-	 *
291
-	 * @return string
292
-	 */
293
-	protected function getFrameworkOption($option)
294
-	{
295
-		return $this->app['config']->get("former.{$this->current()}.$option");
296
-	}
297
-
298
-	////////////////////////////////////////////////////////////////////
299
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
300
-	////////////////////////////////////////////////////////////////////
301
-
302
-	/**
303
-	 * Wraps all label contents with potential additional tags.
304
-	 *
305
-	 * @param  string $label
306
-	 *
307
-	 * @return string A wrapped label
308
-	 */
309
-	public function wrapLabel($label)
310
-	{
311
-		return $label;
312
-	}
313
-
314
-	////////////////////////////////////////////////////////////////////
315
-	//////////////////////////// RENDER BLOCKS /////////////////////////
316
-	////////////////////////////////////////////////////////////////////
317
-
318
-	/**
319
-	 * Render an validation error text
320
-	 *
321
-	 * @param string $text
322
-	 * @param array  $attributes
323
-	 *
324
-	 * @return string
325
-	 */
326
-	public function createValidationError($text, $attributes = array())
327
-	{
328
-		return $this->createHelp($text, $attributes);
329
-	}
12
+    /**
13
+     * The Container
14
+     *
15
+     * @var Container
16
+     */
17
+    protected $app;
18
+
19
+    /**
20
+     * Form types that trigger special styling
21
+     *
22
+     * @var array
23
+     */
24
+    protected $availableTypes = array();
25
+
26
+    /**
27
+     * The field states available
28
+     *
29
+     * @var array
30
+     */
31
+    protected $states = array();
32
+
33
+    /**
34
+     * The default label width (for horizontal forms)
35
+     *
36
+     * @var string
37
+     */
38
+    protected $labelWidth;
39
+
40
+    /**
41
+     * The default field width (for horizontal forms)
42
+     *
43
+     * @var string
44
+     */
45
+    protected $fieldWidth;
46
+
47
+    /**
48
+     * The default offset for fields (for horizontal form fields
49
+     * with no label, so usually equal to the default label width)
50
+     *
51
+     * @var string
52
+     */
53
+    protected $fieldOffset;
54
+
55
+    /**
56
+     * The default HTML tag used for icons
57
+     *
58
+     * @var string
59
+     */
60
+    protected $iconTag;
61
+
62
+    /**
63
+     * The default set for icon fonts
64
+     *
65
+     * @var string
66
+     */
67
+    protected $iconSet;
68
+
69
+    /**
70
+     * The default prefix icon names
71
+     *
72
+     * @var string
73
+     */
74
+    protected $iconPrefix;
75
+
76
+    ////////////////////////////////////////////////////////////////////
77
+    //////////////////////// CURRENT FRAMEWORK /////////////////////////
78
+    ////////////////////////////////////////////////////////////////////
79
+
80
+    /**
81
+     * Get the name of the current framework
82
+     *
83
+     * @return string
84
+     */
85
+    public function current()
86
+    {
87
+        return basename(str_replace('\\', '/', get_class($this)));
88
+    }
89
+
90
+    /**
91
+     * Check if the current framework matches something
92
+     *
93
+     * @param  string $framework
94
+     *
95
+     * @return boolean
96
+     */
97
+    public function is($framework)
98
+    {
99
+        return $framework == $this->current();
100
+    }
101
+
102
+    /**
103
+     * Check if the current framework doesn't match something
104
+     *
105
+     * @param  string $framework
106
+     *
107
+     * @return boolean
108
+     */
109
+    public function isnt($framework)
110
+    {
111
+        return $framework != $this->current();
112
+    }
113
+
114
+    ////////////////////////////////////////////////////////////////////
115
+    /////////////////////////// COMMON METHODS /////////////////////////
116
+    ////////////////////////////////////////////////////////////////////
117
+
118
+    /**
119
+     * List form types triggered special styling form current framework
120
+     *
121
+     * @return array
122
+     */
123
+    public function availableTypes()
124
+    {
125
+        return $this->availableTypes;
126
+    }
127
+
128
+    /**
129
+     * Filter a field state
130
+     *
131
+     * @param string $state
132
+     *
133
+     * @return string
134
+     */
135
+    public function filterState($state)
136
+    {
137
+        // Filter out wrong states
138
+        return in_array($state, $this->states) ? $state : null;
139
+    }
140
+
141
+    /**
142
+     * Framework error state
143
+     *
144
+     * @return string
145
+     */
146
+    public function errorState()
147
+    {
148
+        return 'error';
149
+    }
150
+
151
+    /**
152
+     * Returns corresponding inline class of a field
153
+     *
154
+     * @param Field $field
155
+     *
156
+     * @return string
157
+     */
158
+    public function getInlineLabelClass($field)
159
+    {
160
+        return 'inline';
161
+    }
162
+
163
+    /**
164
+     * Set framework defaults from its config file
165
+     */
166
+    protected function setFrameworkDefaults()
167
+    {
168
+        $this->setFieldWidths($this->getFrameworkOption('labelWidths'));
169
+        $this->setIconDefaults();
170
+    }
171
+
172
+    /**
173
+     * @param string $widths
174
+     */
175
+    protected function setFieldWidths($widths)
176
+    {
177
+    }
178
+
179
+    /**
180
+     * Override framework defaults for icons with config values where set
181
+     */
182
+    protected function setIconDefaults()
183
+    {
184
+        $this->iconTag    = $this->getFrameworkOption('icon.tag');
185
+        $this->iconSet    = $this->getFrameworkOption('icon.set');
186
+        $this->iconPrefix = $this->getFrameworkOption('icon.prefix');
187
+    }
188
+
189
+    /**
190
+     * Render an icon
191
+     *
192
+     * @param array $attributes Its general attributes
193
+     *
194
+     * @return string
195
+     */
196
+    public function createIcon($iconType, $attributes = array(), $iconSettings = array())
197
+    {
198
+        // Check for empty icons
199
+        if (!$iconType) {
200
+            return false;
201
+        }
202
+
203
+        // icon settings can be overridden for a specific icon
204
+        $tag    = array_get($iconSettings, 'tag', $this->iconTag);
205
+        $set    = array_get($iconSettings, 'set', $this->iconSet);
206
+        $prefix = array_get($iconSettings, 'prefix', $this->iconPrefix);
207
+
208
+        return Element::create($tag, null, $attributes)->addClass("$set $prefix-$iconType");
209
+    }
210
+
211
+    ////////////////////////////////////////////////////////////////////
212
+    ///////////////////////////// HELPERS //////////////////////////////
213
+    ////////////////////////////////////////////////////////////////////
214
+
215
+    /**
216
+     * Add classes to a field
217
+     *
218
+     * @param Field $field
219
+     * @param array $classes
220
+     *
221
+     * @return \Former\Traits\Field
222
+     */
223
+    protected function addClassesToField($field, $classes)
224
+    {
225
+        // If we found any class, add them
226
+        if ($classes) {
227
+            $field->addClass(implode(' ', $classes));
228
+        }
229
+
230
+        return $field;
231
+    }
232
+
233
+    /**
234
+     * Prepend an array of classes with a string
235
+     *
236
+     * @param array  $classes The classes to prepend
237
+     * @param string $with    The string to prepend them with
238
+     *
239
+     * @return array A prepended array
240
+     */
241
+    protected function prependWith($classes, $with)
242
+    {
243
+        return array_map(function ($class) use ($with) {
244
+            return $with.$class;
245
+        }, $classes);
246
+    }
247
+
248
+    /**
249
+     * Create a label for a field
250
+     *
251
+     * @param Field   $field
252
+     * @param Element $label The field label if non provided
253
+     *
254
+     * @return string A label
255
+     */
256
+    public function createLabelOf(Field $field, Element $label = null)
257
+    {
258
+        // Get the label and its informations
259
+        if (!$label) {
260
+            $label = $field->getLabel();
261
+        }
262
+
263
+        // Get label "for"
264
+        $for = $field->id ?: $field->getName();
265
+
266
+        // Get label text
267
+        $text = $label->getValue();
268
+        if (!$text) {
269
+            return false;
270
+        }
271
+
272
+        // Append required text
273
+        if ($field->isRequired()) {
274
+            $text .= $this->app['former']->getOption('required_text');
275
+        }
276
+
277
+        // Render plain label if checkable, else a classic one
278
+        $label->setValue($text);
279
+        if (!$field->isCheckable()) {
280
+            $label->for($for);
281
+        }
282
+
283
+        return $label;
284
+    }
285
+
286
+    /**
287
+     * Get an option for the current framework
288
+     *
289
+     * @param string $option
290
+     *
291
+     * @return string
292
+     */
293
+    protected function getFrameworkOption($option)
294
+    {
295
+        return $this->app['config']->get("former.{$this->current()}.$option");
296
+    }
297
+
298
+    ////////////////////////////////////////////////////////////////////
299
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
300
+    ////////////////////////////////////////////////////////////////////
301
+
302
+    /**
303
+     * Wraps all label contents with potential additional tags.
304
+     *
305
+     * @param  string $label
306
+     *
307
+     * @return string A wrapped label
308
+     */
309
+    public function wrapLabel($label)
310
+    {
311
+        return $label;
312
+    }
313
+
314
+    ////////////////////////////////////////////////////////////////////
315
+    //////////////////////////// RENDER BLOCKS /////////////////////////
316
+    ////////////////////////////////////////////////////////////////////
317
+
318
+    /**
319
+     * Render an validation error text
320
+     *
321
+     * @param string $text
322
+     * @param array  $attributes
323
+     *
324
+     * @return string
325
+     */
326
+    public function createValidationError($text, $attributes = array())
327
+    {
328
+        return $this->createHelp($text, $attributes);
329
+    }
330 330
 }
Please login to merge, or discard this patch.
src/Former/Form/Group.php 1 patch
Indentation   +460 added lines, -460 removed lines patch added patch discarded remove patch
@@ -12,464 +12,464 @@
 block discarded – undo
12 12
  */
13 13
 class Group extends Tag
14 14
 {
15
-	/**
16
-	 * The Container
17
-	 *
18
-	 * @var Container
19
-	 */
20
-	protected $app;
21
-
22
-	/**
23
-	 * The current state of the group
24
-	 *
25
-	 * @var string
26
-	 */
27
-	protected $state = null;
28
-
29
-	/**
30
-	 * Whether the field should be displayed raw or not
31
-	 *
32
-	 * @var boolean
33
-	 */
34
-	protected $raw = false;
35
-
36
-	/**
37
-	 * The group label
38
-	 *
39
-	 * @var Element
40
-	 */
41
-	protected $label;
42
-
43
-	/**
44
-	 * The group help
45
-	 *
46
-	 * @var array
47
-	 */
48
-	protected $help = array();
49
-
50
-	/**
51
-	 * An array of elements to preprend the field
52
-	 *
53
-	 * @var array
54
-	 */
55
-	protected $prepend = array();
56
-
57
-	/**
58
-	 * An array of elements to append the field
59
-	 *
60
-	 * @var array
61
-	 */
62
-	protected $append = array();
63
-
64
-	/**
65
-	 * The field validations to be checked for errors
66
-	 *
67
-	 * @var array
68
-	 */
69
-	protected $validations = array();
70
-
71
-	/**
72
-	 * The group's element
73
-	 *
74
-	 * @var string
75
-	 */
76
-	protected $element = 'div';
77
-
78
-	/**
79
-	 * Whether a custom group is opened or not
80
-	 *
81
-	 * @var boolean
82
-	 */
83
-	public static $opened = false;
84
-
85
-	/**
86
-	 * The custom group that is open
87
-	 *
88
-	 * @var Former\Form\Group
89
-	 */
90
-	public static $openGroup = null;
91
-
92
-	////////////////////////////////////////////////////////////////////
93
-	/////////////////////////// CORE METHODS ///////////////////////////
94
-	////////////////////////////////////////////////////////////////////
95
-
96
-	/**
97
-	 * Creates a group
98
-	 *
99
-	 * @param string $label Its label
100
-	 */
101
-	public function __construct(Container $app, $label, $validations = null)
102
-	{
103
-		// Get special classes
104
-		$this->app = $app;
105
-		$this->addClass($this->app['former.framework']->getGroupClasses());
106
-
107
-		// Invisible if Nude
108
-		if ($this->app['former.framework']->is('Nude')) {
109
-			$this->element = '';
110
-		}
111
-
112
-		// Set group label
113
-		if ($label) {
114
-			$this->setLabel($label);
115
-		}
116
-
117
-		// Set validations used to override groups own conclusions
118
-		$this->validations = (array) $validations;
119
-	}
120
-
121
-	/**
122
-	 * Prints out the opening of the Control Group
123
-	 *
124
-	 * @return string A control group opening tag
125
-	 */
126
-	public function __toString()
127
-	{
128
-		return $this->open().$this->getFormattedLabel();
129
-	}
130
-
131
-	/**
132
-	 * Opens a group
133
-	 *
134
-	 * @return string Opening tag
135
-	 */
136
-	public function open()
137
-	{
138
-		if ($this->getErrors()) {
139
-			$this->state($this->app['former.framework']->errorState());
140
-		}
141
-
142
-		// Retrieve state and append it to classes
143
-		if ($this->state) {
144
-			$this->addClass($this->state);
145
-		}
146
-
147
-		// Required state
148
-		if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) {
149
-			$this->addClass($this->app['former']->getOption('required_class'));
150
-		}
151
-
152
-		return parent::open();
153
-	}
154
-
155
-	/**
156
-	 * Set the contents of the current group
157
-	 *
158
-	 * @param string $contents The group contents
159
-	 *
160
-	 * @return string A group
161
-	 */
162
-	public function contents($contents)
163
-	{
164
-		return $this->wrap($contents, $this->getFormattedLabel());
165
-	}
166
-
167
-	/**
168
-	 * Wrap a Field with the current group
169
-	 *
170
-	 * @param  \Former\Traits\Field $field A Field instance
171
-	 *
172
-	 * @return string        A group
173
-	 */
174
-	public function wrapField($field)
175
-	{
176
-		$label = $this->getLabel($field);
177
-		$field = $this->prependAppend($field);
178
-		$field .= $this->getHelp();
179
-
180
-		return $this->wrap($field, $label);
181
-	}
182
-
183
-	////////////////////////////////////////////////////////////////////
184
-	//////////////////////////// FIELD METHODS /////////////////////////
185
-	////////////////////////////////////////////////////////////////////
186
-
187
-	/**
188
-	 * Set the state of the group
189
-	 *
190
-	 * @param  string $state A Bootstrap state class
191
-	 */
192
-	public function state($state)
193
-	{
194
-		// Filter state
195
-		$state = $this->app['former.framework']->filterState($state);
196
-
197
-		$this->state = $state;
198
-	}
199
-
200
-	/**
201
-	 * Set a class on the Group
202
-	 *
203
-	 * @param string $class The class to add
204
-	 */
205
-	public function addGroupClass($class)
206
-	{
207
-		$this->addClass($class);
208
-	}
209
-
210
-	/**
211
-	 * Adds a label to the group
212
-	 *
213
-	 * @param  string $label A label
214
-	 */
215
-	public function setLabel($label)
216
-	{
217
-		if (!$label instanceof Element) {
218
-			$label = Helpers::translate($label);
219
-			$label = Element::create('label', $label)->for($label);
220
-		}
221
-
222
-		$this->label = $label;
223
-	}
224
-
225
-	/**
226
-	 * Get the formatted group label
227
-	 *
228
-	 * @return string|null
229
-	 */
230
-	public function getFormattedLabel()
231
-	{
232
-		if (!$this->label) {
233
-			return false;
234
-		}
235
-
236
-		return $this->label->addClass($this->app['former.framework']->getLabelClasses());
237
-	}
238
-
239
-	/**
240
-	 * Disables the control group for the current field
241
-	 */
242
-	public function raw()
243
-	{
244
-		$this->raw = true;
245
-	}
246
-
247
-	/**
248
-	 * Check if the current group is to be displayed or not
249
-	 *
250
-	 * @return boolean
251
-	 */
252
-	public function isRaw()
253
-	{
254
-		return (bool) $this->raw;
255
-	}
256
-
257
-	////////////////////////////////////////////////////////////////////
258
-	///////////////////////////// HELP BLOCKS //////////////////////////
259
-	////////////////////////////////////////////////////////////////////
260
-
261
-	/**
262
-	 * Alias for inlineHelp
263
-	 *
264
-	 * @param  string $help       The help text
265
-	 * @param  array  $attributes Facultative attributes
266
-	 */
267
-	public function help($help, $attributes = array())
268
-	{
269
-		return $this->inlineHelp($help, $attributes);
270
-	}
271
-
272
-	/**
273
-	 * Add an inline help
274
-	 *
275
-	 * @param  string $help       The help text
276
-	 * @param  array  $attributes Facultative attributes
277
-	 */
278
-	public function inlineHelp($help, $attributes = array())
279
-	{
280
-		// If no help text, do nothing
281
-		if (!$help) {
282
-			return false;
283
-		}
284
-
285
-		$this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes);
286
-	}
287
-
288
-	/**
289
-	 * Add an block help
290
-	 *
291
-	 * @param  string $help       The help text
292
-	 * @param  array  $attributes Facultative attributes
293
-	 */
294
-	public function blockHelp($help, $attributes = array())
295
-	{
296
-		// Reserved method
297
-		if ($this->app['former.framework']->isnt('TwitterBootstrap') &&
298
-		    $this->app['former.framework']->isnt('TwitterBootstrap3') &&
299
-		    $this->app['former.framework']->isnt('TwitterBootstrap4')
300
-		) {
301
-			throw new BadMethodCallException('This method is only available on the Bootstrap framework');
302
-		}
303
-
304
-		// If no help text, do nothing
305
-		if (!$help) {
306
-			return false;
307
-		}
308
-
309
-		$this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes);
310
-	}
311
-
312
-	////////////////////////////////////////////////////////////////////
313
-	///////////////////////// PREPEND/APPEND METHODS ///////////////////
314
-	////////////////////////////////////////////////////////////////////
315
-
316
-	/**
317
-	 * Prepend elements to the field
318
-	 */
319
-	public function prepend()
320
-	{
321
-		$this->placeAround(func_get_args(), 'prepend');
322
-	}
323
-
324
-	/**
325
-	 * Append elements to the field
326
-	 */
327
-	public function append()
328
-	{
329
-		$this->placeAround(func_get_args(), 'append');
330
-	}
331
-
332
-	/**
333
-	 * Prepends an icon to a field
334
-	 *
335
-	 * @param string $icon       The icon to prepend
336
-	 * @param array  $attributes Its attributes
337
-	 */
338
-	public function prependIcon($icon, $attributes = array(), $iconSettings = array())
339
-	{
340
-		$icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
341
-
342
-		$this->prepend($icon);
343
-	}
344
-
345
-	/**
346
-	 * Append an icon to a field
347
-	 *
348
-	 * @param string $icon       The icon to prepend
349
-	 * @param array  $attributes Its attributes
350
-	 */
351
-	public function appendIcon($icon, $attributes = array(), $iconSettings = array())
352
-	{
353
-		$icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
354
-
355
-		$this->append($icon);
356
-	}
357
-
358
-	////////////////////////////////////////////////////////////////////
359
-	//////////////////////////////// HELPERS ///////////////////////////
360
-	////////////////////////////////////////////////////////////////////
361
-
362
-	/**
363
-	 * Get the errors for the group
364
-	 *
365
-	 * @return string
366
-	 */
367
-	public function getErrors()
368
-	{
369
-		$errors = '';
370
-
371
-		if (!self::$opened) {
372
-
373
-			// for non-custom groups, normal error handling applies
374
-			$errors = $this->app['former']->getErrors();
375
-		} elseif (!empty($this->validations)) {
376
-
377
-			// error handling only when validations specified for custom groups
378
-			foreach ($this->validations as $validation) {
379
-				$errors .= $this->app['former']->getErrors($validation);
380
-			}
381
-		}
382
-
383
-		return $errors;
384
-	}
385
-
386
-	/**
387
-	 * Wraps content in a group
388
-	 *
389
-	 * @param string $contents The content
390
-	 * @param string $label    The label to add
391
-	 *
392
-	 * @return string A group
393
-	 */
394
-	public function wrap($contents, $label = null)
395
-	{
396
-		$group = $this->open();
397
-		$group .= $label;
398
-		$group .= $this->app['former.framework']->wrapField($contents);
399
-		$group .= $this->close();
400
-
401
-		return $group;
402
-	}
403
-
404
-	/**
405
-	 * Prints out the current label
406
-	 *
407
-	 * @param  string $field The field to create a label for
408
-	 *
409
-	 * @return string        A <label> tag
410
-	 */
411
-	protected function getLabel($field = null)
412
-	{
413
-		// Don't create a label if none exist
414
-		if (!$field or !$this->label) {
415
-			return null;
416
-		}
417
-
418
-		// Wrap label in framework classes
419
-		$this->label->addClass($this->app['former.framework']->getLabelClasses());
420
-		$this->label = $this->app['former.framework']->createLabelOf($field, $this->label);
421
-		$this->label = $this->app['former.framework']->wrapLabel($this->label);
422
-
423
-		return $this->label;
424
-	}
425
-
426
-	/**
427
-	 * Prints out the current help
428
-	 *
429
-	 * @return string A .help-block or .help-inline
430
-	 */
431
-	protected function getHelp()
432
-	{
433
-		$inline = array_get($this->help, 'inline');
434
-		$block  = array_get($this->help, 'block');
435
-
436
-		// Replace help text with error if any found
437
-		$errors = $this->app['former']->getErrors();
438
-		if ($errors and $this->app['former']->getOption('error_messages')) {
439
-			$inline = $this->app['former.framework']->createValidationError($errors);
440
-		}
441
-
442
-		return join(null, array($inline, $block));
443
-	}
444
-
445
-	/**
446
-	 * Format the field with prepended/appended elements
447
-	 *
448
-	 * @param  Field $field The field to format
449
-	 *
450
-	 * @return string        Field plus supplementary elements
451
-	 */
452
-	protected function prependAppend($field)
453
-	{
454
-		if (!$this->prepend and !$this->append) {
455
-			return $field->render();
456
-		}
457
-
458
-		return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append);
459
-	}
460
-
461
-	/**
462
-	 * Place elements around the field
463
-	 *
464
-	 * @param  array  $items An array of items to place
465
-	 * @param  string $place Where they should end up (prepend|append)
466
-	 */
467
-	protected function placeAround($items, $place)
468
-	{
469
-		// Iterate over the items and place them where they should
470
-		foreach ((array) $items as $item) {
471
-			$item             = $this->app['former.framework']->placeAround($item);
472
-			$this->{$place}[] = $item;
473
-		}
474
-	}
15
+    /**
16
+     * The Container
17
+     *
18
+     * @var Container
19
+     */
20
+    protected $app;
21
+
22
+    /**
23
+     * The current state of the group
24
+     *
25
+     * @var string
26
+     */
27
+    protected $state = null;
28
+
29
+    /**
30
+     * Whether the field should be displayed raw or not
31
+     *
32
+     * @var boolean
33
+     */
34
+    protected $raw = false;
35
+
36
+    /**
37
+     * The group label
38
+     *
39
+     * @var Element
40
+     */
41
+    protected $label;
42
+
43
+    /**
44
+     * The group help
45
+     *
46
+     * @var array
47
+     */
48
+    protected $help = array();
49
+
50
+    /**
51
+     * An array of elements to preprend the field
52
+     *
53
+     * @var array
54
+     */
55
+    protected $prepend = array();
56
+
57
+    /**
58
+     * An array of elements to append the field
59
+     *
60
+     * @var array
61
+     */
62
+    protected $append = array();
63
+
64
+    /**
65
+     * The field validations to be checked for errors
66
+     *
67
+     * @var array
68
+     */
69
+    protected $validations = array();
70
+
71
+    /**
72
+     * The group's element
73
+     *
74
+     * @var string
75
+     */
76
+    protected $element = 'div';
77
+
78
+    /**
79
+     * Whether a custom group is opened or not
80
+     *
81
+     * @var boolean
82
+     */
83
+    public static $opened = false;
84
+
85
+    /**
86
+     * The custom group that is open
87
+     *
88
+     * @var Former\Form\Group
89
+     */
90
+    public static $openGroup = null;
91
+
92
+    ////////////////////////////////////////////////////////////////////
93
+    /////////////////////////// CORE METHODS ///////////////////////////
94
+    ////////////////////////////////////////////////////////////////////
95
+
96
+    /**
97
+     * Creates a group
98
+     *
99
+     * @param string $label Its label
100
+     */
101
+    public function __construct(Container $app, $label, $validations = null)
102
+    {
103
+        // Get special classes
104
+        $this->app = $app;
105
+        $this->addClass($this->app['former.framework']->getGroupClasses());
106
+
107
+        // Invisible if Nude
108
+        if ($this->app['former.framework']->is('Nude')) {
109
+            $this->element = '';
110
+        }
111
+
112
+        // Set group label
113
+        if ($label) {
114
+            $this->setLabel($label);
115
+        }
116
+
117
+        // Set validations used to override groups own conclusions
118
+        $this->validations = (array) $validations;
119
+    }
120
+
121
+    /**
122
+     * Prints out the opening of the Control Group
123
+     *
124
+     * @return string A control group opening tag
125
+     */
126
+    public function __toString()
127
+    {
128
+        return $this->open().$this->getFormattedLabel();
129
+    }
130
+
131
+    /**
132
+     * Opens a group
133
+     *
134
+     * @return string Opening tag
135
+     */
136
+    public function open()
137
+    {
138
+        if ($this->getErrors()) {
139
+            $this->state($this->app['former.framework']->errorState());
140
+        }
141
+
142
+        // Retrieve state and append it to classes
143
+        if ($this->state) {
144
+            $this->addClass($this->state);
145
+        }
146
+
147
+        // Required state
148
+        if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) {
149
+            $this->addClass($this->app['former']->getOption('required_class'));
150
+        }
151
+
152
+        return parent::open();
153
+    }
154
+
155
+    /**
156
+     * Set the contents of the current group
157
+     *
158
+     * @param string $contents The group contents
159
+     *
160
+     * @return string A group
161
+     */
162
+    public function contents($contents)
163
+    {
164
+        return $this->wrap($contents, $this->getFormattedLabel());
165
+    }
166
+
167
+    /**
168
+     * Wrap a Field with the current group
169
+     *
170
+     * @param  \Former\Traits\Field $field A Field instance
171
+     *
172
+     * @return string        A group
173
+     */
174
+    public function wrapField($field)
175
+    {
176
+        $label = $this->getLabel($field);
177
+        $field = $this->prependAppend($field);
178
+        $field .= $this->getHelp();
179
+
180
+        return $this->wrap($field, $label);
181
+    }
182
+
183
+    ////////////////////////////////////////////////////////////////////
184
+    //////////////////////////// FIELD METHODS /////////////////////////
185
+    ////////////////////////////////////////////////////////////////////
186
+
187
+    /**
188
+     * Set the state of the group
189
+     *
190
+     * @param  string $state A Bootstrap state class
191
+     */
192
+    public function state($state)
193
+    {
194
+        // Filter state
195
+        $state = $this->app['former.framework']->filterState($state);
196
+
197
+        $this->state = $state;
198
+    }
199
+
200
+    /**
201
+     * Set a class on the Group
202
+     *
203
+     * @param string $class The class to add
204
+     */
205
+    public function addGroupClass($class)
206
+    {
207
+        $this->addClass($class);
208
+    }
209
+
210
+    /**
211
+     * Adds a label to the group
212
+     *
213
+     * @param  string $label A label
214
+     */
215
+    public function setLabel($label)
216
+    {
217
+        if (!$label instanceof Element) {
218
+            $label = Helpers::translate($label);
219
+            $label = Element::create('label', $label)->for($label);
220
+        }
221
+
222
+        $this->label = $label;
223
+    }
224
+
225
+    /**
226
+     * Get the formatted group label
227
+     *
228
+     * @return string|null
229
+     */
230
+    public function getFormattedLabel()
231
+    {
232
+        if (!$this->label) {
233
+            return false;
234
+        }
235
+
236
+        return $this->label->addClass($this->app['former.framework']->getLabelClasses());
237
+    }
238
+
239
+    /**
240
+     * Disables the control group for the current field
241
+     */
242
+    public function raw()
243
+    {
244
+        $this->raw = true;
245
+    }
246
+
247
+    /**
248
+     * Check if the current group is to be displayed or not
249
+     *
250
+     * @return boolean
251
+     */
252
+    public function isRaw()
253
+    {
254
+        return (bool) $this->raw;
255
+    }
256
+
257
+    ////////////////////////////////////////////////////////////////////
258
+    ///////////////////////////// HELP BLOCKS //////////////////////////
259
+    ////////////////////////////////////////////////////////////////////
260
+
261
+    /**
262
+     * Alias for inlineHelp
263
+     *
264
+     * @param  string $help       The help text
265
+     * @param  array  $attributes Facultative attributes
266
+     */
267
+    public function help($help, $attributes = array())
268
+    {
269
+        return $this->inlineHelp($help, $attributes);
270
+    }
271
+
272
+    /**
273
+     * Add an inline help
274
+     *
275
+     * @param  string $help       The help text
276
+     * @param  array  $attributes Facultative attributes
277
+     */
278
+    public function inlineHelp($help, $attributes = array())
279
+    {
280
+        // If no help text, do nothing
281
+        if (!$help) {
282
+            return false;
283
+        }
284
+
285
+        $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes);
286
+    }
287
+
288
+    /**
289
+     * Add an block help
290
+     *
291
+     * @param  string $help       The help text
292
+     * @param  array  $attributes Facultative attributes
293
+     */
294
+    public function blockHelp($help, $attributes = array())
295
+    {
296
+        // Reserved method
297
+        if ($this->app['former.framework']->isnt('TwitterBootstrap') &&
298
+            $this->app['former.framework']->isnt('TwitterBootstrap3') &&
299
+            $this->app['former.framework']->isnt('TwitterBootstrap4')
300
+        ) {
301
+            throw new BadMethodCallException('This method is only available on the Bootstrap framework');
302
+        }
303
+
304
+        // If no help text, do nothing
305
+        if (!$help) {
306
+            return false;
307
+        }
308
+
309
+        $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes);
310
+    }
311
+
312
+    ////////////////////////////////////////////////////////////////////
313
+    ///////////////////////// PREPEND/APPEND METHODS ///////////////////
314
+    ////////////////////////////////////////////////////////////////////
315
+
316
+    /**
317
+     * Prepend elements to the field
318
+     */
319
+    public function prepend()
320
+    {
321
+        $this->placeAround(func_get_args(), 'prepend');
322
+    }
323
+
324
+    /**
325
+     * Append elements to the field
326
+     */
327
+    public function append()
328
+    {
329
+        $this->placeAround(func_get_args(), 'append');
330
+    }
331
+
332
+    /**
333
+     * Prepends an icon to a field
334
+     *
335
+     * @param string $icon       The icon to prepend
336
+     * @param array  $attributes Its attributes
337
+     */
338
+    public function prependIcon($icon, $attributes = array(), $iconSettings = array())
339
+    {
340
+        $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
341
+
342
+        $this->prepend($icon);
343
+    }
344
+
345
+    /**
346
+     * Append an icon to a field
347
+     *
348
+     * @param string $icon       The icon to prepend
349
+     * @param array  $attributes Its attributes
350
+     */
351
+    public function appendIcon($icon, $attributes = array(), $iconSettings = array())
352
+    {
353
+        $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
354
+
355
+        $this->append($icon);
356
+    }
357
+
358
+    ////////////////////////////////////////////////////////////////////
359
+    //////////////////////////////// HELPERS ///////////////////////////
360
+    ////////////////////////////////////////////////////////////////////
361
+
362
+    /**
363
+     * Get the errors for the group
364
+     *
365
+     * @return string
366
+     */
367
+    public function getErrors()
368
+    {
369
+        $errors = '';
370
+
371
+        if (!self::$opened) {
372
+
373
+            // for non-custom groups, normal error handling applies
374
+            $errors = $this->app['former']->getErrors();
375
+        } elseif (!empty($this->validations)) {
376
+
377
+            // error handling only when validations specified for custom groups
378
+            foreach ($this->validations as $validation) {
379
+                $errors .= $this->app['former']->getErrors($validation);
380
+            }
381
+        }
382
+
383
+        return $errors;
384
+    }
385
+
386
+    /**
387
+     * Wraps content in a group
388
+     *
389
+     * @param string $contents The content
390
+     * @param string $label    The label to add
391
+     *
392
+     * @return string A group
393
+     */
394
+    public function wrap($contents, $label = null)
395
+    {
396
+        $group = $this->open();
397
+        $group .= $label;
398
+        $group .= $this->app['former.framework']->wrapField($contents);
399
+        $group .= $this->close();
400
+
401
+        return $group;
402
+    }
403
+
404
+    /**
405
+     * Prints out the current label
406
+     *
407
+     * @param  string $field The field to create a label for
408
+     *
409
+     * @return string        A <label> tag
410
+     */
411
+    protected function getLabel($field = null)
412
+    {
413
+        // Don't create a label if none exist
414
+        if (!$field or !$this->label) {
415
+            return null;
416
+        }
417
+
418
+        // Wrap label in framework classes
419
+        $this->label->addClass($this->app['former.framework']->getLabelClasses());
420
+        $this->label = $this->app['former.framework']->createLabelOf($field, $this->label);
421
+        $this->label = $this->app['former.framework']->wrapLabel($this->label);
422
+
423
+        return $this->label;
424
+    }
425
+
426
+    /**
427
+     * Prints out the current help
428
+     *
429
+     * @return string A .help-block or .help-inline
430
+     */
431
+    protected function getHelp()
432
+    {
433
+        $inline = array_get($this->help, 'inline');
434
+        $block  = array_get($this->help, 'block');
435
+
436
+        // Replace help text with error if any found
437
+        $errors = $this->app['former']->getErrors();
438
+        if ($errors and $this->app['former']->getOption('error_messages')) {
439
+            $inline = $this->app['former.framework']->createValidationError($errors);
440
+        }
441
+
442
+        return join(null, array($inline, $block));
443
+    }
444
+
445
+    /**
446
+     * Format the field with prepended/appended elements
447
+     *
448
+     * @param  Field $field The field to format
449
+     *
450
+     * @return string        Field plus supplementary elements
451
+     */
452
+    protected function prependAppend($field)
453
+    {
454
+        if (!$this->prepend and !$this->append) {
455
+            return $field->render();
456
+        }
457
+
458
+        return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append);
459
+    }
460
+
461
+    /**
462
+     * Place elements around the field
463
+     *
464
+     * @param  array  $items An array of items to place
465
+     * @param  string $place Where they should end up (prepend|append)
466
+     */
467
+    protected function placeAround($items, $place)
468
+    {
469
+        // Iterate over the items and place them where they should
470
+        foreach ((array) $items as $item) {
471
+            $item             = $this->app['former.framework']->placeAround($item);
472
+            $this->{$place}[] = $item;
473
+        }
474
+    }
475 475
 }
Please login to merge, or discard this patch.
src/Former/Framework/TwitterBootstrap4.php 1 patch
Indentation   +455 added lines, -455 removed lines patch added patch discarded remove patch
@@ -13,459 +13,459 @@
 block discarded – undo
13 13
  */
14 14
 class TwitterBootstrap4 extends Framework implements FrameworkInterface
15 15
 {
16
-	/**
17
-	 * Form types that trigger special styling for this Framework
18
-	 *
19
-	 * @var array
20
-	 */
21
-	protected $availableTypes = array('horizontal', 'vertical', 'inline');
22
-
23
-	/**
24
-	 * The button types available
25
-	 *
26
-	 * @var array
27
-	 */
28
-	private $buttons = array(
29
-		'lg',
30
-		'sm',
31
-		'xs',
32
-		'block',
33
-		'link',
34
-		'primary',
35
-		'secondary',
36
-		'warning',
37
-		'danger',
38
-		'success',
39
-		'info',
40
-		'light',
41
-		'dark',
42
-	);
43
-
44
-	/**
45
-	 * The field sizes available
46
-	 *
47
-	 * @var array
48
-	 */
49
-	private $fields = array(
50
-		'lg',
51
-		'sm',
52
-		// 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6',
53
-		// 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12',
54
-		// 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6',
55
-		// 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12',
56
-		// 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6',
57
-		// 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12',
58
-		// 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6',
59
-		// 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12',
60
-	);
61
-
62
-	/**
63
-	 * The field states available
64
-	 *
65
-	 * @var array
66
-	 */
67
-	protected $states = array(
68
-		'is-invalid',
69
-	);
70
-
71
-	/**
72
-	 * The default HTML tag used for icons
73
-	 *
74
-	 * @var string
75
-	 */
76
-	protected $iconTag = 'i';
77
-
78
-	/**
79
-	 * The default set for icon fonts
80
-	 * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome
81
-	 *
82
-	 * @var string
83
-	 */
84
-	protected $iconSet = 'fa';
85
-
86
-	/**
87
-	 * The default prefix icon names
88
-	 * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular
89
-	 *
90
-	 * @var string
91
-	 */
92
-	protected $iconPrefix = 'fa';
93
-
94
-	/**
95
-	 * Create a new TwitterBootstrap instance
96
-	 *
97
-	 * @param \Illuminate\Container\Container $app
98
-	 */
99
-	public function __construct(Container $app)
100
-	{
101
-		$this->app = $app;
102
-		$this->setFrameworkDefaults();
103
-	}
104
-
105
-	////////////////////////////////////////////////////////////////////
106
-	/////////////////////////// FILTER ARRAYS //////////////////////////
107
-	////////////////////////////////////////////////////////////////////
108
-
109
-	/**
110
-	 * Filter buttons classes
111
-	 *
112
-	 * @param  array $classes An array of classes
113
-	 *
114
-	 * @return string[] A filtered array
115
-	 */
116
-	public function filterButtonClasses($classes)
117
-	{
118
-		// Filter classes
119
-		// $classes = array_intersect($classes, $this->buttons);
120
-
121
-		// Prepend button type
122
-		$classes   = $this->prependWith($classes, 'btn-');
123
-		$classes[] = 'btn';
124
-
125
-		return $classes;
126
-	}
127
-
128
-	/**
129
-	 * Filter field classes
130
-	 *
131
-	 * @param  array $classes An array of classes
132
-	 *
133
-	 * @return array A filtered array
134
-	 */
135
-	public function filterFieldClasses($classes)
136
-	{
137
-		// Filter classes
138
-		$classes = array_intersect($classes, $this->fields);
139
-
140
-		// Prepend field type
141
-		$classes = array_map(function ($class) {
142
-			return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
143
-		}, $classes);
144
-
145
-		return $classes;
146
-	}
147
-
148
-	////////////////////////////////////////////////////////////////////
149
-	///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
150
-	////////////////////////////////////////////////////////////////////
151
-
152
-	/**
153
-	 * Framework error state
154
-	 *
155
-	 * @return string
156
-	 */
157
-	public function errorState()
158
-	{
159
-		return 'is-invalid';
160
-	}
161
-
162
-	/**
163
-	 * Returns corresponding inline class of a field
164
-	 *
165
-	 * @param Field $field
166
-	 *
167
-	 * @return string
168
-	 */
169
-	public function getInlineLabelClass($field)
170
-	{
171
-		$inlineClass = parent::getInlineLabelClass($field);
172
-		if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) {
173
-			$inlineClass = 'form-check-label';
174
-		}
175
-
176
-		return $inlineClass;
177
-	}
178
-
179
-	/**
180
-	 * Set the fields width from a label width
181
-	 *
182
-	 * @param array $labelWidths
183
-	 */
184
-	protected function setFieldWidths($labelWidths)
185
-	{
186
-		$labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
187
-
188
-		$viewports = $this->getFrameworkOption('viewports');
189
-		foreach ($labelWidths as $viewport => $columns) {
190
-			if ($viewport) {
191
-				$labelWidthClass .= " col-$viewports[$viewport]-$columns";
192
-				$fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns);
193
-				$fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns";
194
-			}
195
-		}
196
-
197
-		$this->labelWidth  = ltrim($labelWidthClass);
198
-		$this->fieldWidth  = ltrim($fieldWidthClass);
199
-		$this->fieldOffset = ltrim($fieldOffsetClass);
200
-	}
201
-
202
-	////////////////////////////////////////////////////////////////////
203
-	///////////////////////////// ADD CLASSES //////////////////////////
204
-	////////////////////////////////////////////////////////////////////
205
-
206
-	/**
207
-	 * Add classes to a field
208
-	 *
209
-	 * @param Field $field
210
-	 * @param array $classes The possible classes to add
211
-	 *
212
-	 * @return Field
213
-	 */
214
-	public function getFieldClasses(Field $field, $classes)
215
-	{
216
-		// Add inline class for checkables
217
-		if ($field->isCheckable()) {
218
-			$classes[] = 'form-check';
219
-
220
-			if (in_array('inline', $classes)) {
221
-				$field->inline();
222
-			}
223
-		}
224
-
225
-		// Filter classes according to field type
226
-		if ($field->isButton()) {
227
-			$classes = $this->filterButtonClasses($classes);
228
-		} else {
229
-			$classes = $this->filterFieldClasses($classes);
230
-		}
231
-
232
-		// Add form-control class for text-type, textarea and select fields
233
-		// As text-type is open-ended we instead exclude those that shouldn't receive the class
234
-		if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array(
235
-					'file',
236
-					'plaintext',
237
-				)) and !in_array('form-control', $classes)
238
-		) {
239
-			$classes[] = 'form-control';
240
-		}
241
-
242
-		if ($this->app['former']->getErrors($field->getName())) {
243
-			$classes[] = $this->errorState();
244
-		}
245
-
246
-		return $this->addClassesToField($field, $classes);
247
-	}
248
-
249
-	/**
250
-	 * Add group classes
251
-	 *
252
-	 * @return string A list of group classes
253
-	 */
254
-	public function getGroupClasses()
255
-	{
256
-		if ($this->app['former.form']->isOfType('horizontal')) {
257
-			return 'form-group row';
258
-		} else {
259
-			return 'form-group';
260
-		}
261
-	}
262
-
263
-	/**
264
-	 * Add label classes
265
-	 *
266
-	 * @return string[] An array of attributes with the label class
267
-	 */
268
-	public function getLabelClasses()
269
-	{
270
-		if ($this->app['former.form']->isOfType('horizontal')) {
271
-			return array('control-label', $this->labelWidth);
272
-		} elseif ($this->app['former.form']->isOfType('inline')) {
273
-			return array('sr-only');
274
-		} else {
275
-			return array('control-label');
276
-		}
277
-	}
278
-
279
-	/**
280
-	 * Add uneditable field classes
281
-	 *
282
-	 * @return string An array of attributes with the uneditable class
283
-	 */
284
-	public function getUneditableClasses()
285
-	{
286
-		return '';
287
-	}
288
-
289
-	/**
290
-	 * Add plain text field classes
291
-	 *
292
-	 * @return string An array of attributes with the plain text class
293
-	 */
294
-	public function getPlainTextClasses()
295
-	{
296
-		return 'form-control-static';
297
-	}
298
-
299
-	/**
300
-	 * Add form class
301
-	 *
302
-	 * @param  string $type The type of form to add
303
-	 *
304
-	 * @return string|null
305
-	 */
306
-	public function getFormClasses($type)
307
-	{
308
-		return $type ? 'form-'.$type : null;
309
-	}
310
-
311
-	/**
312
-	 * Add actions block class
313
-	 *
314
-	 * @return string|null
315
-	 */
316
-	public function getActionClasses()
317
-	{
318
-		if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) {
319
-			return 'form-group row';
320
-		}
321
-
322
-		return null;
323
-	}
324
-
325
-	////////////////////////////////////////////////////////////////////
326
-	//////////////////////////// RENDER BLOCKS /////////////////////////
327
-	////////////////////////////////////////////////////////////////////
328
-
329
-	/**
330
-	 * Render an help text
331
-	 *
332
-	 * @param string $text
333
-	 * @param array  $attributes
334
-	 *
335
-	 * @return Element
336
-	 */
337
-	public function createHelp($text, $attributes = array())
338
-	{
339
-		return Element::create('small', $text, $attributes)->addClass('text-muted');
340
-	}
341
-
342
-	/**
343
-	 * Render an validation error text
344
-	 *
345
-	 * @param string $text
346
-	 * @param array  $attributes
347
-	 *
348
-	 * @return string
349
-	 */
350
-	public function createValidationError($text, $attributes = array())
351
-	{
352
-		return Element::create('div', $text, $attributes)->addClass('invalid-feedback');
353
-	}
354
-
355
-	/**
356
-	 * Render an help text
357
-	 *
358
-	 * @param string $text
359
-	 * @param array  $attributes
360
-	 *
361
-	 * @return Element
362
-	 */
363
-	public function createBlockHelp($text, $attributes = array())
364
-	{
365
-		return Element::create('small', $text, $attributes)->addClass('form-text text-muted');
366
-	}
367
-
368
-	/**
369
-	 * Render a disabled field
370
-	 *
371
-	 * @param Field $field
372
-	 *
373
-	 * @return Element
374
-	 */
375
-	public function createDisabledField(Field $field)
376
-	{
377
-		return Element::create('span', $field->getValue(), $field->getAttributes());
378
-	}
379
-
380
-	/**
381
-	 * Render a plain text field
382
-	 *
383
-	 * @param Field $field
384
-	 *
385
-	 * @return Element
386
-	 */
387
-	public function createPlainTextField(Field $field)
388
-	{
389
-		$label = $field->getLabel();
390
-		if ($label) {
391
-			$label->for('');
392
-		}
393
-
394
-		return Element::create('div', $field->getValue(), $field->getAttributes());
395
-	}
396
-
397
-	////////////////////////////////////////////////////////////////////
398
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
399
-	////////////////////////////////////////////////////////////////////
400
-
401
-	/**
402
-	 * Wrap an item to be prepended or appended to the current field
403
-	 *
404
-	 * @return Element A wrapped item
405
-	 */
406
-	public function placeAround($item)
407
-	{
408
-		// Render object
409
-		if (is_object($item) and method_exists($item, '__toString')) {
410
-			$item = $item->__toString();
411
-		}
412
-
413
-		// Get class to use
414
-		$class = (strpos($item, '<button') !== false) ? 'btn' : 'addon';
415
-
416
-		return Element::create('span', $item)->addClass('input-group-'.$class);
417
-	}
418
-
419
-	/**
420
-	 * Wrap a field with prepended and appended items
421
-	 *
422
-	 * @param  Field $field
423
-	 * @param  array $prepend
424
-	 * @param  array $append
425
-	 *
426
-	 * @return string A field concatented with prepended and/or appended items
427
-	 */
428
-	public function prependAppend($field, $prepend, $append)
429
-	{
430
-		$return = '<div class="input-group">';
431
-		$return .= join(null, $prepend);
432
-		$return .= $field->render();
433
-		$return .= join(null, $append);
434
-		$return .= '</div>';
435
-
436
-		return $return;
437
-	}
438
-
439
-	/**
440
-	 * Wrap a field with potential additional tags
441
-	 *
442
-	 * @param  Field $field
443
-	 *
444
-	 * @return Element A wrapped field
445
-	 */
446
-	public function wrapField($field)
447
-	{
448
-		if ($this->app['former.form']->isOfType('horizontal')) {
449
-			return Element::create('div', $field)->addClass($this->fieldWidth);
450
-		}
451
-
452
-		return $field;
453
-	}
454
-
455
-	/**
456
-	 * Wrap actions block with potential additional tags
457
-	 *
458
-	 * @param  Actions $actions
459
-	 *
460
-	 * @return string A wrapped actions block
461
-	 */
462
-	public function wrapActions($actions)
463
-	{
464
-		// For horizontal forms, we wrap the actions in a div
465
-		if ($this->app['former.form']->isOfType('horizontal')) {
466
-			return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
467
-		}
468
-
469
-		return $actions;
470
-	}
16
+    /**
17
+     * Form types that trigger special styling for this Framework
18
+     *
19
+     * @var array
20
+     */
21
+    protected $availableTypes = array('horizontal', 'vertical', 'inline');
22
+
23
+    /**
24
+     * The button types available
25
+     *
26
+     * @var array
27
+     */
28
+    private $buttons = array(
29
+        'lg',
30
+        'sm',
31
+        'xs',
32
+        'block',
33
+        'link',
34
+        'primary',
35
+        'secondary',
36
+        'warning',
37
+        'danger',
38
+        'success',
39
+        'info',
40
+        'light',
41
+        'dark',
42
+    );
43
+
44
+    /**
45
+     * The field sizes available
46
+     *
47
+     * @var array
48
+     */
49
+    private $fields = array(
50
+        'lg',
51
+        'sm',
52
+        // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6',
53
+        // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12',
54
+        // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6',
55
+        // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12',
56
+        // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6',
57
+        // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12',
58
+        // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6',
59
+        // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12',
60
+    );
61
+
62
+    /**
63
+     * The field states available
64
+     *
65
+     * @var array
66
+     */
67
+    protected $states = array(
68
+        'is-invalid',
69
+    );
70
+
71
+    /**
72
+     * The default HTML tag used for icons
73
+     *
74
+     * @var string
75
+     */
76
+    protected $iconTag = 'i';
77
+
78
+    /**
79
+     * The default set for icon fonts
80
+     * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome
81
+     *
82
+     * @var string
83
+     */
84
+    protected $iconSet = 'fa';
85
+
86
+    /**
87
+     * The default prefix icon names
88
+     * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular
89
+     *
90
+     * @var string
91
+     */
92
+    protected $iconPrefix = 'fa';
93
+
94
+    /**
95
+     * Create a new TwitterBootstrap instance
96
+     *
97
+     * @param \Illuminate\Container\Container $app
98
+     */
99
+    public function __construct(Container $app)
100
+    {
101
+        $this->app = $app;
102
+        $this->setFrameworkDefaults();
103
+    }
104
+
105
+    ////////////////////////////////////////////////////////////////////
106
+    /////////////////////////// FILTER ARRAYS //////////////////////////
107
+    ////////////////////////////////////////////////////////////////////
108
+
109
+    /**
110
+     * Filter buttons classes
111
+     *
112
+     * @param  array $classes An array of classes
113
+     *
114
+     * @return string[] A filtered array
115
+     */
116
+    public function filterButtonClasses($classes)
117
+    {
118
+        // Filter classes
119
+        // $classes = array_intersect($classes, $this->buttons);
120
+
121
+        // Prepend button type
122
+        $classes   = $this->prependWith($classes, 'btn-');
123
+        $classes[] = 'btn';
124
+
125
+        return $classes;
126
+    }
127
+
128
+    /**
129
+     * Filter field classes
130
+     *
131
+     * @param  array $classes An array of classes
132
+     *
133
+     * @return array A filtered array
134
+     */
135
+    public function filterFieldClasses($classes)
136
+    {
137
+        // Filter classes
138
+        $classes = array_intersect($classes, $this->fields);
139
+
140
+        // Prepend field type
141
+        $classes = array_map(function ($class) {
142
+            return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
143
+        }, $classes);
144
+
145
+        return $classes;
146
+    }
147
+
148
+    ////////////////////////////////////////////////////////////////////
149
+    ///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
150
+    ////////////////////////////////////////////////////////////////////
151
+
152
+    /**
153
+     * Framework error state
154
+     *
155
+     * @return string
156
+     */
157
+    public function errorState()
158
+    {
159
+        return 'is-invalid';
160
+    }
161
+
162
+    /**
163
+     * Returns corresponding inline class of a field
164
+     *
165
+     * @param Field $field
166
+     *
167
+     * @return string
168
+     */
169
+    public function getInlineLabelClass($field)
170
+    {
171
+        $inlineClass = parent::getInlineLabelClass($field);
172
+        if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) {
173
+            $inlineClass = 'form-check-label';
174
+        }
175
+
176
+        return $inlineClass;
177
+    }
178
+
179
+    /**
180
+     * Set the fields width from a label width
181
+     *
182
+     * @param array $labelWidths
183
+     */
184
+    protected function setFieldWidths($labelWidths)
185
+    {
186
+        $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
187
+
188
+        $viewports = $this->getFrameworkOption('viewports');
189
+        foreach ($labelWidths as $viewport => $columns) {
190
+            if ($viewport) {
191
+                $labelWidthClass .= " col-$viewports[$viewport]-$columns";
192
+                $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns);
193
+                $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns";
194
+            }
195
+        }
196
+
197
+        $this->labelWidth  = ltrim($labelWidthClass);
198
+        $this->fieldWidth  = ltrim($fieldWidthClass);
199
+        $this->fieldOffset = ltrim($fieldOffsetClass);
200
+    }
201
+
202
+    ////////////////////////////////////////////////////////////////////
203
+    ///////////////////////////// ADD CLASSES //////////////////////////
204
+    ////////////////////////////////////////////////////////////////////
205
+
206
+    /**
207
+     * Add classes to a field
208
+     *
209
+     * @param Field $field
210
+     * @param array $classes The possible classes to add
211
+     *
212
+     * @return Field
213
+     */
214
+    public function getFieldClasses(Field $field, $classes)
215
+    {
216
+        // Add inline class for checkables
217
+        if ($field->isCheckable()) {
218
+            $classes[] = 'form-check';
219
+
220
+            if (in_array('inline', $classes)) {
221
+                $field->inline();
222
+            }
223
+        }
224
+
225
+        // Filter classes according to field type
226
+        if ($field->isButton()) {
227
+            $classes = $this->filterButtonClasses($classes);
228
+        } else {
229
+            $classes = $this->filterFieldClasses($classes);
230
+        }
231
+
232
+        // Add form-control class for text-type, textarea and select fields
233
+        // As text-type is open-ended we instead exclude those that shouldn't receive the class
234
+        if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array(
235
+                    'file',
236
+                    'plaintext',
237
+                )) and !in_array('form-control', $classes)
238
+        ) {
239
+            $classes[] = 'form-control';
240
+        }
241
+
242
+        if ($this->app['former']->getErrors($field->getName())) {
243
+            $classes[] = $this->errorState();
244
+        }
245
+
246
+        return $this->addClassesToField($field, $classes);
247
+    }
248
+
249
+    /**
250
+     * Add group classes
251
+     *
252
+     * @return string A list of group classes
253
+     */
254
+    public function getGroupClasses()
255
+    {
256
+        if ($this->app['former.form']->isOfType('horizontal')) {
257
+            return 'form-group row';
258
+        } else {
259
+            return 'form-group';
260
+        }
261
+    }
262
+
263
+    /**
264
+     * Add label classes
265
+     *
266
+     * @return string[] An array of attributes with the label class
267
+     */
268
+    public function getLabelClasses()
269
+    {
270
+        if ($this->app['former.form']->isOfType('horizontal')) {
271
+            return array('control-label', $this->labelWidth);
272
+        } elseif ($this->app['former.form']->isOfType('inline')) {
273
+            return array('sr-only');
274
+        } else {
275
+            return array('control-label');
276
+        }
277
+    }
278
+
279
+    /**
280
+     * Add uneditable field classes
281
+     *
282
+     * @return string An array of attributes with the uneditable class
283
+     */
284
+    public function getUneditableClasses()
285
+    {
286
+        return '';
287
+    }
288
+
289
+    /**
290
+     * Add plain text field classes
291
+     *
292
+     * @return string An array of attributes with the plain text class
293
+     */
294
+    public function getPlainTextClasses()
295
+    {
296
+        return 'form-control-static';
297
+    }
298
+
299
+    /**
300
+     * Add form class
301
+     *
302
+     * @param  string $type The type of form to add
303
+     *
304
+     * @return string|null
305
+     */
306
+    public function getFormClasses($type)
307
+    {
308
+        return $type ? 'form-'.$type : null;
309
+    }
310
+
311
+    /**
312
+     * Add actions block class
313
+     *
314
+     * @return string|null
315
+     */
316
+    public function getActionClasses()
317
+    {
318
+        if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) {
319
+            return 'form-group row';
320
+        }
321
+
322
+        return null;
323
+    }
324
+
325
+    ////////////////////////////////////////////////////////////////////
326
+    //////////////////////////// RENDER BLOCKS /////////////////////////
327
+    ////////////////////////////////////////////////////////////////////
328
+
329
+    /**
330
+     * Render an help text
331
+     *
332
+     * @param string $text
333
+     * @param array  $attributes
334
+     *
335
+     * @return Element
336
+     */
337
+    public function createHelp($text, $attributes = array())
338
+    {
339
+        return Element::create('small', $text, $attributes)->addClass('text-muted');
340
+    }
341
+
342
+    /**
343
+     * Render an validation error text
344
+     *
345
+     * @param string $text
346
+     * @param array  $attributes
347
+     *
348
+     * @return string
349
+     */
350
+    public function createValidationError($text, $attributes = array())
351
+    {
352
+        return Element::create('div', $text, $attributes)->addClass('invalid-feedback');
353
+    }
354
+
355
+    /**
356
+     * Render an help text
357
+     *
358
+     * @param string $text
359
+     * @param array  $attributes
360
+     *
361
+     * @return Element
362
+     */
363
+    public function createBlockHelp($text, $attributes = array())
364
+    {
365
+        return Element::create('small', $text, $attributes)->addClass('form-text text-muted');
366
+    }
367
+
368
+    /**
369
+     * Render a disabled field
370
+     *
371
+     * @param Field $field
372
+     *
373
+     * @return Element
374
+     */
375
+    public function createDisabledField(Field $field)
376
+    {
377
+        return Element::create('span', $field->getValue(), $field->getAttributes());
378
+    }
379
+
380
+    /**
381
+     * Render a plain text field
382
+     *
383
+     * @param Field $field
384
+     *
385
+     * @return Element
386
+     */
387
+    public function createPlainTextField(Field $field)
388
+    {
389
+        $label = $field->getLabel();
390
+        if ($label) {
391
+            $label->for('');
392
+        }
393
+
394
+        return Element::create('div', $field->getValue(), $field->getAttributes());
395
+    }
396
+
397
+    ////////////////////////////////////////////////////////////////////
398
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
399
+    ////////////////////////////////////////////////////////////////////
400
+
401
+    /**
402
+     * Wrap an item to be prepended or appended to the current field
403
+     *
404
+     * @return Element A wrapped item
405
+     */
406
+    public function placeAround($item)
407
+    {
408
+        // Render object
409
+        if (is_object($item) and method_exists($item, '__toString')) {
410
+            $item = $item->__toString();
411
+        }
412
+
413
+        // Get class to use
414
+        $class = (strpos($item, '<button') !== false) ? 'btn' : 'addon';
415
+
416
+        return Element::create('span', $item)->addClass('input-group-'.$class);
417
+    }
418
+
419
+    /**
420
+     * Wrap a field with prepended and appended items
421
+     *
422
+     * @param  Field $field
423
+     * @param  array $prepend
424
+     * @param  array $append
425
+     *
426
+     * @return string A field concatented with prepended and/or appended items
427
+     */
428
+    public function prependAppend($field, $prepend, $append)
429
+    {
430
+        $return = '<div class="input-group">';
431
+        $return .= join(null, $prepend);
432
+        $return .= $field->render();
433
+        $return .= join(null, $append);
434
+        $return .= '</div>';
435
+
436
+        return $return;
437
+    }
438
+
439
+    /**
440
+     * Wrap a field with potential additional tags
441
+     *
442
+     * @param  Field $field
443
+     *
444
+     * @return Element A wrapped field
445
+     */
446
+    public function wrapField($field)
447
+    {
448
+        if ($this->app['former.form']->isOfType('horizontal')) {
449
+            return Element::create('div', $field)->addClass($this->fieldWidth);
450
+        }
451
+
452
+        return $field;
453
+    }
454
+
455
+    /**
456
+     * Wrap actions block with potential additional tags
457
+     *
458
+     * @param  Actions $actions
459
+     *
460
+     * @return string A wrapped actions block
461
+     */
462
+    public function wrapActions($actions)
463
+    {
464
+        // For horizontal forms, we wrap the actions in a div
465
+        if ($this->app['former.form']->isOfType('horizontal')) {
466
+            return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
467
+        }
468
+
469
+        return $actions;
470
+    }
471 471
 }
Please login to merge, or discard this patch.