Completed
Push — master ( e4067b...4f8f5b )
by Alex
20s
created
src/Former/Helpers.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
 		//Convert parametrs of old format to new format
128 128
 		if (!is_callable($text)) {
129 129
 			$optionTextValue = $text;
130
-			$text = function ($model) use ($optionTextValue) {
130
+			$text = function($model) use ($optionTextValue) {
131 131
 				if ($optionTextValue and isset($model->$optionTextValue)) {
132 132
 					return $model->$optionTextValue;
133 133
 				} elseif ((is_object($model) || is_string($model)) && method_exists($model, '__toString')) {
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
 					$optionAttributeValue = $modelAttributeName($model);
173 173
 				} elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
174 174
 					$optionAttributeValue = $model->$modelAttributeName;
175
-				} elseif($optionAttributeName === 'value') {
175
+				} elseif ($optionAttributeName === 'value') {
176 176
 					//For backward compatibility
177 177
 					if (method_exists($model, 'getKey')) {
178 178
 						$optionAttributeValue = $model->getKey();
Please login to merge, or discard this patch.
Indentation   +191 added lines, -191 removed lines patch added patch discarded remove patch
@@ -9,195 +9,195 @@
 block discarded – undo
9 9
  */
10 10
 class Helpers
11 11
 {
12
-	/**
13
-	 * The IoC Container
14
-	 *
15
-	 * @var Container
16
-	 */
17
-	protected static $app;
18
-
19
-	/**
20
-	 * Bind a Container to the Helpers class
21
-	 *
22
-	 * @param Container $app
23
-	 */
24
-	public static function setApp(Container $app)
25
-	{
26
-		static::$app = $app;
27
-	}
28
-
29
-	/**
30
-	 * Encodes HTML
31
-	 *
32
-	 * @param string|null $value The string to encode
33
-	 *
34
-	 * @return string
35
-	 */
36
-	public static function encode($value)
37
-	{
38
-		if ($value === null) {
39
-			return '';
40
-		}
41
-
42
-		return htmlentities($value, ENT_QUOTES, 'UTF-8', true);
43
-	}
44
-
45
-	////////////////////////////////////////////////////////////////////
46
-	///////////////////////// LOCALIZATION HELPERS /////////////////////
47
-	////////////////////////////////////////////////////////////////////
48
-
49
-	/**
50
-	 * Translates a string by trying several fallbacks
51
-	 *
52
-	 * @param  string $key      The key to translate
53
-	 * @param  string $fallback The ultimate fallback
54
-	 *
55
-	 * @return string           A translated string
56
-	 */
57
-	public static function translate($key, $fallback = null)
58
-	{
59
-		// If nothing was given, return nothing, bitch
60
-		if (!$key) {
61
-			return $fallback;
62
-		}
63
-
64
-		// If no fallback, use the key
65
-		if (!$fallback) {
66
-			$fallback = $key;
67
-		}
68
-
69
-		// Assure we don't already have a Lang object
70
-		if (is_object($key) and method_exists($key, 'get')) {
71
-			return $key->get();
72
-		}
73
-
74
-		$translation   = null;
75
-		$translateFrom = static::$app['former']->getOption('translate_from');
76
-		if (substr($translateFrom, -1) !== '/') {
77
-			$translateFrom .= '.';
78
-		}
79
-		$translateFrom .= $key;
80
-
81
-		// Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']]
82
-		if (strpos($translateFrom, ']') !== false) {
83
-			$translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom);
84
-		}
85
-
86
-		// Search for the key itself, if it is valid
87
-		$validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1;
88
-		if ($validKey && static::$app['translator']->has($key)) {
89
-			$translation = static::$app['translator']->get($key);
90
-		} elseif (static::$app['translator']->has($translateFrom)) {
91
-			$translation = static::$app['translator']->get($translateFrom);
92
-		}
93
-
94
-		// Replace by fallback if invalid
95
-		if (!$translation or is_array($translation)) {
96
-			$translation = $fallback;
97
-		}
98
-
99
-		// Capitalize
100
-		$capitalize = static::$app['former']->getOption('capitalize_translations');
101
-
102
-		return $capitalize ? ucfirst($translation) : $translation;
103
-	}
104
-
105
-	////////////////////////////////////////////////////////////////////
106
-	////////////////////////// DATABASE HELPERS ////////////////////////
107
-	////////////////////////////////////////////////////////////////////
108
-
109
-	/**
110
-	 * Transforms an array of models into an associative array
111
-	 *
112
-	 * @param  array|object    $query      The array of results
113
-	 * @param  string|function $text       The value to use as text
114
-	 * @param  string|array    $attributes The data to use as attributes
115
-	 *
116
-	 * @return array               A data array
117
-	 */
118
-	public static function queryToArray($query, $text = null, $attributes = null)
119
-	{
120
-		// Automatically fetch Lang objects for people who store translated options lists
121
-		// Same of unfetched queries
122
-		if (!$query instanceof Collection) {
123
-			if ((is_object($query) || is_string($query)) && method_exists($query, 'get')) {
124
-				$query = $query->get();
125
-			}
126
-			if (!is_array($query)) {
127
-				$query = (array) $query;
128
-			}
129
-		}
130
-
131
-		//Convert parametrs of old format to new format
132
-		if (!is_callable($text)) {
133
-			$optionTextValue = $text;
134
-			$text = function ($model) use ($optionTextValue) {
135
-				if ($optionTextValue and isset($model->$optionTextValue)) {
136
-					return $model->$optionTextValue;
137
-				} elseif ((is_object($model) || is_string($model)) && method_exists($model, '__toString')) {
138
-					return  $model->__toString();
139
-				} else {
140
-					return null;
141
-				}
142
-			};
143
-		}
144
-
145
-		if (!is_array($attributes)) {
146
-			if (is_string($attributes)) {
147
-				$attributes = ['value' => $attributes];
148
-			} else {
149
-				$attributes = ['value' => null];
150
-			}
151
-		}
152
-
153
-		if (!isset($attributes['value'])) {
154
-			$attributes['value'] = null;
155
-		}
156
-		//-------------------------------------------------
157
-
158
-		// Populates the new options
159
-		foreach ($query as $model) {
160
-			// If it's an array, convert to object
161
-			if (is_array($model)) {
162
-				$model = (object) $model;
163
-			}
164
-
165
-			// Calculate option text
166
-			$optionText = $text($model);
167
-
168
-			// Skip if no text value found
169
-			if (!$optionText) {
170
-				continue;
171
-			}
172
-
173
-			//Collect option attributes
174
-			foreach ($attributes as $optionAttributeName => $modelAttributeName) {
175
-				if (is_callable($modelAttributeName)) {
176
-					$optionAttributeValue = $modelAttributeName($model);
177
-				} elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
178
-					$optionAttributeValue = $model->$modelAttributeName;
179
-				} elseif($optionAttributeName === 'value') {
180
-					//For backward compatibility
181
-					if (method_exists($model, 'getKey')) {
182
-						$optionAttributeValue = $model->getKey();
183
-					} elseif (isset($model->id)) {
184
-						$optionAttributeValue = $model->id;
185
-					} else {
186
-						$optionAttributeValue = $optionText;
187
-					}
188
-				} else {
189
-					$optionAttributeValue = '';
190
-				}
191
-
192
-				//For backward compatibility
193
-				if (count($attributes) === 1) {
194
-					$array[$optionAttributeValue] = (string) $optionText;
195
-				} else {
196
-					$array[$optionText][$optionAttributeName] = (string) $optionAttributeValue;
197
-				}
198
-			}
199
-		}
200
-
201
-		return !empty($array) ? $array : $query;
202
-	}
12
+    /**
13
+     * The IoC Container
14
+     *
15
+     * @var Container
16
+     */
17
+    protected static $app;
18
+
19
+    /**
20
+     * Bind a Container to the Helpers class
21
+     *
22
+     * @param Container $app
23
+     */
24
+    public static function setApp(Container $app)
25
+    {
26
+        static::$app = $app;
27
+    }
28
+
29
+    /**
30
+     * Encodes HTML
31
+     *
32
+     * @param string|null $value The string to encode
33
+     *
34
+     * @return string
35
+     */
36
+    public static function encode($value)
37
+    {
38
+        if ($value === null) {
39
+            return '';
40
+        }
41
+
42
+        return htmlentities($value, ENT_QUOTES, 'UTF-8', true);
43
+    }
44
+
45
+    ////////////////////////////////////////////////////////////////////
46
+    ///////////////////////// LOCALIZATION HELPERS /////////////////////
47
+    ////////////////////////////////////////////////////////////////////
48
+
49
+    /**
50
+     * Translates a string by trying several fallbacks
51
+     *
52
+     * @param  string $key      The key to translate
53
+     * @param  string $fallback The ultimate fallback
54
+     *
55
+     * @return string           A translated string
56
+     */
57
+    public static function translate($key, $fallback = null)
58
+    {
59
+        // If nothing was given, return nothing, bitch
60
+        if (!$key) {
61
+            return $fallback;
62
+        }
63
+
64
+        // If no fallback, use the key
65
+        if (!$fallback) {
66
+            $fallback = $key;
67
+        }
68
+
69
+        // Assure we don't already have a Lang object
70
+        if (is_object($key) and method_exists($key, 'get')) {
71
+            return $key->get();
72
+        }
73
+
74
+        $translation   = null;
75
+        $translateFrom = static::$app['former']->getOption('translate_from');
76
+        if (substr($translateFrom, -1) !== '/') {
77
+            $translateFrom .= '.';
78
+        }
79
+        $translateFrom .= $key;
80
+
81
+        // Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']]
82
+        if (strpos($translateFrom, ']') !== false) {
83
+            $translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom);
84
+        }
85
+
86
+        // Search for the key itself, if it is valid
87
+        $validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1;
88
+        if ($validKey && static::$app['translator']->has($key)) {
89
+            $translation = static::$app['translator']->get($key);
90
+        } elseif (static::$app['translator']->has($translateFrom)) {
91
+            $translation = static::$app['translator']->get($translateFrom);
92
+        }
93
+
94
+        // Replace by fallback if invalid
95
+        if (!$translation or is_array($translation)) {
96
+            $translation = $fallback;
97
+        }
98
+
99
+        // Capitalize
100
+        $capitalize = static::$app['former']->getOption('capitalize_translations');
101
+
102
+        return $capitalize ? ucfirst($translation) : $translation;
103
+    }
104
+
105
+    ////////////////////////////////////////////////////////////////////
106
+    ////////////////////////// DATABASE HELPERS ////////////////////////
107
+    ////////////////////////////////////////////////////////////////////
108
+
109
+    /**
110
+     * Transforms an array of models into an associative array
111
+     *
112
+     * @param  array|object    $query      The array of results
113
+     * @param  string|function $text       The value to use as text
114
+     * @param  string|array    $attributes The data to use as attributes
115
+     *
116
+     * @return array               A data array
117
+     */
118
+    public static function queryToArray($query, $text = null, $attributes = null)
119
+    {
120
+        // Automatically fetch Lang objects for people who store translated options lists
121
+        // Same of unfetched queries
122
+        if (!$query instanceof Collection) {
123
+            if ((is_object($query) || is_string($query)) && method_exists($query, 'get')) {
124
+                $query = $query->get();
125
+            }
126
+            if (!is_array($query)) {
127
+                $query = (array) $query;
128
+            }
129
+        }
130
+
131
+        //Convert parametrs of old format to new format
132
+        if (!is_callable($text)) {
133
+            $optionTextValue = $text;
134
+            $text = function ($model) use ($optionTextValue) {
135
+                if ($optionTextValue and isset($model->$optionTextValue)) {
136
+                    return $model->$optionTextValue;
137
+                } elseif ((is_object($model) || is_string($model)) && method_exists($model, '__toString')) {
138
+                    return  $model->__toString();
139
+                } else {
140
+                    return null;
141
+                }
142
+            };
143
+        }
144
+
145
+        if (!is_array($attributes)) {
146
+            if (is_string($attributes)) {
147
+                $attributes = ['value' => $attributes];
148
+            } else {
149
+                $attributes = ['value' => null];
150
+            }
151
+        }
152
+
153
+        if (!isset($attributes['value'])) {
154
+            $attributes['value'] = null;
155
+        }
156
+        //-------------------------------------------------
157
+
158
+        // Populates the new options
159
+        foreach ($query as $model) {
160
+            // If it's an array, convert to object
161
+            if (is_array($model)) {
162
+                $model = (object) $model;
163
+            }
164
+
165
+            // Calculate option text
166
+            $optionText = $text($model);
167
+
168
+            // Skip if no text value found
169
+            if (!$optionText) {
170
+                continue;
171
+            }
172
+
173
+            //Collect option attributes
174
+            foreach ($attributes as $optionAttributeName => $modelAttributeName) {
175
+                if (is_callable($modelAttributeName)) {
176
+                    $optionAttributeValue = $modelAttributeName($model);
177
+                } elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
178
+                    $optionAttributeValue = $model->$modelAttributeName;
179
+                } elseif($optionAttributeName === 'value') {
180
+                    //For backward compatibility
181
+                    if (method_exists($model, 'getKey')) {
182
+                        $optionAttributeValue = $model->getKey();
183
+                    } elseif (isset($model->id)) {
184
+                        $optionAttributeValue = $model->id;
185
+                    } else {
186
+                        $optionAttributeValue = $optionText;
187
+                    }
188
+                } else {
189
+                    $optionAttributeValue = '';
190
+                }
191
+
192
+                //For backward compatibility
193
+                if (count($attributes) === 1) {
194
+                    $array[$optionAttributeValue] = (string) $optionText;
195
+                } else {
196
+                    $array[$optionText][$optionAttributeName] = (string) $optionAttributeValue;
197
+                }
198
+            }
199
+        }
200
+
201
+        return !empty($array) ? $array : $query;
202
+    }
203 203
 }
Please login to merge, or discard this patch.
src/Former/Form/Fields/Plaintext.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -9,31 +9,31 @@
 block discarded – undo
9 9
  */
10 10
 class Plaintext extends Field
11 11
 {
12
-	////////////////////////////////////////////////////////////////////
13
-	/////////////////////////// CORE METHODS ///////////////////////////
14
-	////////////////////////////////////////////////////////////////////
12
+    ////////////////////////////////////////////////////////////////////
13
+    /////////////////////////// CORE METHODS ///////////////////////////
14
+    ////////////////////////////////////////////////////////////////////
15 15
 
16
-	/**
17
-	 * Prints out the current tag
18
-	 *
19
-	 * @return string A plain text tag
20
-	 */
21
-	public function render()
22
-	{
23
-		$this->addClass($this->app['former.framework']->getPlainTextClasses());
24
-		$this->setId();
25
-		if ($this->app['former']->getOption('escape_plaintext_value', true)) {
26
-			$this->escapeValue();
27
-		}
16
+    /**
17
+     * Prints out the current tag
18
+     *
19
+     * @return string A plain text tag
20
+     */
21
+    public function render()
22
+    {
23
+        $this->addClass($this->app['former.framework']->getPlainTextClasses());
24
+        $this->setId();
25
+        if ($this->app['former']->getOption('escape_plaintext_value', true)) {
26
+            $this->escapeValue();
27
+        }
28 28
 
29
-		return $this->app['former.framework']->createPlainTextField($this);
30
-	}
29
+        return $this->app['former.framework']->createPlainTextField($this);
30
+    }
31 31
 
32
-	protected function escapeValue()
33
-	{
34
-		$valueToEscape = $this->getValue();
35
-		$value = is_string($valueToEscape) || $valueToEscape instanceof HtmlString ? e($valueToEscape) : $valueToEscape;
32
+    protected function escapeValue()
33
+    {
34
+        $valueToEscape = $this->getValue();
35
+        $value = is_string($valueToEscape) || $valueToEscape instanceof HtmlString ? e($valueToEscape) : $valueToEscape;
36 36
 
37
-		return $this->forceValue($value);
38
-	}
37
+        return $this->forceValue($value);
38
+    }
39 39
 }
Please login to merge, or discard this patch.
src/config/former.php 1 patch
Indentation   +235 added lines, -235 removed lines patch added patch discarded remove patch
@@ -1,240 +1,240 @@
 block discarded – undo
1 1
 <?php return array(
2 2
 
3
-	// Markup
4
-	////////////////////////////////////////////////////////////////////
5
-
6
-	// Whether labels should be automatically computed from name
7
-	'automatic_label'         => true,
8
-
9
-	// The default form type
10
-	'default_form_type'       => 'horizontal',
11
-
12
-	// Whether Former should escape HTML tags of 'plaintext' fields value
13
-	// Enabled by default
14
-	//
15
-	// Instead of disabled this option, you should use the 'HtmlString' class:
16
-	//  Former::plaintext('text')
17
-	//      ->forceValue(
18
-	//          new Illuminate\Support\HtmlString('<b>your HTML data</b>')
19
-	//      )
20
-	'escape_plaintext_value'  => true,
21
-
22
-	// Validation
23
-	////////////////////////////////////////////////////////////////////
24
-
25
-	// Whether Former should fetch errors from Session
26
-	'fetch_errors'            => true,
27
-
28
-	// Whether Former should try to apply Validator rules as attributes
29
-	'live_validation'         => true,
30
-
31
-	// Whether Former should automatically fetch error messages and
32
-	// display them next to the matching fields
33
-	'error_messages'          => true,
34
-
35
-	// Checkables
36
-	////////////////////////////////////////////////////////////////////
37
-
38
-	// Whether checkboxes should always be present in the POST data,
39
-	// no matter if you checked them or not
40
-	'push_checkboxes'         => false,
41
-
42
-	// The value a checkbox will have in the POST array if unchecked
43
-	'unchecked_value'         => 0,
44
-
45
-	// Required fields
46
-	////////////////////////////////////////////////////////////////////
47
-
48
-	// The class to be added to required fields
49
-	'required_class'          => 'required',
50
-
51
-	// A facultative text to append to the labels of required fields
52
-	'required_text'           => '<sup>*</sup>',
53
-
54
-	// Translations
55
-	////////////////////////////////////////////////////////////////////
56
-
57
-	// Where Former should look for translations
58
-	'translate_from'          => 'validation.attributes',
59
-
60
-	// Whether text that comes out of the translated
61
-	// should be capitalized (ex: email => Email) automatically
62
-	'capitalize_translations' => true,
63
-
64
-	// An array of attributes to automatically translate
65
-	'translatable'            => array(
66
-		'help',
67
-		'inlineHelp',
68
-		'blockHelp',
69
-		'placeholder',
70
-		'data_placeholder',
71
-		'label',
72
-	),
73
-
74
-	// Framework
75
-	////////////////////////////////////////////////////////////////////
76
-
77
-	// The framework to be used by Former
78
-	'framework'               => 'TwitterBootstrap3',
79
-
80
-	'TwitterBootstrap5'       => array(
81
-
82
-		// Map Former-supported viewports to Bootstrap 5 equivalents
83
-		'viewports'   => array(
84
-			'large'  => 'lg',
85
-			'medium' => 'md',
86
-			'small'  => 'sm',
87
-			'mini'   => 'xs',
88
-		),
89
-		// Width of labels for horizontal forms expressed as viewport => grid columns
90
-		'labelWidths' => array(
91
-			'large' => 2,
92
-			'small' => 4,
93
-		),
94
-		// HTML markup and classes used by Bootstrap 5 for icons
95
-		'icon'        => array(
96
-			'tag'    => 'i',
97
-			'set'    => 'fa',
98
-			'prefix' => 'fa',
99
-		),
100
-
101
-	),
102
-
103
-	'TwitterBootstrap4'       => array(
104
-
105
-		// Map Former-supported viewports to Bootstrap 4 equivalents
106
-		'viewports'   => array(
107
-			'large'  => 'lg',
108
-			'medium' => 'md',
109
-			'small'  => 'sm',
110
-			'mini'   => 'xs',
111
-		),
112
-		// Width of labels for horizontal forms expressed as viewport => grid columns
113
-		'labelWidths' => array(
114
-			'large' => 2,
115
-			'small' => 4,
116
-		),
117
-		// HTML markup and classes used by Bootstrap 4 for icons
118
-		'icon'        => array(
119
-			'tag'    => 'i',
120
-			'set'    => 'fa',
121
-			'prefix' => 'fa',
122
-		),
123
-
124
-	),
125
-
126
-	'TwitterBootstrap3'       => array(
127
-
128
-		// Map Former-supported viewports to Bootstrap 3 equivalents
129
-		'viewports'   => array(
130
-			'large'  => 'lg',
131
-			'medium' => 'md',
132
-			'small'  => 'sm',
133
-			'mini'   => 'xs',
134
-		),
135
-		// Width of labels for horizontal forms expressed as viewport => grid columns
136
-		'labelWidths' => array(
137
-			'large' => 2,
138
-			'small' => 4,
139
-		),
140
-		// HTML markup and classes used by Bootstrap 3 for icons
141
-		'icon'        => array(
142
-			'tag'    => 'span',
143
-			'set'    => 'glyphicon',
144
-			'prefix' => 'glyphicon',
145
-		),
146
-
147
-	),
148
-
149
-	'Nude'                    => array(  // No-framework markup
150
-		'icon' => array(
151
-			'tag'    => 'i',
152
-			'set'    => null,
153
-			'prefix' => 'icon',
154
-		),
155
-	),
156
-
157
-	'TwitterBootstrap'        => array( // Twitter Bootstrap version 2
158
-		'icon' => array(
159
-			'tag'    => 'i',
160
-			'set'    => null,
161
-			'prefix' => 'icon',
162
-		),
163
-	),
164
-
165
-	'ZurbFoundation5'         => array(
166
-		// Map Former-supported viewports to Foundation 5 equivalents
167
-		'viewports'           => array(
168
-			'large'  => 'large',
169
-			'medium' => null,
170
-			'small'  => 'small',
171
-			'mini'   => null,
172
-		),
173
-		// Width of labels for horizontal forms expressed as viewport => grid columns
174
-		'labelWidths'         => array(
175
-			'small' => 3,
176
-		),
177
-		// Classes to be applied to wrapped labels in horizontal forms
178
-		'wrappedLabelClasses' => array('right', 'inline'),
179
-		// HTML markup and classes used by Foundation 5 for icons
180
-		'icon'                => array(
181
-			'tag'    => 'i',
182
-			'set'    => null,
183
-			'prefix' => 'fi',
184
-		),
185
-		// CSS for inline validation errors
186
-		'error_classes'       => array('class' => 'error'),
187
-	),
188
-
189
-	'ZurbFoundation4'         => array(
190
-		// Foundation 4 also has an experimental "medium" breakpoint
191
-		// explained at http://foundation.zurb.com/docs/components/grid.html
192
-		'viewports'           => array(
193
-			'large'  => 'large',
194
-			'medium' => null,
195
-			'small'  => 'small',
196
-			'mini'   => null,
197
-		),
198
-		// Width of labels for horizontal forms expressed as viewport => grid columns
199
-		'labelWidths'         => array(
200
-			'small' => 3,
201
-		),
202
-		// Classes to be applied to wrapped labels in horizontal forms
203
-		'wrappedLabelClasses' => array('right', 'inline'),
204
-		// HTML markup and classes used by Foundation 4 for icons
205
-		'icon'                => array(
206
-			'tag'    => 'i',
207
-			'set'    => 'general',
208
-			'prefix' => 'foundicon',
209
-		),
210
-		// CSS for inline validation errors
211
-		'error_classes'       => array('class' => 'alert-box radius warning'),
212
-	),
213
-
214
-	'ZurbFoundation'          => array( // Foundation 3
215
-		'viewports'           => array(
216
-			'large'  => '',
217
-			'medium' => null,
218
-			'small'  => 'mobile-',
219
-			'mini'   => null,
220
-		),
221
-		// Width of labels for horizontal forms expressed as viewport => grid columns
222
-		'labelWidths'         => array(
223
-			'large' => 2,
224
-			'small' => 4,
225
-		),
226
-		// Classes to be applied to wrapped labels in horizontal forms
227
-		'wrappedLabelClasses' => array('right', 'inline'),
228
-		// HTML markup and classes used by Foundation 3 for icons
229
-		'icon'                => array(
230
-			'tag'    => 'i',
231
-			'set'    => null,
232
-			'prefix' => 'fi',
233
-		),
234
-		// CSS for inline validation errors
235
-		// should work for Zurb 2 and 3
236
-		'error_classes'       => array('class' => 'alert-box alert error'),
237
-	),
3
+    // Markup
4
+    ////////////////////////////////////////////////////////////////////
5
+
6
+    // Whether labels should be automatically computed from name
7
+    'automatic_label'         => true,
8
+
9
+    // The default form type
10
+    'default_form_type'       => 'horizontal',
11
+
12
+    // Whether Former should escape HTML tags of 'plaintext' fields value
13
+    // Enabled by default
14
+    //
15
+    // Instead of disabled this option, you should use the 'HtmlString' class:
16
+    //  Former::plaintext('text')
17
+    //      ->forceValue(
18
+    //          new Illuminate\Support\HtmlString('<b>your HTML data</b>')
19
+    //      )
20
+    'escape_plaintext_value'  => true,
21
+
22
+    // Validation
23
+    ////////////////////////////////////////////////////////////////////
24
+
25
+    // Whether Former should fetch errors from Session
26
+    'fetch_errors'            => true,
27
+
28
+    // Whether Former should try to apply Validator rules as attributes
29
+    'live_validation'         => true,
30
+
31
+    // Whether Former should automatically fetch error messages and
32
+    // display them next to the matching fields
33
+    'error_messages'          => true,
34
+
35
+    // Checkables
36
+    ////////////////////////////////////////////////////////////////////
37
+
38
+    // Whether checkboxes should always be present in the POST data,
39
+    // no matter if you checked them or not
40
+    'push_checkboxes'         => false,
41
+
42
+    // The value a checkbox will have in the POST array if unchecked
43
+    'unchecked_value'         => 0,
44
+
45
+    // Required fields
46
+    ////////////////////////////////////////////////////////////////////
47
+
48
+    // The class to be added to required fields
49
+    'required_class'          => 'required',
50
+
51
+    // A facultative text to append to the labels of required fields
52
+    'required_text'           => '<sup>*</sup>',
53
+
54
+    // Translations
55
+    ////////////////////////////////////////////////////////////////////
56
+
57
+    // Where Former should look for translations
58
+    'translate_from'          => 'validation.attributes',
59
+
60
+    // Whether text that comes out of the translated
61
+    // should be capitalized (ex: email => Email) automatically
62
+    'capitalize_translations' => true,
63
+
64
+    // An array of attributes to automatically translate
65
+    'translatable'            => array(
66
+        'help',
67
+        'inlineHelp',
68
+        'blockHelp',
69
+        'placeholder',
70
+        'data_placeholder',
71
+        'label',
72
+    ),
73
+
74
+    // Framework
75
+    ////////////////////////////////////////////////////////////////////
76
+
77
+    // The framework to be used by Former
78
+    'framework'               => 'TwitterBootstrap3',
79
+
80
+    'TwitterBootstrap5'       => array(
81
+
82
+        // Map Former-supported viewports to Bootstrap 5 equivalents
83
+        'viewports'   => array(
84
+            'large'  => 'lg',
85
+            'medium' => 'md',
86
+            'small'  => 'sm',
87
+            'mini'   => 'xs',
88
+        ),
89
+        // Width of labels for horizontal forms expressed as viewport => grid columns
90
+        'labelWidths' => array(
91
+            'large' => 2,
92
+            'small' => 4,
93
+        ),
94
+        // HTML markup and classes used by Bootstrap 5 for icons
95
+        'icon'        => array(
96
+            'tag'    => 'i',
97
+            'set'    => 'fa',
98
+            'prefix' => 'fa',
99
+        ),
100
+
101
+    ),
102
+
103
+    'TwitterBootstrap4'       => array(
104
+
105
+        // Map Former-supported viewports to Bootstrap 4 equivalents
106
+        'viewports'   => array(
107
+            'large'  => 'lg',
108
+            'medium' => 'md',
109
+            'small'  => 'sm',
110
+            'mini'   => 'xs',
111
+        ),
112
+        // Width of labels for horizontal forms expressed as viewport => grid columns
113
+        'labelWidths' => array(
114
+            'large' => 2,
115
+            'small' => 4,
116
+        ),
117
+        // HTML markup and classes used by Bootstrap 4 for icons
118
+        'icon'        => array(
119
+            'tag'    => 'i',
120
+            'set'    => 'fa',
121
+            'prefix' => 'fa',
122
+        ),
123
+
124
+    ),
125
+
126
+    'TwitterBootstrap3'       => array(
127
+
128
+        // Map Former-supported viewports to Bootstrap 3 equivalents
129
+        'viewports'   => array(
130
+            'large'  => 'lg',
131
+            'medium' => 'md',
132
+            'small'  => 'sm',
133
+            'mini'   => 'xs',
134
+        ),
135
+        // Width of labels for horizontal forms expressed as viewport => grid columns
136
+        'labelWidths' => array(
137
+            'large' => 2,
138
+            'small' => 4,
139
+        ),
140
+        // HTML markup and classes used by Bootstrap 3 for icons
141
+        'icon'        => array(
142
+            'tag'    => 'span',
143
+            'set'    => 'glyphicon',
144
+            'prefix' => 'glyphicon',
145
+        ),
146
+
147
+    ),
148
+
149
+    'Nude'                    => array(  // No-framework markup
150
+        'icon' => array(
151
+            'tag'    => 'i',
152
+            'set'    => null,
153
+            'prefix' => 'icon',
154
+        ),
155
+    ),
156
+
157
+    'TwitterBootstrap'        => array( // Twitter Bootstrap version 2
158
+        'icon' => array(
159
+            'tag'    => 'i',
160
+            'set'    => null,
161
+            'prefix' => 'icon',
162
+        ),
163
+    ),
164
+
165
+    'ZurbFoundation5'         => array(
166
+        // Map Former-supported viewports to Foundation 5 equivalents
167
+        'viewports'           => array(
168
+            'large'  => 'large',
169
+            'medium' => null,
170
+            'small'  => 'small',
171
+            'mini'   => null,
172
+        ),
173
+        // Width of labels for horizontal forms expressed as viewport => grid columns
174
+        'labelWidths'         => array(
175
+            'small' => 3,
176
+        ),
177
+        // Classes to be applied to wrapped labels in horizontal forms
178
+        'wrappedLabelClasses' => array('right', 'inline'),
179
+        // HTML markup and classes used by Foundation 5 for icons
180
+        'icon'                => array(
181
+            'tag'    => 'i',
182
+            'set'    => null,
183
+            'prefix' => 'fi',
184
+        ),
185
+        // CSS for inline validation errors
186
+        'error_classes'       => array('class' => 'error'),
187
+    ),
188
+
189
+    'ZurbFoundation4'         => array(
190
+        // Foundation 4 also has an experimental "medium" breakpoint
191
+        // explained at http://foundation.zurb.com/docs/components/grid.html
192
+        'viewports'           => array(
193
+            'large'  => 'large',
194
+            'medium' => null,
195
+            'small'  => 'small',
196
+            'mini'   => null,
197
+        ),
198
+        // Width of labels for horizontal forms expressed as viewport => grid columns
199
+        'labelWidths'         => array(
200
+            'small' => 3,
201
+        ),
202
+        // Classes to be applied to wrapped labels in horizontal forms
203
+        'wrappedLabelClasses' => array('right', 'inline'),
204
+        // HTML markup and classes used by Foundation 4 for icons
205
+        'icon'                => array(
206
+            'tag'    => 'i',
207
+            'set'    => 'general',
208
+            'prefix' => 'foundicon',
209
+        ),
210
+        // CSS for inline validation errors
211
+        'error_classes'       => array('class' => 'alert-box radius warning'),
212
+    ),
213
+
214
+    'ZurbFoundation'          => array( // Foundation 3
215
+        'viewports'           => array(
216
+            'large'  => '',
217
+            'medium' => null,
218
+            'small'  => 'mobile-',
219
+            'mini'   => null,
220
+        ),
221
+        // Width of labels for horizontal forms expressed as viewport => grid columns
222
+        'labelWidths'         => array(
223
+            'large' => 2,
224
+            'small' => 4,
225
+        ),
226
+        // Classes to be applied to wrapped labels in horizontal forms
227
+        'wrappedLabelClasses' => array('right', 'inline'),
228
+        // HTML markup and classes used by Foundation 3 for icons
229
+        'icon'                => array(
230
+            'tag'    => 'i',
231
+            'set'    => null,
232
+            'prefix' => 'fi',
233
+        ),
234
+        // CSS for inline validation errors
235
+        // should work for Zurb 2 and 3
236
+        'error_classes'       => array('class' => 'alert-box alert error'),
237
+    ),
238 238
 
239 239
 
240 240
 );
Please login to merge, or discard this patch.
src/Former/Framework/TwitterBootstrap5.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -139,7 +139,7 @@
 block discarded – undo
139 139
 		$classes = array_intersect($classes, $this->fields);
140 140
 
141 141
 		// Prepend field type
142
-		$classes = array_map(function ($class) {
142
+		$classes = array_map(function($class) {
143 143
 			return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
144 144
 		}, $classes);
145 145
 
Please login to merge, or discard this patch.
Indentation   +480 added lines, -480 removed lines patch added patch discarded remove patch
@@ -13,484 +13,484 @@
 block discarded – undo
13 13
  */
14 14
 class TwitterBootstrap5 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 .= " offset-$viewports[$viewport]-$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
-			// Adds correct checkbox input class when is a checkbox (or radio)
219
-			$field->addClass('form-check-input');
220
-			$classes[] = 'form-check';
221
-
222
-			if (in_array('inline', $classes)) {
223
-				$field->inline();
224
-			}
225
-		}
226
-
227
-		// Filter classes according to field type
228
-		if ($field->isButton()) {
229
-			$classes = $this->filterButtonClasses($classes);
230
-		} else {
231
-			$classes = $this->filterFieldClasses($classes);
232
-		}
233
-
234
-		// Add form-control class for text-type, textarea and file fields
235
-		// As text-type is open-ended we instead exclude those that shouldn't receive the class
236
-		if (!$field->isCheckable() && !$field->isButton() && !in_array($field->getType(), [
237
-					'plaintext',
238
-					'select',
239
-				]) && !in_array('form-control', $classes)
240
-		) {
241
-			$classes[] = 'form-control';
242
-		}
243
-
244
-		// Add form-select class for select fields
245
-		if ($field->getType() === 'select' && !in_array('form-select', $classes)) {
246
-			$classes[] = 'form-select';
247
-		}
248
-
249
-		if ($this->app['former']->getErrors($field->getName())) {
250
-			$classes[] = $this->errorState();
251
-		}
252
-
253
-		return $this->addClassesToField($field, $classes);
254
-	}
255
-
256
-	/**
257
-	 * Add group classes
258
-	 *
259
-	 * @return string A list of group classes
260
-	 */
261
-	public function getGroupClasses()
262
-	{
263
-		if ($this->app['former.form']->isOfType('horizontal')) {
264
-			return 'mb-3 row';
265
-		} else {
266
-			return 'mb-3';
267
-		}
268
-	}
269
-
270
-	/**
271
-	 * Add label classes
272
-	 *
273
-	 * @return string[] An array of attributes with the label class
274
-	 */
275
-	public function getLabelClasses()
276
-	{
277
-		if ($this->app['former.form']->isOfType('horizontal')) {
278
-			return array('col-form-label', $this->labelWidth);
279
-		} elseif ($this->app['former.form']->isOfType('inline')) {
280
-			return array('visually-hidden');
281
-		} else {
282
-			return array('form-label');
283
-		}
284
-	}
285
-
286
-	/**
287
-	 * Add uneditable field classes
288
-	 *
289
-	 * @return string An array of attributes with the uneditable class
290
-	 */
291
-	public function getUneditableClasses()
292
-	{
293
-		return '';
294
-	}
295
-
296
-	/**
297
-	 * Add plain text field classes
298
-	 *
299
-	 * @return string An array of attributes with the plain text class
300
-	 */
301
-	public function getPlainTextClasses()
302
-	{
303
-		return 'form-control-plaintext';
304
-	}
305
-
306
-	/**
307
-	 * Add form class
308
-	 *
309
-	 * @param  string $type The type of form to add
310
-	 *
311
-	 * @return string|null
312
-	 */
313
-	public function getFormClasses($type)
314
-	{
315
-		return $type ? 'form-'.$type : null;
316
-	}
317
-
318
-	/**
319
-	 * Add actions block class
320
-	 *
321
-	 * @return string|null
322
-	 */
323
-	public function getActionClasses()
324
-	{
325
-		if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) {
326
-			return 'mb-3 row';
327
-		}
328
-
329
-		return null;
330
-	}
331
-
332
-	/**
333
-	 * Add floating label class
334
-	 *
335
-	 * @return string Get the floating label class
336
-	 */
337
-	public function getFloatingLabelClass()
338
-	{
339
-		return 'form-floating';
340
-	}
341
-
342
-	////////////////////////////////////////////////////////////////////
343
-	//////////////////////////// RENDER BLOCKS /////////////////////////
344
-	////////////////////////////////////////////////////////////////////
345
-
346
-	/**
347
-	 * Render an help text
348
-	 *
349
-	 * @param string $text
350
-	 * @param array  $attributes
351
-	 *
352
-	 * @return Element
353
-	 */
354
-	public function createHelp($text, $attributes = array())
355
-	{
356
-		return Element::create('span', $text, $attributes)->addClass('form-text');
357
-	}
358
-
359
-	/**
360
-	 * Render an validation error text
361
-	 *
362
-	 * @param string $text
363
-	 * @param array  $attributes
364
-	 *
365
-	 * @return string
366
-	 */
367
-	public function createValidationError($text, $attributes = array())
368
-	{
369
-		return Element::create('div', $text, $attributes)->addClass('invalid-feedback');
370
-	}
371
-
372
-	/**
373
-	 * Render an help text
374
-	 *
375
-	 * @param string $text
376
-	 * @param array  $attributes
377
-	 *
378
-	 * @return Element
379
-	 */
380
-	public function createBlockHelp($text, $attributes = array())
381
-	{
382
-		return Element::create('div', $text, $attributes)->addClass('form-text');
383
-	}
384
-
385
-	/**
386
-	 * Render a disabled field
387
-	 *
388
-	 * @param Field $field
389
-	 *
390
-	 * @return Element
391
-	 */
392
-	public function createDisabledField(Field $field)
393
-	{
394
-		return Element::create('span', $field->getValue(), $field->getAttributes());
395
-	}
396
-
397
-	/**
398
-	 * Render a plain text field
399
-	 *
400
-	 * @param Field $field
401
-	 *
402
-	 * @return Element
403
-	 */
404
-	public function createPlainTextField(Field $field)
405
-	{
406
-		$label = $field->getLabel();
407
-		if ($label) {
408
-			$label->for('');
409
-		}
410
-
411
-		return Element::create('div', $field->getValue(), $field->getAttributes());
412
-	}
413
-
414
-	////////////////////////////////////////////////////////////////////
415
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
416
-	////////////////////////////////////////////////////////////////////
417
-
418
-	/**
419
-	 * Wrap an item to be prepended or appended to the current field
420
-	 *
421
-	 * @return Element A wrapped item
422
-	 */
423
-	public function placeAround($item, $place = null)
424
-	{
425
-		// Render object
426
-		if (is_object($item) and method_exists($item, '__toString')) {
427
-			$item = $item->__toString();
428
-		}
429
-
430
-		$items = (array) $item;
431
-		$element = '';
432
-		foreach ($items as $item) {
433
-			$hasButtonTag = strpos(ltrim($item), '<button') === 0;
434
-
435
-			// Get class to use
436
-			$class = $hasButtonTag ? '' : 'input-group-text';
437
-
438
-			$element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class);
439
-		}
440
-
441
-		return $element;
442
-	}
443
-
444
-	/**
445
-	 * Wrap a field with prepended and appended items
446
-	 *
447
-	 * @param  Field $field
448
-	 * @param  array $prepend
449
-	 * @param  array $append
450
-	 *
451
-	 * @return string A field concatented with prepended and/or appended items
452
-	 */
453
-	public function prependAppend($field, $prepend, $append)
454
-	{
455
-		$return = '<div class="input-group">';
456
-		$return .= implode('', $prepend);
457
-		$return .= $field->render();
458
-		$return .= implode('', $append);
459
-		$return .= '</div>';
460
-
461
-		return $return;
462
-	}
463
-
464
-	/**
465
-	 * Wrap a field with potential additional tags
466
-	 *
467
-	 * @param  Field $field
468
-	 *
469
-	 * @return Element A wrapped field
470
-	 */
471
-	public function wrapField($field)
472
-	{
473
-		if ($this->app['former.form']->isOfType('horizontal')) {
474
-			return Element::create('div', $field)->addClass($this->fieldWidth);
475
-		}
476
-
477
-		return $field;
478
-	}
479
-
480
-	/**
481
-	 * Wrap actions block with potential additional tags
482
-	 *
483
-	 * @param  Actions $actions
484
-	 *
485
-	 * @return string A wrapped actions block
486
-	 */
487
-	public function wrapActions($actions)
488
-	{
489
-		// For horizontal forms, we wrap the actions in a div
490
-		if ($this->app['former.form']->isOfType('horizontal')) {
491
-			return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
492
-		}
493
-
494
-		return $actions;
495
-	}
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 .= " offset-$viewports[$viewport]-$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
+            // Adds correct checkbox input class when is a checkbox (or radio)
219
+            $field->addClass('form-check-input');
220
+            $classes[] = 'form-check';
221
+
222
+            if (in_array('inline', $classes)) {
223
+                $field->inline();
224
+            }
225
+        }
226
+
227
+        // Filter classes according to field type
228
+        if ($field->isButton()) {
229
+            $classes = $this->filterButtonClasses($classes);
230
+        } else {
231
+            $classes = $this->filterFieldClasses($classes);
232
+        }
233
+
234
+        // Add form-control class for text-type, textarea and file fields
235
+        // As text-type is open-ended we instead exclude those that shouldn't receive the class
236
+        if (!$field->isCheckable() && !$field->isButton() && !in_array($field->getType(), [
237
+                    'plaintext',
238
+                    'select',
239
+                ]) && !in_array('form-control', $classes)
240
+        ) {
241
+            $classes[] = 'form-control';
242
+        }
243
+
244
+        // Add form-select class for select fields
245
+        if ($field->getType() === 'select' && !in_array('form-select', $classes)) {
246
+            $classes[] = 'form-select';
247
+        }
248
+
249
+        if ($this->app['former']->getErrors($field->getName())) {
250
+            $classes[] = $this->errorState();
251
+        }
252
+
253
+        return $this->addClassesToField($field, $classes);
254
+    }
255
+
256
+    /**
257
+     * Add group classes
258
+     *
259
+     * @return string A list of group classes
260
+     */
261
+    public function getGroupClasses()
262
+    {
263
+        if ($this->app['former.form']->isOfType('horizontal')) {
264
+            return 'mb-3 row';
265
+        } else {
266
+            return 'mb-3';
267
+        }
268
+    }
269
+
270
+    /**
271
+     * Add label classes
272
+     *
273
+     * @return string[] An array of attributes with the label class
274
+     */
275
+    public function getLabelClasses()
276
+    {
277
+        if ($this->app['former.form']->isOfType('horizontal')) {
278
+            return array('col-form-label', $this->labelWidth);
279
+        } elseif ($this->app['former.form']->isOfType('inline')) {
280
+            return array('visually-hidden');
281
+        } else {
282
+            return array('form-label');
283
+        }
284
+    }
285
+
286
+    /**
287
+     * Add uneditable field classes
288
+     *
289
+     * @return string An array of attributes with the uneditable class
290
+     */
291
+    public function getUneditableClasses()
292
+    {
293
+        return '';
294
+    }
295
+
296
+    /**
297
+     * Add plain text field classes
298
+     *
299
+     * @return string An array of attributes with the plain text class
300
+     */
301
+    public function getPlainTextClasses()
302
+    {
303
+        return 'form-control-plaintext';
304
+    }
305
+
306
+    /**
307
+     * Add form class
308
+     *
309
+     * @param  string $type The type of form to add
310
+     *
311
+     * @return string|null
312
+     */
313
+    public function getFormClasses($type)
314
+    {
315
+        return $type ? 'form-'.$type : null;
316
+    }
317
+
318
+    /**
319
+     * Add actions block class
320
+     *
321
+     * @return string|null
322
+     */
323
+    public function getActionClasses()
324
+    {
325
+        if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) {
326
+            return 'mb-3 row';
327
+        }
328
+
329
+        return null;
330
+    }
331
+
332
+    /**
333
+     * Add floating label class
334
+     *
335
+     * @return string Get the floating label class
336
+     */
337
+    public function getFloatingLabelClass()
338
+    {
339
+        return 'form-floating';
340
+    }
341
+
342
+    ////////////////////////////////////////////////////////////////////
343
+    //////////////////////////// RENDER BLOCKS /////////////////////////
344
+    ////////////////////////////////////////////////////////////////////
345
+
346
+    /**
347
+     * Render an help text
348
+     *
349
+     * @param string $text
350
+     * @param array  $attributes
351
+     *
352
+     * @return Element
353
+     */
354
+    public function createHelp($text, $attributes = array())
355
+    {
356
+        return Element::create('span', $text, $attributes)->addClass('form-text');
357
+    }
358
+
359
+    /**
360
+     * Render an validation error text
361
+     *
362
+     * @param string $text
363
+     * @param array  $attributes
364
+     *
365
+     * @return string
366
+     */
367
+    public function createValidationError($text, $attributes = array())
368
+    {
369
+        return Element::create('div', $text, $attributes)->addClass('invalid-feedback');
370
+    }
371
+
372
+    /**
373
+     * Render an help text
374
+     *
375
+     * @param string $text
376
+     * @param array  $attributes
377
+     *
378
+     * @return Element
379
+     */
380
+    public function createBlockHelp($text, $attributes = array())
381
+    {
382
+        return Element::create('div', $text, $attributes)->addClass('form-text');
383
+    }
384
+
385
+    /**
386
+     * Render a disabled field
387
+     *
388
+     * @param Field $field
389
+     *
390
+     * @return Element
391
+     */
392
+    public function createDisabledField(Field $field)
393
+    {
394
+        return Element::create('span', $field->getValue(), $field->getAttributes());
395
+    }
396
+
397
+    /**
398
+     * Render a plain text field
399
+     *
400
+     * @param Field $field
401
+     *
402
+     * @return Element
403
+     */
404
+    public function createPlainTextField(Field $field)
405
+    {
406
+        $label = $field->getLabel();
407
+        if ($label) {
408
+            $label->for('');
409
+        }
410
+
411
+        return Element::create('div', $field->getValue(), $field->getAttributes());
412
+    }
413
+
414
+    ////////////////////////////////////////////////////////////////////
415
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
416
+    ////////////////////////////////////////////////////////////////////
417
+
418
+    /**
419
+     * Wrap an item to be prepended or appended to the current field
420
+     *
421
+     * @return Element A wrapped item
422
+     */
423
+    public function placeAround($item, $place = null)
424
+    {
425
+        // Render object
426
+        if (is_object($item) and method_exists($item, '__toString')) {
427
+            $item = $item->__toString();
428
+        }
429
+
430
+        $items = (array) $item;
431
+        $element = '';
432
+        foreach ($items as $item) {
433
+            $hasButtonTag = strpos(ltrim($item), '<button') === 0;
434
+
435
+            // Get class to use
436
+            $class = $hasButtonTag ? '' : 'input-group-text';
437
+
438
+            $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class);
439
+        }
440
+
441
+        return $element;
442
+    }
443
+
444
+    /**
445
+     * Wrap a field with prepended and appended items
446
+     *
447
+     * @param  Field $field
448
+     * @param  array $prepend
449
+     * @param  array $append
450
+     *
451
+     * @return string A field concatented with prepended and/or appended items
452
+     */
453
+    public function prependAppend($field, $prepend, $append)
454
+    {
455
+        $return = '<div class="input-group">';
456
+        $return .= implode('', $prepend);
457
+        $return .= $field->render();
458
+        $return .= implode('', $append);
459
+        $return .= '</div>';
460
+
461
+        return $return;
462
+    }
463
+
464
+    /**
465
+     * Wrap a field with potential additional tags
466
+     *
467
+     * @param  Field $field
468
+     *
469
+     * @return Element A wrapped field
470
+     */
471
+    public function wrapField($field)
472
+    {
473
+        if ($this->app['former.form']->isOfType('horizontal')) {
474
+            return Element::create('div', $field)->addClass($this->fieldWidth);
475
+        }
476
+
477
+        return $field;
478
+    }
479
+
480
+    /**
481
+     * Wrap actions block with potential additional tags
482
+     *
483
+     * @param  Actions $actions
484
+     *
485
+     * @return string A wrapped actions block
486
+     */
487
+    public function wrapActions($actions)
488
+    {
489
+        // For horizontal forms, we wrap the actions in a div
490
+        if ($this->app['former.form']->isOfType('horizontal')) {
491
+            return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
492
+        }
493
+
494
+        return $actions;
495
+    }
496 496
 }
Please login to merge, or discard this patch.
src/Former/Form/Fields/Switchbox.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -3,21 +3,21 @@
 block discarded – undo
3 3
 
4 4
 class Switchbox extends Checkbox
5 5
 {
6
-	////////////////////////////////////////////////////////////////////
7
-	////////////////////////// FIELD METHODS ///////////////////////////
8
-	////////////////////////////////////////////////////////////////////
6
+    ////////////////////////////////////////////////////////////////////
7
+    ////////////////////////// FIELD METHODS ///////////////////////////
8
+    ////////////////////////////////////////////////////////////////////
9 9
 
10
-	/**
11
-	 * Create a serie of switches
12
-	 */
13
-	public function switches()
14
-	{
15
-		if ($this->isGrouped()) {
16
-			// Remove any possible items added by the Populator.
17
-			$this->items = array();
18
-		}
19
-		$this->items(func_get_args());
10
+    /**
11
+     * Create a serie of switches
12
+     */
13
+    public function switches()
14
+    {
15
+        if ($this->isGrouped()) {
16
+            // Remove any possible items added by the Populator.
17
+            $this->items = array();
18
+        }
19
+        $this->items(func_get_args());
20 20
 
21
-		return $this;
22
-	}
21
+        return $this;
22
+    }
23 23
 }
Please login to merge, or discard this patch.
src/Former/MethodDispatcher.php 1 patch
Indentation   +239 added lines, -239 removed lines patch added patch discarded remove patch
@@ -12,243 +12,243 @@
 block discarded – undo
12 12
  */
13 13
 class MethodDispatcher
14 14
 {
15
-	/**
16
-	 * The IoC Container
17
-	 *
18
-	 * @var Container
19
-	 */
20
-	protected $app;
21
-
22
-	/**
23
-	 * An array of fields repositories
24
-	 *
25
-	 * @var array
26
-	 */
27
-	protected $repositories = array();
28
-
29
-	/**
30
-	 * Build a new Dispatcher
31
-	 *
32
-	 * @param Container $app
33
-	 * @param array     $repositories
34
-	 */
35
-	public function __construct(Container $app, $repositories)
36
-	{
37
-		$this->app          = $app;
38
-		$this->repositories = (array) $repositories;
39
-	}
40
-
41
-	////////////////////////////////////////////////////////////////////
42
-	///////////////////////////// REPOSITORIES /////////////////////////
43
-	////////////////////////////////////////////////////////////////////
44
-
45
-	/**
46
-	 * Add a fields repository
47
-	 *
48
-	 * @param string $repository
49
-	 *
50
-	 * @return $this
51
-	 */
52
-	public function addRepository($repository)
53
-	{
54
-		array_unshift($this->repositories, $repository);
55
-
56
-		return $this;
57
-	}
58
-
59
-	////////////////////////////////////////////////////////////////////
60
-	///////////////////////////// DISPATCHERS //////////////////////////
61
-	////////////////////////////////////////////////////////////////////
62
-
63
-	/**
64
-	 * Dispatch a call to a registered macro
65
-	 *
66
-	 * @param  string $method     The macro's name
67
-	 * @param  array  $parameters The macro's arguments
68
-	 *
69
-	 * @return mixed
70
-	 */
71
-	public function toMacros($method, $parameters)
72
-	{
73
-		if (!$this->app['former']->hasMacro($method)) {
74
-			return false;
75
-		}
76
-
77
-		// Get and format macro
78
-		$callback = $this->app['former']->getMacro($method);
79
-		if ($callback instanceof Closure) {
80
-			return call_user_func_array($callback, $parameters);
81
-		} // Cancel if the macro is invalid
82
-		elseif (!is_string($callback)) {
83
-			return false;
84
-		}
85
-
86
-		// Get class and method
87
-		list($class, $method) = explode('@', $callback);
88
-		$this->app->instance('Illuminate\Container\Container', $this->app);
89
-
90
-		return call_user_func_array(array($this->app->make($class), $method), $parameters);
91
-	}
92
-
93
-	/**
94
-	 * Dispatch a call over to Elements
95
-	 *
96
-	 * @param string $method     The method called
97
-	 * @param array  $parameters Its parameters
98
-	 *
99
-	 * @return string
100
-	 */
101
-	public function toElements($method, $parameters)
102
-	{
103
-		// Disregards if the method isn't an element
104
-		if (!method_exists($elements = new Form\Elements($this->app, $this->app['session']), $method)) {
105
-			return false;
106
-		}
107
-
108
-		return call_user_func_array(array($elements, $method), $parameters);
109
-	}
110
-
111
-	/**
112
-	 * Dispatch a call over to Form
113
-	 *
114
-	 * @param string $method     The method called
115
-	 * @param array  $parameters Its parameters
116
-	 *
117
-	 * @return Form
118
-	 */
119
-	public function toForm($method, $parameters)
120
-	{
121
-		// Disregards if the method doesn't contain 'open'
122
-		if (!Str::contains($method, 'open') and !Str::contains($method, 'Open')) {
123
-			return false;
124
-		}
125
-
126
-		$form = new Form\Form($this->app, $this->app['url'], $this->app['former.populator']);
127
-
128
-		return $form->openForm($method, $parameters);
129
-	}
130
-
131
-	/**
132
-	 * Dispatch a call over to Group
133
-	 *
134
-	 * @param string $method     The method called
135
-	 * @param array  $parameters Its parameters
136
-	 *
137
-	 * @return Group
138
-	 */
139
-	public function toGroup($method, $parameters)
140
-	{
141
-		// Disregards if the method isn't "group"
142
-		if ($method != 'group') {
143
-			return false;
144
-		}
145
-
146
-		// Create opener
147
-		$group = new Form\Group(
148
-			$this->app,
149
-			Arr::get($parameters, 0, null),
150
-			Arr::get($parameters, 1, null)
151
-		);
152
-
153
-		// Set custom group as true
154
-		Form\Group::$opened = true;
155
-
156
-		// Set custom group reference
157
-		Form\Group::$openGroup = $group;
158
-
159
-		return $group;
160
-	}
161
-
162
-	/**
163
-	 * Dispatch a call over to Actions
164
-	 *
165
-	 * @param string $method     The method called
166
-	 * @param array  $parameters Its parameters
167
-	 *
168
-	 * @return Actions
169
-	 */
170
-	public function toActions($method, $parameters)
171
-	{
172
-		if ($method != 'actions') {
173
-			return false;
174
-		}
175
-
176
-		return new Form\Actions($this->app, $parameters);
177
-	}
178
-
179
-	/**
180
-	 * Dispatch a call over to the Fields
181
-	 *
182
-	 * @param string $method     The method called
183
-	 * @param array  $parameters Its parameters
184
-	 *
185
-	 * @return Field
186
-	 */
187
-	public function toFields($method, $parameters)
188
-	{
189
-		// Listing parameters
190
-		$class = $this->getClassFromMethod($method);
191
-		$field = new $class(
192
-			$this->app,
193
-			$method,
194
-			Arr::get($parameters, 0),
195
-			Arr::get($parameters, 1),
196
-			Arr::get($parameters, 2),
197
-			Arr::get($parameters, 3),
198
-			Arr::get($parameters, 4),
199
-			Arr::get($parameters, 5)
200
-		);
201
-
202
-		return $field;
203
-	}
204
-
205
-	////////////////////////////////////////////////////////////////////
206
-	///////////////////////////// HELPERS //////////////////////////////
207
-	////////////////////////////////////////////////////////////////////
208
-
209
-	/**
210
-	 * Get the correct class to call according to the created field
211
-	 *
212
-	 * @param  string $method The field created
213
-	 *
214
-	 * @return string The correct class
215
-	 */
216
-	protected function getClassFromMethod($method)
217
-	{
218
-		// If the field's name directly match a class, call it
219
-		$class        = Str::singular(Str::title($method));
220
-		$studly_class = Str::singular(Str::studly($method));
221
-		foreach ($this->repositories as $repository) {
222
-			if (class_exists($repository.$studly_class)) {
223
-				return $repository.$studly_class;
224
-			} else {
225
-				if (class_exists($repository.$class)) {
226
-					return $repository.$class;
227
-				}
228
-			}
229
-		}
230
-
231
-		// Else convert known fields to their classes
232
-		switch ($method) {
233
-			case 'switch':
234
-			case 'switches':
235
-				$class = Former::FIELDSPACE.'Switchbox';
236
-				break;
237
-			case 'submit':
238
-			case 'link':
239
-			case 'reset':
240
-				$class = Former::FIELDSPACE.'Button';
241
-				break;
242
-
243
-			case 'multiselect':
244
-				$class = Former::FIELDSPACE.'Select';
245
-				break;
246
-
247
-			default:
248
-				$class = Former::FIELDSPACE.'Input';
249
-				break;
250
-		}
251
-
252
-		return $class;
253
-	}
15
+    /**
16
+     * The IoC Container
17
+     *
18
+     * @var Container
19
+     */
20
+    protected $app;
21
+
22
+    /**
23
+     * An array of fields repositories
24
+     *
25
+     * @var array
26
+     */
27
+    protected $repositories = array();
28
+
29
+    /**
30
+     * Build a new Dispatcher
31
+     *
32
+     * @param Container $app
33
+     * @param array     $repositories
34
+     */
35
+    public function __construct(Container $app, $repositories)
36
+    {
37
+        $this->app          = $app;
38
+        $this->repositories = (array) $repositories;
39
+    }
40
+
41
+    ////////////////////////////////////////////////////////////////////
42
+    ///////////////////////////// REPOSITORIES /////////////////////////
43
+    ////////////////////////////////////////////////////////////////////
44
+
45
+    /**
46
+     * Add a fields repository
47
+     *
48
+     * @param string $repository
49
+     *
50
+     * @return $this
51
+     */
52
+    public function addRepository($repository)
53
+    {
54
+        array_unshift($this->repositories, $repository);
55
+
56
+        return $this;
57
+    }
58
+
59
+    ////////////////////////////////////////////////////////////////////
60
+    ///////////////////////////// DISPATCHERS //////////////////////////
61
+    ////////////////////////////////////////////////////////////////////
62
+
63
+    /**
64
+     * Dispatch a call to a registered macro
65
+     *
66
+     * @param  string $method     The macro's name
67
+     * @param  array  $parameters The macro's arguments
68
+     *
69
+     * @return mixed
70
+     */
71
+    public function toMacros($method, $parameters)
72
+    {
73
+        if (!$this->app['former']->hasMacro($method)) {
74
+            return false;
75
+        }
76
+
77
+        // Get and format macro
78
+        $callback = $this->app['former']->getMacro($method);
79
+        if ($callback instanceof Closure) {
80
+            return call_user_func_array($callback, $parameters);
81
+        } // Cancel if the macro is invalid
82
+        elseif (!is_string($callback)) {
83
+            return false;
84
+        }
85
+
86
+        // Get class and method
87
+        list($class, $method) = explode('@', $callback);
88
+        $this->app->instance('Illuminate\Container\Container', $this->app);
89
+
90
+        return call_user_func_array(array($this->app->make($class), $method), $parameters);
91
+    }
92
+
93
+    /**
94
+     * Dispatch a call over to Elements
95
+     *
96
+     * @param string $method     The method called
97
+     * @param array  $parameters Its parameters
98
+     *
99
+     * @return string
100
+     */
101
+    public function toElements($method, $parameters)
102
+    {
103
+        // Disregards if the method isn't an element
104
+        if (!method_exists($elements = new Form\Elements($this->app, $this->app['session']), $method)) {
105
+            return false;
106
+        }
107
+
108
+        return call_user_func_array(array($elements, $method), $parameters);
109
+    }
110
+
111
+    /**
112
+     * Dispatch a call over to Form
113
+     *
114
+     * @param string $method     The method called
115
+     * @param array  $parameters Its parameters
116
+     *
117
+     * @return Form
118
+     */
119
+    public function toForm($method, $parameters)
120
+    {
121
+        // Disregards if the method doesn't contain 'open'
122
+        if (!Str::contains($method, 'open') and !Str::contains($method, 'Open')) {
123
+            return false;
124
+        }
125
+
126
+        $form = new Form\Form($this->app, $this->app['url'], $this->app['former.populator']);
127
+
128
+        return $form->openForm($method, $parameters);
129
+    }
130
+
131
+    /**
132
+     * Dispatch a call over to Group
133
+     *
134
+     * @param string $method     The method called
135
+     * @param array  $parameters Its parameters
136
+     *
137
+     * @return Group
138
+     */
139
+    public function toGroup($method, $parameters)
140
+    {
141
+        // Disregards if the method isn't "group"
142
+        if ($method != 'group') {
143
+            return false;
144
+        }
145
+
146
+        // Create opener
147
+        $group = new Form\Group(
148
+            $this->app,
149
+            Arr::get($parameters, 0, null),
150
+            Arr::get($parameters, 1, null)
151
+        );
152
+
153
+        // Set custom group as true
154
+        Form\Group::$opened = true;
155
+
156
+        // Set custom group reference
157
+        Form\Group::$openGroup = $group;
158
+
159
+        return $group;
160
+    }
161
+
162
+    /**
163
+     * Dispatch a call over to Actions
164
+     *
165
+     * @param string $method     The method called
166
+     * @param array  $parameters Its parameters
167
+     *
168
+     * @return Actions
169
+     */
170
+    public function toActions($method, $parameters)
171
+    {
172
+        if ($method != 'actions') {
173
+            return false;
174
+        }
175
+
176
+        return new Form\Actions($this->app, $parameters);
177
+    }
178
+
179
+    /**
180
+     * Dispatch a call over to the Fields
181
+     *
182
+     * @param string $method     The method called
183
+     * @param array  $parameters Its parameters
184
+     *
185
+     * @return Field
186
+     */
187
+    public function toFields($method, $parameters)
188
+    {
189
+        // Listing parameters
190
+        $class = $this->getClassFromMethod($method);
191
+        $field = new $class(
192
+            $this->app,
193
+            $method,
194
+            Arr::get($parameters, 0),
195
+            Arr::get($parameters, 1),
196
+            Arr::get($parameters, 2),
197
+            Arr::get($parameters, 3),
198
+            Arr::get($parameters, 4),
199
+            Arr::get($parameters, 5)
200
+        );
201
+
202
+        return $field;
203
+    }
204
+
205
+    ////////////////////////////////////////////////////////////////////
206
+    ///////////////////////////// HELPERS //////////////////////////////
207
+    ////////////////////////////////////////////////////////////////////
208
+
209
+    /**
210
+     * Get the correct class to call according to the created field
211
+     *
212
+     * @param  string $method The field created
213
+     *
214
+     * @return string The correct class
215
+     */
216
+    protected function getClassFromMethod($method)
217
+    {
218
+        // If the field's name directly match a class, call it
219
+        $class        = Str::singular(Str::title($method));
220
+        $studly_class = Str::singular(Str::studly($method));
221
+        foreach ($this->repositories as $repository) {
222
+            if (class_exists($repository.$studly_class)) {
223
+                return $repository.$studly_class;
224
+            } else {
225
+                if (class_exists($repository.$class)) {
226
+                    return $repository.$class;
227
+                }
228
+            }
229
+        }
230
+
231
+        // Else convert known fields to their classes
232
+        switch ($method) {
233
+            case 'switch':
234
+            case 'switches':
235
+                $class = Former::FIELDSPACE.'Switchbox';
236
+                break;
237
+            case 'submit':
238
+            case 'link':
239
+            case 'reset':
240
+                $class = Former::FIELDSPACE.'Button';
241
+                break;
242
+
243
+            case 'multiselect':
244
+                $class = Former::FIELDSPACE.'Select';
245
+                break;
246
+
247
+            default:
248
+                $class = Former::FIELDSPACE.'Input';
249
+                break;
250
+        }
251
+
252
+        return $class;
253
+    }
254 254
 }
Please login to merge, or discard this patch.
src/Former/Traits/Checkable.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -365,7 +365,7 @@  discard block
 block discarded – undo
365 365
 
366 366
 		// In Bootsrap 3 or 4 or 5, don't append the the checkable type (radio/checkbox) as a class if
367 367
 		// rendering inline.
368
-		$class =  in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline;
368
+		$class = in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline;
369 369
 
370 370
 		// Merge custom attributes with global attributes
371 371
 		$attributes = array_merge($this->attributes, $attributes);
@@ -405,7 +405,7 @@  discard block
 block discarded – undo
405 405
 			if ($this->app['former']->framework() === 'TwitterBootstrap5' &&
406 406
 				$this->isOfType('switch', 'switches')
407 407
 			) {
408
-				$wrapperClass.= ' form-switch';
408
+				$wrapperClass .= ' form-switch';
409 409
 			}
410 410
 			$element = Element::create('div', $element)->class($wrapperClass)->render();
411 411
 		} else {
@@ -442,7 +442,7 @@  discard block
 block discarded – undo
442 442
 		$this->app['former']->labels[] = $name;
443 443
 
444 444
 		// Count number of fields with the same ID
445
-		$where  = array_filter($this->app['former']->labels, function ($label) use ($name) {
445
+		$where  = array_filter($this->app['former']->labels, function($label) use ($name) {
446 446
 			return $label == $name;
447 447
 		});
448 448
 		$unique = sizeof($where);
Please login to merge, or discard this patch.
Indentation   +573 added lines, -573 removed lines patch added patch discarded remove patch
@@ -14,577 +14,577 @@
 block discarded – undo
14 14
  */
15 15
 abstract class Checkable extends Field
16 16
 {
17
-	/**
18
-	 * Renders the checkables as inline
19
-	 *
20
-	 * @var boolean
21
-	 */
22
-	protected $inline = false;
23
-
24
-	/**
25
-	 * Add a text to a single element
26
-	 *
27
-	 * @var string
28
-	 */
29
-	protected $text = null;
30
-
31
-	/**
32
-	 * Renders the checkables as grouped
33
-	 *
34
-	 * @var boolean
35
-	 */
36
-	protected $grouped = false;
37
-
38
-	/**
39
-	 * The checkable items currently stored
40
-	 *
41
-	 * @var array
42
-	 */
43
-	protected $items = array();
44
-
45
-	/**
46
-	 * The type of checkable item
47
-	 *
48
-	 * @var string
49
-	 */
50
-	protected $checkable = null;
51
-
52
-	/**
53
-	 * An array of checked items
54
-	 *
55
-	 * @var array
56
-	 */
57
-	protected $checked = array();
58
-
59
-	/**
60
-	 * The checkable currently being focused on
61
-	 *
62
-	 * @var integer
63
-	 */
64
-	protected $focus = null;
65
-
66
-	/**
67
-	 * Whether this particular checkable is to be pushed
68
-	 *
69
-	 * @var boolean
70
-	 */
71
-	protected $isPushed = null;
72
-
73
-	////////////////////////////////////////////////////////////////////
74
-	//////////////////////////// CORE METHODS //////////////////////////
75
-	////////////////////////////////////////////////////////////////////
76
-
77
-	/**
78
-	 * Build a new checkable
79
-	 *
80
-	 * @param Container $app
81
-	 * @param string    $type
82
-	 * @param array     $name
83
-	 * @param           $label
84
-	 * @param           $value
85
-	 * @param           $attributes
86
-	 */
87
-	public function __construct(Container $app, $type, $name, $label, $value, $attributes)
88
-	{
89
-		// Unify auto and chained methods of grouping checkboxes
90
-		if (Str::endsWith($name, '[]')) {
91
-			$name = substr($name, 0, -2);
92
-			$this->grouped();
93
-		}
94
-		parent::__construct($app, $type, $name, $label, $value, $attributes);
95
-
96
-		if (is_array($this->value)) {
97
-			$this->items($this->value);
98
-		}
99
-	}
100
-
101
-	/**
102
-	 * Apply methods to focused checkable
103
-	 *
104
-	 * @param string $method
105
-	 * @param array  $parameters
106
-	 *
107
-	 * @return $this
108
-	 */
109
-	public function __call($method, $parameters)
110
-	{
111
-		$focused = $this->setOnFocused('attributes.'.$method, Arr::get($parameters, 0));
112
-		if ($focused) {
113
-			return $this;
114
-		}
115
-
116
-		return parent::__call($method, $parameters);
117
-	}
118
-
119
-	/**
120
-	 * Prints out the currently stored checkables
121
-	 */
122
-	public function render()
123
-	{
124
-		$html = null;
125
-
126
-		$this->setFieldClasses();
127
-
128
-		// Multiple items
129
-		if ($this->items) {
130
-			unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]);
131
-			foreach ($this->items as $key => $item) {
132
-				$value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key;
133
-				$html .= $this->createCheckable($item, $value);
134
-			}
135
-
136
-			return $html;
137
-		}
138
-
139
-		// Single item
140
-		return $this->createCheckable(array(
141
-			'name'  => $this->name,
142
-			'label' => $this->text,
143
-			'value' => $this->value,
144
-			'attributes' => $this->attributes,
145
-		));
146
-	}
147
-
148
-	////////////////////////////////////////////////////////////////////
149
-	////////////////////////// FIELD METHODS ///////////////////////////
150
-	////////////////////////////////////////////////////////////////////
151
-
152
-	/**
153
-	 * Focus on a particular checkable
154
-	 *
155
-	 * @param integer $on The checkable to focus on
156
-	 *
157
-	 * @return $this
158
-	 */
159
-	public function on($on)
160
-	{
161
-		if (!isset($this->items[$on])) {
162
-			return $this;
163
-		} else {
164
-			$this->focus = $on;
165
-		}
166
-
167
-		return $this;
168
-	}
169
-
170
-	/**
171
-	 * Set the checkables as inline
172
-	 */
173
-	public function inline($isInline = true)
174
-	{
175
-		$this->inline = $isInline;
176
-
177
-		return $this;
178
-	}
179
-
180
-	/**
181
-	 * Set the checkables as stacked
182
-	 */
183
-	public function stacked($isStacked = true)
184
-	{
185
-		$this->inline = !$isStacked;
186
-
187
-		return $this;
188
-	}
189
-
190
-	/**
191
-	 * Set the checkables as grouped
192
-	 */
193
-	public function grouped($isGrouped = true)
194
-	{
195
-		$this->grouped = $isGrouped;
196
-
197
-		return $this;
198
-	}
199
-
200
-	/**
201
-	 * Add text to a single checkable
202
-	 *
203
-	 * @param  string $text The checkable label
204
-	 *
205
-	 * @return $this
206
-	 */
207
-	public function text($text)
208
-	{
209
-		// Translate and format
210
-		$text = Helpers::translate($text);
211
-
212
-		// Apply on focused if any
213
-		$focused = $this->setOnFocused('label', $text);
214
-		if ($focused) {
215
-			return $this;
216
-		}
217
-
218
-		$this->text = $text;
219
-
220
-		return $this;
221
-	}
222
-
223
-	/**
224
-	 * Push this particular checkbox
225
-	 *
226
-	 * @param boolean $pushed
227
-	 *
228
-	 * @return $this
229
-	 */
230
-	public function push($pushed = true)
231
-	{
232
-		$this->isPushed = $pushed;
233
-
234
-		return $this;
235
-	}
236
-
237
-	/**
238
-	 * Check a specific item
239
-	 *
240
-	 * @param bool|string $checked The checkable to check, or an array of checked items
241
-	 *
242
-	 * @return $this
243
-	 */
244
-	public function check($checked = true)
245
-	{
246
-		// If we're setting all the checked items at once
247
-		if (is_array($checked)) {
248
-			$this->checked = $checked;
249
-			// Checking an item in particular
250
-		} elseif (is_string($checked) or is_int($checked)) {
251
-			$this->checked[$checked] = true;
252
-			// Only setting a single item
253
-		} else {
254
-			$this->checked[$this->name] = (bool) $checked;
255
-		}
256
-
257
-		return $this;
258
-	}
259
-
260
-
261
-	/**
262
-	 * Check if the checkables are inline
263
-	 *
264
-	 * @return boolean
265
-	 */
266
-	public function isInline()
267
-	{
268
-		return $this->inline;
269
-	}
270
-
271
-	////////////////////////////////////////////////////////////////////
272
-	////////////////////////// INTERNAL METHODS ////////////////////////
273
-	////////////////////////////////////////////////////////////////////
274
-
275
-	/**
276
-	 * Creates a series of checkable items
277
-	 *
278
-	 * @param array $_items Items to create
279
-	 */
280
-	protected function items($_items)
281
-	{
282
-		// If passing an array
283
-		if (sizeof($_items) == 1 and
284
-			isset($_items[0]) and
285
-			is_array($_items[0])
286
-		) {
287
-			$_items = $_items[0];
288
-		}
289
-
290
-		// Fetch models if that's what we were passed
291
-		if (isset($_items[0]) and is_object($_items[0])) {
292
-			$_items = Helpers::queryToArray($_items);
293
-			$_items = array_flip($_items);
294
-		}
295
-
296
-		// Iterate through items, assign a name and a label to each
297
-		$count = 0;
298
-		foreach ($_items as $label => $name) {
299
-
300
-			// Define a fallback name in case none is found
301
-			$fallback = $this->isCheckbox()
302
-				? $this->name.'_'.$count
303
-				: $this->name;
304
-
305
-			// Grouped fields
306
-			if ($this->isGrouped()) {
307
-				$attributes['id'] = str_replace('[]', '', $fallback);
308
-				$fallback         = str_replace('[]', '', $this->name).'[]';
309
-			}
310
-
311
-			// If we haven't any name defined for the checkable, try to compute some
312
-			if (!is_string($label) and !is_array($name)) {
313
-				$label = $name;
314
-				$name  = $fallback;
315
-			}
316
-
317
-			// If we gave custom information on the item, add them
318
-			if (is_array($name)) {
319
-				$attributes = $name;
320
-				$name       = Arr::get($attributes, 'name', $fallback);
321
-				unset($attributes['name']);
322
-			}
323
-
324
-			// Store all informations we have in an array
325
-			$item = array(
326
-				'name'  => $name,
327
-				'label' => Helpers::translate($label),
328
-				'count' => $count,
329
-			);
330
-			if (isset($attributes)) {
331
-				$item['attributes'] = $attributes;
332
-			}
333
-
334
-			$this->items[] = $item;
335
-			$count++;
336
-		}
337
-	}
338
-
339
-	/**
340
-	 * Renders a checkable
341
-	 *
342
-	 * @param string|array $item          A checkable item
343
-	 * @param integer      $fallbackValue A fallback value if none is set
344
-	 *
345
-	 * @return string
346
-	 */
347
-	protected function createCheckable($item, $fallbackValue = 1)
348
-	{
349
-		// Extract informations
350
-		extract($item);
351
-
352
-		// Set default values
353
-		if (!isset($attributes)) {
354
-			$attributes = array();
355
-		}
356
-		if (isset($attributes['value'])) {
357
-			$value = $attributes['value'];
358
-		}
359
-		if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) {
360
-			$value = $fallbackValue;
361
-		}
362
-
363
-		// If inline items, add class
364
-		$isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : '';
365
-
366
-		// In Bootsrap 3 or 4 or 5, don't append the the checkable type (radio/checkbox) as a class if
367
-		// rendering inline.
368
-		$class =  in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline;
369
-
370
-		// Merge custom attributes with global attributes
371
-		$attributes = array_merge($this->attributes, $attributes);
372
-		if (!isset($attributes['id'])) {
373
-			$attributes['id'] = $name.$this->unique($name);
374
-		}
375
-
376
-		// Create field
377
-		$field = Input::create($this->checkable, $name, Helpers::encode($value), $attributes);
378
-		if ($this->isChecked($item, $value)) {
379
-			$field->checked('checked');
380
-		}
381
-
382
-		// Add hidden checkbox if requested
383
-		if ($this->isOfType('checkbox', 'checkboxes', 'switch', 'switches')) {
384
-			if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) {
385
-				$field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render();
386
-
387
-				// app['former.field'] was overwritten by Former::hidden() call in the line above, so here
388
-				// we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object
389
-				$this->app->instance('former.field', $this);
390
-			}
391
-		}
392
-
393
-		// If no label to wrap, return plain checkable
394
-		if (!$label && !$this->isOfType('switch', 'switches')) {
395
-			$element = (is_object($field)) ? $field->render() : $field;
396
-		} elseif (in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])) {
397
-			$element = $field;
398
-			if ($label) {
399
-				// Revised for Bootstrap 4 and 5, move the 'input' outside of the 'label'
400
-				$labelClass = 'form-check-label';
401
-				$element .= Element::create('label', $label)->for($attributes['id'])->class($labelClass)->render();
402
-			}
403
-
404
-			$wrapperClass = $this->inline ? 'form-check form-check-inline' : 'form-check';
405
-			if ($this->app['former']->framework() === 'TwitterBootstrap5' &&
406
-				$this->isOfType('switch', 'switches')
407
-			) {
408
-				$wrapperClass.= ' form-switch';
409
-			}
410
-			$element = Element::create('div', $element)->class($wrapperClass)->render();
411
-		} else {
412
-			// Original way is to add the 'input' inside the 'label'
413
-			$element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render();
414
-		}
415
-
416
-		// If BS3, if checkables are stacked, wrap them in a div with the checkable type
417
-		if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') {
418
-			$wrapper = Element::create('div', $element)->class($this->checkable);
419
-			if ($this->getAttribute('disabled')) {
420
-				$wrapper->addClass('disabled');
421
-			}
422
-			$element = $wrapper->render();
423
-		}
424
-
425
-		// Return the field
426
-		return $element;
427
-	}
428
-
429
-	////////////////////////////////////////////////////////////////////
430
-	///////////////////////////// HELPERS //////////////////////////////
431
-	////////////////////////////////////////////////////////////////////
432
-
433
-	/**
434
-	 * Generate an unique ID for a field
435
-	 *
436
-	 * @param string $name The field's name
437
-	 *
438
-	 * @return string A field number to use
439
-	 */
440
-	protected function unique($name)
441
-	{
442
-		$this->app['former']->labels[] = $name;
443
-
444
-		// Count number of fields with the same ID
445
-		$where  = array_filter($this->app['former']->labels, function ($label) use ($name) {
446
-			return $label == $name;
447
-		});
448
-		$unique = sizeof($where);
449
-
450
-		// In case the field doesn't need to be numbered
451
-		if ($unique < 2 or empty($this->items)) {
452
-			return false;
453
-		}
454
-
455
-		return $unique;
456
-	}
457
-
458
-	/**
459
-	 * Set something on the currently focused checkable
460
-	 *
461
-	 * @param string $attribute The key to set
462
-	 * @param string $value     Its value
463
-	 *
464
-	 * @return $this|bool
465
-	 */
466
-	protected function setOnFocused($attribute, $value)
467
-	{
468
-		if (is_null($this->focus)) {
469
-			return false;
470
-		}
471
-
472
-		$this->items[$this->focus] = Arr::set($this->items[$this->focus], $attribute, $value);
473
-
474
-		return $this;
475
-	}
476
-
477
-	/**
478
-	 * Check if a checkable is checked
479
-	 *
480
-	 * @return boolean Checked or not
481
-	 */
482
-	protected function isChecked($item = null, $value = null)
483
-	{
484
-		if (isset($item['name'])) {
485
-			$name = $item['name'];
486
-		}
487
-		if (empty($name)) {
488
-			$name = $this->name;
489
-		}
490
-
491
-		// If it's a checkbox, see if we marqued that one as checked in the array
492
-		// Or if it's a single radio, simply see if we called check
493
-		if ($this->isCheckbox() or
494
-			!$this->isCheckbox() and !$this->items
495
-		) {
496
-			$checked = Arr::get($this->checked, $name, false);
497
-
498
-			// If there are multiple, search for the value
499
-			// as the name are the same between radios
500
-		} else {
501
-			$checked = Arr::get($this->checked, $value, false);
502
-		}
503
-
504
-		// Check the values and POST array
505
-		if ($this->isGrouped()) {
506
-			// The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[])
507
-			$groupIndex = self::getGroupIndexFromItem($item);
508
-
509
-			// Search using the bare name, not the individual item name
510
-			$post   = $this->app['former']->getPost($this->name);
511
-			$static = $this->app['former']->getValue($this->bind ?: $this->name);
512
-
513
-			if (isset($post[$groupIndex])) {
514
-				$post = $post[$groupIndex];
515
-			}
516
-
517
-			/**
518
-			 * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must
519
-			 * match the value in order for the checkbox to be considered checked, e.g.:
520
-			 *
521
-			 *  array(
522
-			 *    'name' = 'roles[foo]',
523
-			 *    'value' => 'foo',
524
-			 *  )
525
-			 */
526
-			if ($static instanceof Collection) {
527
-				// If the repopulate value is a collection, search for an item matching the $groupIndex
528
-				foreach ($static as $staticItem) {
529
-					$staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem;
530
-					if ($staticItemValue == $groupIndex) {
531
-						$static = $staticItemValue;
532
-						break;
533
-					}
534
-				}
535
-			} else if (isset($static[$groupIndex])) {
536
-				$static = $static[$groupIndex];
537
-			}
538
-		} else {
539
-			$post   = $this->app['former']->getPost($name);
540
-			$static = $this->app['former']->getValue($this->bind ?: $name);
541
-		}
542
-
543
-		if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) {
544
-			$isChecked = ($post == $value);
545
-		} elseif (!is_null($static)) {
546
-			$isChecked = ($static == $value);
547
-		} else {
548
-			$isChecked = $checked;
549
-		}
550
-
551
-		return $isChecked ? true : false;
552
-	}
553
-
554
-	/**
555
-	 * Check if the current element is a checkbox
556
-	 *
557
-	 * @return boolean Checkbox or radio
558
-	 */
559
-	protected function isCheckbox()
560
-	{
561
-		return $this->checkable == 'checkbox';
562
-	}
563
-
564
-	/**
565
-	 * Check if the checkables are grouped or not
566
-	 *
567
-	 * @return boolean
568
-	 */
569
-	protected function isGrouped()
570
-	{
571
-		return
572
-			$this->grouped == true or
573
-			strpos($this->name, '[]') !== false;
574
-	}
575
-
576
-	/**
577
-	 * @param array $item The item array, containing at least name and count keys.
578
-	 *
579
-	 * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[])
580
-	 */
581
-	public static function getGroupIndexFromItem($item)
582
-	{
583
-		$groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']);
584
-		if (empty($groupIndex) or $groupIndex == $item['name']) {
585
-			return $item['count'];
586
-		}
587
-
588
-		return $groupIndex;
589
-	}
17
+    /**
18
+     * Renders the checkables as inline
19
+     *
20
+     * @var boolean
21
+     */
22
+    protected $inline = false;
23
+
24
+    /**
25
+     * Add a text to a single element
26
+     *
27
+     * @var string
28
+     */
29
+    protected $text = null;
30
+
31
+    /**
32
+     * Renders the checkables as grouped
33
+     *
34
+     * @var boolean
35
+     */
36
+    protected $grouped = false;
37
+
38
+    /**
39
+     * The checkable items currently stored
40
+     *
41
+     * @var array
42
+     */
43
+    protected $items = array();
44
+
45
+    /**
46
+     * The type of checkable item
47
+     *
48
+     * @var string
49
+     */
50
+    protected $checkable = null;
51
+
52
+    /**
53
+     * An array of checked items
54
+     *
55
+     * @var array
56
+     */
57
+    protected $checked = array();
58
+
59
+    /**
60
+     * The checkable currently being focused on
61
+     *
62
+     * @var integer
63
+     */
64
+    protected $focus = null;
65
+
66
+    /**
67
+     * Whether this particular checkable is to be pushed
68
+     *
69
+     * @var boolean
70
+     */
71
+    protected $isPushed = null;
72
+
73
+    ////////////////////////////////////////////////////////////////////
74
+    //////////////////////////// CORE METHODS //////////////////////////
75
+    ////////////////////////////////////////////////////////////////////
76
+
77
+    /**
78
+     * Build a new checkable
79
+     *
80
+     * @param Container $app
81
+     * @param string    $type
82
+     * @param array     $name
83
+     * @param           $label
84
+     * @param           $value
85
+     * @param           $attributes
86
+     */
87
+    public function __construct(Container $app, $type, $name, $label, $value, $attributes)
88
+    {
89
+        // Unify auto and chained methods of grouping checkboxes
90
+        if (Str::endsWith($name, '[]')) {
91
+            $name = substr($name, 0, -2);
92
+            $this->grouped();
93
+        }
94
+        parent::__construct($app, $type, $name, $label, $value, $attributes);
95
+
96
+        if (is_array($this->value)) {
97
+            $this->items($this->value);
98
+        }
99
+    }
100
+
101
+    /**
102
+     * Apply methods to focused checkable
103
+     *
104
+     * @param string $method
105
+     * @param array  $parameters
106
+     *
107
+     * @return $this
108
+     */
109
+    public function __call($method, $parameters)
110
+    {
111
+        $focused = $this->setOnFocused('attributes.'.$method, Arr::get($parameters, 0));
112
+        if ($focused) {
113
+            return $this;
114
+        }
115
+
116
+        return parent::__call($method, $parameters);
117
+    }
118
+
119
+    /**
120
+     * Prints out the currently stored checkables
121
+     */
122
+    public function render()
123
+    {
124
+        $html = null;
125
+
126
+        $this->setFieldClasses();
127
+
128
+        // Multiple items
129
+        if ($this->items) {
130
+            unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]);
131
+            foreach ($this->items as $key => $item) {
132
+                $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key;
133
+                $html .= $this->createCheckable($item, $value);
134
+            }
135
+
136
+            return $html;
137
+        }
138
+
139
+        // Single item
140
+        return $this->createCheckable(array(
141
+            'name'  => $this->name,
142
+            'label' => $this->text,
143
+            'value' => $this->value,
144
+            'attributes' => $this->attributes,
145
+        ));
146
+    }
147
+
148
+    ////////////////////////////////////////////////////////////////////
149
+    ////////////////////////// FIELD METHODS ///////////////////////////
150
+    ////////////////////////////////////////////////////////////////////
151
+
152
+    /**
153
+     * Focus on a particular checkable
154
+     *
155
+     * @param integer $on The checkable to focus on
156
+     *
157
+     * @return $this
158
+     */
159
+    public function on($on)
160
+    {
161
+        if (!isset($this->items[$on])) {
162
+            return $this;
163
+        } else {
164
+            $this->focus = $on;
165
+        }
166
+
167
+        return $this;
168
+    }
169
+
170
+    /**
171
+     * Set the checkables as inline
172
+     */
173
+    public function inline($isInline = true)
174
+    {
175
+        $this->inline = $isInline;
176
+
177
+        return $this;
178
+    }
179
+
180
+    /**
181
+     * Set the checkables as stacked
182
+     */
183
+    public function stacked($isStacked = true)
184
+    {
185
+        $this->inline = !$isStacked;
186
+
187
+        return $this;
188
+    }
189
+
190
+    /**
191
+     * Set the checkables as grouped
192
+     */
193
+    public function grouped($isGrouped = true)
194
+    {
195
+        $this->grouped = $isGrouped;
196
+
197
+        return $this;
198
+    }
199
+
200
+    /**
201
+     * Add text to a single checkable
202
+     *
203
+     * @param  string $text The checkable label
204
+     *
205
+     * @return $this
206
+     */
207
+    public function text($text)
208
+    {
209
+        // Translate and format
210
+        $text = Helpers::translate($text);
211
+
212
+        // Apply on focused if any
213
+        $focused = $this->setOnFocused('label', $text);
214
+        if ($focused) {
215
+            return $this;
216
+        }
217
+
218
+        $this->text = $text;
219
+
220
+        return $this;
221
+    }
222
+
223
+    /**
224
+     * Push this particular checkbox
225
+     *
226
+     * @param boolean $pushed
227
+     *
228
+     * @return $this
229
+     */
230
+    public function push($pushed = true)
231
+    {
232
+        $this->isPushed = $pushed;
233
+
234
+        return $this;
235
+    }
236
+
237
+    /**
238
+     * Check a specific item
239
+     *
240
+     * @param bool|string $checked The checkable to check, or an array of checked items
241
+     *
242
+     * @return $this
243
+     */
244
+    public function check($checked = true)
245
+    {
246
+        // If we're setting all the checked items at once
247
+        if (is_array($checked)) {
248
+            $this->checked = $checked;
249
+            // Checking an item in particular
250
+        } elseif (is_string($checked) or is_int($checked)) {
251
+            $this->checked[$checked] = true;
252
+            // Only setting a single item
253
+        } else {
254
+            $this->checked[$this->name] = (bool) $checked;
255
+        }
256
+
257
+        return $this;
258
+    }
259
+
260
+
261
+    /**
262
+     * Check if the checkables are inline
263
+     *
264
+     * @return boolean
265
+     */
266
+    public function isInline()
267
+    {
268
+        return $this->inline;
269
+    }
270
+
271
+    ////////////////////////////////////////////////////////////////////
272
+    ////////////////////////// INTERNAL METHODS ////////////////////////
273
+    ////////////////////////////////////////////////////////////////////
274
+
275
+    /**
276
+     * Creates a series of checkable items
277
+     *
278
+     * @param array $_items Items to create
279
+     */
280
+    protected function items($_items)
281
+    {
282
+        // If passing an array
283
+        if (sizeof($_items) == 1 and
284
+            isset($_items[0]) and
285
+            is_array($_items[0])
286
+        ) {
287
+            $_items = $_items[0];
288
+        }
289
+
290
+        // Fetch models if that's what we were passed
291
+        if (isset($_items[0]) and is_object($_items[0])) {
292
+            $_items = Helpers::queryToArray($_items);
293
+            $_items = array_flip($_items);
294
+        }
295
+
296
+        // Iterate through items, assign a name and a label to each
297
+        $count = 0;
298
+        foreach ($_items as $label => $name) {
299
+
300
+            // Define a fallback name in case none is found
301
+            $fallback = $this->isCheckbox()
302
+                ? $this->name.'_'.$count
303
+                : $this->name;
304
+
305
+            // Grouped fields
306
+            if ($this->isGrouped()) {
307
+                $attributes['id'] = str_replace('[]', '', $fallback);
308
+                $fallback         = str_replace('[]', '', $this->name).'[]';
309
+            }
310
+
311
+            // If we haven't any name defined for the checkable, try to compute some
312
+            if (!is_string($label) and !is_array($name)) {
313
+                $label = $name;
314
+                $name  = $fallback;
315
+            }
316
+
317
+            // If we gave custom information on the item, add them
318
+            if (is_array($name)) {
319
+                $attributes = $name;
320
+                $name       = Arr::get($attributes, 'name', $fallback);
321
+                unset($attributes['name']);
322
+            }
323
+
324
+            // Store all informations we have in an array
325
+            $item = array(
326
+                'name'  => $name,
327
+                'label' => Helpers::translate($label),
328
+                'count' => $count,
329
+            );
330
+            if (isset($attributes)) {
331
+                $item['attributes'] = $attributes;
332
+            }
333
+
334
+            $this->items[] = $item;
335
+            $count++;
336
+        }
337
+    }
338
+
339
+    /**
340
+     * Renders a checkable
341
+     *
342
+     * @param string|array $item          A checkable item
343
+     * @param integer      $fallbackValue A fallback value if none is set
344
+     *
345
+     * @return string
346
+     */
347
+    protected function createCheckable($item, $fallbackValue = 1)
348
+    {
349
+        // Extract informations
350
+        extract($item);
351
+
352
+        // Set default values
353
+        if (!isset($attributes)) {
354
+            $attributes = array();
355
+        }
356
+        if (isset($attributes['value'])) {
357
+            $value = $attributes['value'];
358
+        }
359
+        if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) {
360
+            $value = $fallbackValue;
361
+        }
362
+
363
+        // If inline items, add class
364
+        $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : '';
365
+
366
+        // In Bootsrap 3 or 4 or 5, don't append the the checkable type (radio/checkbox) as a class if
367
+        // rendering inline.
368
+        $class =  in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline;
369
+
370
+        // Merge custom attributes with global attributes
371
+        $attributes = array_merge($this->attributes, $attributes);
372
+        if (!isset($attributes['id'])) {
373
+            $attributes['id'] = $name.$this->unique($name);
374
+        }
375
+
376
+        // Create field
377
+        $field = Input::create($this->checkable, $name, Helpers::encode($value), $attributes);
378
+        if ($this->isChecked($item, $value)) {
379
+            $field->checked('checked');
380
+        }
381
+
382
+        // Add hidden checkbox if requested
383
+        if ($this->isOfType('checkbox', 'checkboxes', 'switch', 'switches')) {
384
+            if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) {
385
+                $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render();
386
+
387
+                // app['former.field'] was overwritten by Former::hidden() call in the line above, so here
388
+                // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object
389
+                $this->app->instance('former.field', $this);
390
+            }
391
+        }
392
+
393
+        // If no label to wrap, return plain checkable
394
+        if (!$label && !$this->isOfType('switch', 'switches')) {
395
+            $element = (is_object($field)) ? $field->render() : $field;
396
+        } elseif (in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])) {
397
+            $element = $field;
398
+            if ($label) {
399
+                // Revised for Bootstrap 4 and 5, move the 'input' outside of the 'label'
400
+                $labelClass = 'form-check-label';
401
+                $element .= Element::create('label', $label)->for($attributes['id'])->class($labelClass)->render();
402
+            }
403
+
404
+            $wrapperClass = $this->inline ? 'form-check form-check-inline' : 'form-check';
405
+            if ($this->app['former']->framework() === 'TwitterBootstrap5' &&
406
+                $this->isOfType('switch', 'switches')
407
+            ) {
408
+                $wrapperClass.= ' form-switch';
409
+            }
410
+            $element = Element::create('div', $element)->class($wrapperClass)->render();
411
+        } else {
412
+            // Original way is to add the 'input' inside the 'label'
413
+            $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render();
414
+        }
415
+
416
+        // If BS3, if checkables are stacked, wrap them in a div with the checkable type
417
+        if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') {
418
+            $wrapper = Element::create('div', $element)->class($this->checkable);
419
+            if ($this->getAttribute('disabled')) {
420
+                $wrapper->addClass('disabled');
421
+            }
422
+            $element = $wrapper->render();
423
+        }
424
+
425
+        // Return the field
426
+        return $element;
427
+    }
428
+
429
+    ////////////////////////////////////////////////////////////////////
430
+    ///////////////////////////// HELPERS //////////////////////////////
431
+    ////////////////////////////////////////////////////////////////////
432
+
433
+    /**
434
+     * Generate an unique ID for a field
435
+     *
436
+     * @param string $name The field's name
437
+     *
438
+     * @return string A field number to use
439
+     */
440
+    protected function unique($name)
441
+    {
442
+        $this->app['former']->labels[] = $name;
443
+
444
+        // Count number of fields with the same ID
445
+        $where  = array_filter($this->app['former']->labels, function ($label) use ($name) {
446
+            return $label == $name;
447
+        });
448
+        $unique = sizeof($where);
449
+
450
+        // In case the field doesn't need to be numbered
451
+        if ($unique < 2 or empty($this->items)) {
452
+            return false;
453
+        }
454
+
455
+        return $unique;
456
+    }
457
+
458
+    /**
459
+     * Set something on the currently focused checkable
460
+     *
461
+     * @param string $attribute The key to set
462
+     * @param string $value     Its value
463
+     *
464
+     * @return $this|bool
465
+     */
466
+    protected function setOnFocused($attribute, $value)
467
+    {
468
+        if (is_null($this->focus)) {
469
+            return false;
470
+        }
471
+
472
+        $this->items[$this->focus] = Arr::set($this->items[$this->focus], $attribute, $value);
473
+
474
+        return $this;
475
+    }
476
+
477
+    /**
478
+     * Check if a checkable is checked
479
+     *
480
+     * @return boolean Checked or not
481
+     */
482
+    protected function isChecked($item = null, $value = null)
483
+    {
484
+        if (isset($item['name'])) {
485
+            $name = $item['name'];
486
+        }
487
+        if (empty($name)) {
488
+            $name = $this->name;
489
+        }
490
+
491
+        // If it's a checkbox, see if we marqued that one as checked in the array
492
+        // Or if it's a single radio, simply see if we called check
493
+        if ($this->isCheckbox() or
494
+            !$this->isCheckbox() and !$this->items
495
+        ) {
496
+            $checked = Arr::get($this->checked, $name, false);
497
+
498
+            // If there are multiple, search for the value
499
+            // as the name are the same between radios
500
+        } else {
501
+            $checked = Arr::get($this->checked, $value, false);
502
+        }
503
+
504
+        // Check the values and POST array
505
+        if ($this->isGrouped()) {
506
+            // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[])
507
+            $groupIndex = self::getGroupIndexFromItem($item);
508
+
509
+            // Search using the bare name, not the individual item name
510
+            $post   = $this->app['former']->getPost($this->name);
511
+            $static = $this->app['former']->getValue($this->bind ?: $this->name);
512
+
513
+            if (isset($post[$groupIndex])) {
514
+                $post = $post[$groupIndex];
515
+            }
516
+
517
+            /**
518
+             * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must
519
+             * match the value in order for the checkbox to be considered checked, e.g.:
520
+             *
521
+             *  array(
522
+             *    'name' = 'roles[foo]',
523
+             *    'value' => 'foo',
524
+             *  )
525
+             */
526
+            if ($static instanceof Collection) {
527
+                // If the repopulate value is a collection, search for an item matching the $groupIndex
528
+                foreach ($static as $staticItem) {
529
+                    $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem;
530
+                    if ($staticItemValue == $groupIndex) {
531
+                        $static = $staticItemValue;
532
+                        break;
533
+                    }
534
+                }
535
+            } else if (isset($static[$groupIndex])) {
536
+                $static = $static[$groupIndex];
537
+            }
538
+        } else {
539
+            $post   = $this->app['former']->getPost($name);
540
+            $static = $this->app['former']->getValue($this->bind ?: $name);
541
+        }
542
+
543
+        if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) {
544
+            $isChecked = ($post == $value);
545
+        } elseif (!is_null($static)) {
546
+            $isChecked = ($static == $value);
547
+        } else {
548
+            $isChecked = $checked;
549
+        }
550
+
551
+        return $isChecked ? true : false;
552
+    }
553
+
554
+    /**
555
+     * Check if the current element is a checkbox
556
+     *
557
+     * @return boolean Checkbox or radio
558
+     */
559
+    protected function isCheckbox()
560
+    {
561
+        return $this->checkable == 'checkbox';
562
+    }
563
+
564
+    /**
565
+     * Check if the checkables are grouped or not
566
+     *
567
+     * @return boolean
568
+     */
569
+    protected function isGrouped()
570
+    {
571
+        return
572
+            $this->grouped == true or
573
+            strpos($this->name, '[]') !== false;
574
+    }
575
+
576
+    /**
577
+     * @param array $item The item array, containing at least name and count keys.
578
+     *
579
+     * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[])
580
+     */
581
+    public static function getGroupIndexFromItem($item)
582
+    {
583
+        $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']);
584
+        if (empty($groupIndex) or $groupIndex == $item['name']) {
585
+            return $item['count'];
586
+        }
587
+
588
+        return $groupIndex;
589
+    }
590 590
 }
Please login to merge, or discard this patch.
src/Former/Form/Group.php 1 patch
Indentation   +551 added lines, -551 removed lines patch added patch discarded remove patch
@@ -14,555 +14,555 @@
 block discarded – undo
14 14
  */
15 15
 class Group extends Tag
16 16
 {
17
-	/**
18
-	 * The Container
19
-	 *
20
-	 * @var Container
21
-	 */
22
-	protected $app;
23
-
24
-	/**
25
-	 * The current state of the group
26
-	 *
27
-	 * @var string
28
-	 */
29
-	protected $state = null;
30
-
31
-	/**
32
-	 * Whether the field should be displayed raw or not
33
-	 *
34
-	 * @var boolean
35
-	 */
36
-	protected $raw = false;
37
-
38
-	/**
39
-	 * The group label
40
-	 *
41
-	 * @var Element
42
-	 */
43
-	protected $label;
44
-
45
-	/**
46
-	 * The group help
47
-	 *
48
-	 * @var array
49
-	 */
50
-	protected $help = array();
51
-
52
-	/**
53
-	 * An array of elements to preprend the field
54
-	 *
55
-	 * @var array
56
-	 */
57
-	protected $prepend = array();
58
-
59
-	/**
60
-	 * An array of elements to append the field
61
-	 *
62
-	 * @var array
63
-	 */
64
-	protected $append = array();
65
-
66
-	/**
67
-	 * The field validations to be checked for errors
68
-	 *
69
-	 * @var array
70
-	 */
71
-	protected $validations = array();
72
-
73
-	/**
74
-	 * The group's element
75
-	 *
76
-	 * @var string
77
-	 */
78
-	protected $element = 'div';
79
-
80
-	/**
81
-	 * Whether a custom group is opened or not
82
-	 *
83
-	 * @var boolean
84
-	 */
85
-	public static $opened = false;
86
-
87
-	/**
88
-	 * The custom group that is open
89
-	 *
90
-	 * @var Former\Form\Group
91
-	 */
92
-	public static $openGroup = null;
93
-
94
-	////////////////////////////////////////////////////////////////////
95
-	/////////////////////////// CORE METHODS ///////////////////////////
96
-	////////////////////////////////////////////////////////////////////
97
-
98
-	/**
99
-	 * Creates a group
100
-	 *
101
-	 * @param string $label Its label
102
-	 */
103
-	public function __construct(Container $app, $label, $validations = null)
104
-	{
105
-		// Get special classes
106
-		$this->app = $app;
107
-		$this->addClass($this->app['former.framework']->getGroupClasses());
108
-
109
-		// Invisible if Nude
110
-		if ($this->app['former.framework']->is('Nude')) {
111
-			$this->element = '';
112
-		}
113
-
114
-		// Set group label
115
-		if ($label) {
116
-			$this->setLabel($label);
117
-		}
118
-
119
-		// Set validations used to override groups own conclusions
120
-		$this->validations = (array) $validations;
121
-	}
122
-
123
-	/**
124
-	 * Prints out the opening of the Control Group
125
-	 *
126
-	 * @return string A control group opening tag
127
-	 */
128
-	public function __toString()
129
-	{
130
-		return $this->open().$this->getFormattedLabel();
131
-	}
132
-
133
-	/**
134
-	 * Opens a group
135
-	 *
136
-	 * @return string Opening tag
137
-	 */
138
-	public function open()
139
-	{
140
-		if ($this->getErrors()) {
141
-			$this->state($this->app['former.framework']->errorState());
142
-		}
143
-
144
-		// Retrieve state and append it to classes
145
-		if ($this->state) {
146
-			$this->addClass($this->state);
147
-		}
148
-
149
-		// Required state
150
-		if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) {
151
-			$this->addClass($this->app['former']->getOption('required_class'));
152
-		}
153
-
154
-		return parent::open();
155
-	}
156
-
157
-	/**
158
-	 * Set the contents of the current group
159
-	 *
160
-	 * @param string $contents The group contents
161
-	 *
162
-	 * @return string A group
163
-	 */
164
-	public function contents($contents)
165
-	{
166
-		return $this->wrap($contents, $this->getFormattedLabel());
167
-	}
168
-
169
-	/**
170
-	 * Wrap a Field with the current group
171
-	 *
172
-	 * @param  \Former\Traits\Field $field A Field instance
173
-	 *
174
-	 * @return string        A group
175
-	 */
176
-	public function wrapField($field)
177
-	{
178
-		$label = $this->getLabel($field);
179
-		$help = $this->getHelp();
180
-		if ($field->isCheckable() &&
181
-			in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])
182
-		) {
183
-			$wrapperClass = null;
184
-			if ($this->app['former']->framework() === 'TwitterBootstrap4') {
185
-				$wrapperClass = $field->isInline() ? 'form-check form-check-inline' : 'form-check';
186
-			}
187
-			if ($this->app['former']->getErrors($field->getName())) {
188
-				$hiddenInput = Input::create('hidden')->addClass('form-check-input is-invalid');
189
-				$help = $hiddenInput.$help;
190
-			}
191
-			$help = $help ? Element::create('div', $help)->addClass($wrapperClass) : '';
192
-		}
193
-		$withFloatingLabel = $field->withFloatingLabel();
194
-
195
-		$field = $this->prependAppend($field);
196
-		$field .= $help;
197
-
198
-		if ($withFloatingLabel &&
199
-			$this->app['former']->framework() === 'TwitterBootstrap5'
200
-		) {
201
-			return $this->wrapWithFloatingLabel($field, $label);
202
-		}
203
-
204
-		return $this->wrap($field, $label);
205
-	}
206
-
207
-	////////////////////////////////////////////////////////////////////
208
-	//////////////////////////// FIELD METHODS /////////////////////////
209
-	////////////////////////////////////////////////////////////////////
210
-
211
-	/**
212
-	 * Set the state of the group
213
-	 *
214
-	 * @param  string $state A Bootstrap state class
215
-	 */
216
-	public function state($state)
217
-	{
218
-		// Filter state
219
-		$state = $this->app['former.framework']->filterState($state);
220
-
221
-		$this->state = $state;
222
-	}
223
-
224
-	/**
225
-	 * Set a class on the Group
226
-	 *
227
-	 * @param string $class The class(es) to add on the Group
228
-	 */
229
-	public function addGroupClass($class)
230
-	{
231
-		$this->addClass($class);
232
-	}
233
-
234
-	/**
235
-	 * Remove one or more classes on the Group
236
-	 *
237
-	 * @param string $class The class(es) to remove on the Group
238
-	 */
239
-	public function removeGroupClass($class)
240
-	{
241
-		$this->removeClass($class);
242
-	}
243
-
244
-	/**
245
-	 * Set a class on the Label
246
-	 *
247
-	 * @param string $class The class(es) to add on the Label
248
-	 */
249
-	public function addLabelClass($class)
250
-	{
251
-		// Don't add a label class if it isn't an Element instance
252
-		if (!$this->label instanceof Element) {
253
-			return $this;
254
-		}
255
-
256
-		$this->label->addClass($class);
257
-
258
-		return $this;
259
-	}
260
-
261
-	/**
262
-	 * Remove one or more classes on the Label
263
-	 *
264
-	 * @param string $class The class(es) to remove on the Label
265
-	 */
266
-	public function removeLabelClass($class)
267
-	{
268
-		// Don't remove a label class if it isn't an Element instance
269
-		if (!$this->label instanceof Element) {
270
-			return $this;
271
-		}
272
-
273
-		$this->label->removeClass($class);
274
-
275
-		return $this;
276
-	}
277
-
278
-	/**
279
-	 * Adds a label to the group
280
-	 *
281
-	 * @param  string $label A label
282
-	 */
283
-	public function setLabel($label)
284
-	{
285
-		if (!$label instanceof Element) {
286
-			$label = Helpers::translate($label);
287
-			$label = Element::create('label', $label)->for($label);
288
-		}
289
-
290
-		$this->label = $label;
291
-	}
292
-
293
-	/**
294
-	 * Get the formatted group label
295
-	 *
296
-	 * @return string|null
297
-	 */
298
-	public function getFormattedLabel()
299
-	{
300
-		if (!$this->label) {
301
-			return false;
302
-		}
303
-
304
-		return $this->label->addClass($this->app['former.framework']->getLabelClasses());
305
-	}
306
-
307
-	/**
308
-	 * Disables the control group for the current field
309
-	 */
310
-	public function raw()
311
-	{
312
-		$this->raw = true;
313
-	}
314
-
315
-	/**
316
-	 * Check if the current group is to be displayed or not
317
-	 *
318
-	 * @return boolean
319
-	 */
320
-	public function isRaw()
321
-	{
322
-		return (bool) $this->raw;
323
-	}
324
-
325
-	////////////////////////////////////////////////////////////////////
326
-	///////////////////////////// HELP BLOCKS //////////////////////////
327
-	////////////////////////////////////////////////////////////////////
328
-
329
-	/**
330
-	 * Alias for inlineHelp
331
-	 *
332
-	 * @param  string $help       The help text
333
-	 * @param  array  $attributes Facultative attributes
334
-	 */
335
-	public function help($help, $attributes = array())
336
-	{
337
-		return $this->inlineHelp($help, $attributes);
338
-	}
339
-
340
-	/**
341
-	 * Add an inline help
342
-	 *
343
-	 * @param  string $help       The help text
344
-	 * @param  array  $attributes Facultative attributes
345
-	 */
346
-	public function inlineHelp($help, $attributes = array())
347
-	{
348
-		// If no help text, do nothing
349
-		if (!$help) {
350
-			return false;
351
-		}
352
-
353
-		$this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes);
354
-	}
355
-
356
-	/**
357
-	 * Add an block help
358
-	 *
359
-	 * @param  string $help       The help text
360
-	 * @param  array  $attributes Facultative attributes
361
-	 */
362
-	public function blockHelp($help, $attributes = array())
363
-	{
364
-		// Reserved method
365
-		if ($this->app['former.framework']->isnt('TwitterBootstrap') &&
366
-			$this->app['former.framework']->isnt('TwitterBootstrap3') &&
367
-			$this->app['former.framework']->isnt('TwitterBootstrap4') &&
368
-			$this->app['former.framework']->isnt('TwitterBootstrap5')
369
-		) {
370
-			throw new BadMethodCallException('This method is only available on the Bootstrap framework');
371
-		}
372
-
373
-		// If no help text, do nothing
374
-		if (!$help) {
375
-			return false;
376
-		}
377
-
378
-		$this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes);
379
-	}
380
-
381
-	////////////////////////////////////////////////////////////////////
382
-	///////////////////////// PREPEND/APPEND METHODS ///////////////////
383
-	////////////////////////////////////////////////////////////////////
384
-
385
-	/**
386
-	 * Prepend elements to the field
387
-	 */
388
-	public function prepend()
389
-	{
390
-		$this->placeAround(func_get_args(), 'prepend');
391
-	}
392
-
393
-	/**
394
-	 * Append elements to the field
395
-	 */
396
-	public function append()
397
-	{
398
-		$this->placeAround(func_get_args(), 'append');
399
-	}
400
-
401
-	/**
402
-	 * Prepends an icon to a field
403
-	 *
404
-	 * @param string $icon       The icon to prepend
405
-	 * @param array  $attributes Its attributes
406
-	 */
407
-	public function prependIcon($icon, $attributes = array(), $iconSettings = array())
408
-	{
409
-		$icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
410
-
411
-		$this->prepend($icon);
412
-	}
413
-
414
-	/**
415
-	 * Append an icon to a field
416
-	 *
417
-	 * @param string $icon       The icon to prepend
418
-	 * @param array  $attributes Its attributes
419
-	 */
420
-	public function appendIcon($icon, $attributes = array(), $iconSettings = array())
421
-	{
422
-		$icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
423
-
424
-		$this->append($icon);
425
-	}
426
-
427
-	////////////////////////////////////////////////////////////////////
428
-	//////////////////////////////// HELPERS ///////////////////////////
429
-	////////////////////////////////////////////////////////////////////
430
-
431
-	/**
432
-	 * Get the errors for the group
433
-	 *
434
-	 * @return string
435
-	 */
436
-	public function getErrors()
437
-	{
438
-		$errors = '';
439
-
440
-		if (!self::$opened) {
441
-
442
-			// for non-custom groups, normal error handling applies
443
-			$errors = $this->app['former']->getErrors();
444
-		} elseif (!empty($this->validations)) {
445
-
446
-			// error handling only when validations specified for custom groups
447
-			foreach ($this->validations as $validation) {
448
-				$errors .= $this->app['former']->getErrors($validation);
449
-			}
450
-		}
451
-
452
-		return $errors;
453
-	}
454
-
455
-	/**
456
-	 * Wraps content in a group
457
-	 *
458
-	 * @param string $contents The content
459
-	 * @param string $label    The label to add
460
-	 *
461
-	 * @return string A group
462
-	 */
463
-	public function wrap($contents, $label = null)
464
-	{
465
-		$group = $this->open();
466
-		$group .= $label;
467
-		$group .= $this->app['former.framework']->wrapField($contents);
468
-		$group .= $this->close();
469
-
470
-		return $group;
471
-	}
472
-
473
-	/**
474
-	 * Wraps content in a group with floating label
475
-	 *
476
-	 * @param string $contents The content
477
-	 * @param string $label    The label to add
478
-	 *
479
-	 * @return string A group
480
-	 */
481
-	public function wrapWithFloatingLabel($contents, $label = null)
482
-	{
483
-		$floatingLabelClass = $this->app['former.framework']->getFloatingLabelClass();
484
-		if ($floatingLabelClass) {
485
-			$this->addClass($floatingLabelClass);
486
-		}
487
-		return $this->wrap($label, $contents);
488
-	}
489
-
490
-	/**
491
-	 * Prints out the current label
492
-	 *
493
-	 * @param  string $field The field to create a label for
494
-	 *
495
-	 * @return string        A <label> tag
496
-	 */
497
-	 protected function getLabel($field = null)
498
- 	{
499
- 		// Don't create a label if none exist
500
- 		if (!$field or !$this->label) {
501
- 			return null;
502
- 		}
503
-
504
- 		// Wrap label in framework classes
505
- 		$labelClasses = $this->app['former.framework']->getLabelClasses();
506
- 		if ($field->isCheckable() &&
507
- 			in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5']) &&
508
- 			$this->app['former.form']->isOfType('horizontal')
509
- 		) {
510
- 			$labelClasses = array_merge($labelClasses, array('pt-0'));
511
- 		}
512
- 		$this->label->addClass($labelClasses);
513
- 		$this->label = $this->app['former.framework']->createLabelOf($field, $this->label);
514
- 		$this->label = $this->app['former.framework']->wrapLabel($this->label);
515
-
516
- 		return $this->label;
517
- 	}
518
-
519
-	/**
520
-	 * Prints out the current help
521
-	 *
522
-	 * @return string A .help-block or .help-inline
523
-	 */
524
-	protected function getHelp()
525
-	{
526
-		$inline = Arr::get($this->help, 'inline');
527
-		$block  = Arr::get($this->help, 'block');
528
-
529
-		// Replace help text with error if any found
530
-		$errors = $this->app['former']->getErrors();
531
-		if ($errors and $this->app['former']->getOption('error_messages')) {
532
-			$inline = $this->app['former.framework']->createValidationError($errors);
533
-		}
534
-
535
-		return implode('', array($inline, $block));
536
-	}
537
-
538
-	/**
539
-	 * Format the field with prepended/appended elements
540
-	 *
541
-	 * @param  Field $field The field to format
542
-	 *
543
-	 * @return string        Field plus supplementary elements
544
-	 */
545
-	protected function prependAppend($field)
546
-	{
547
-		if (!$this->prepend and !$this->append) {
548
-			return $field->render();
549
-		}
550
-
551
-		return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append);
552
-	}
553
-
554
-	/**
555
-	 * Place elements around the field
556
-	 *
557
-	 * @param  array  $items An array of items to place
558
-	 * @param  string $place Where they should end up (prepend|append)
559
-	 */
560
-	protected function placeAround($items, $place)
561
-	{
562
-		// Iterate over the items and place them where they should
563
-		foreach ((array) $items as $item) {
564
-			$item             = $this->app['former.framework']->placeAround($item, $place);
565
-			$this->{$place}[] = $item;
566
-		}
567
-	}
17
+    /**
18
+     * The Container
19
+     *
20
+     * @var Container
21
+     */
22
+    protected $app;
23
+
24
+    /**
25
+     * The current state of the group
26
+     *
27
+     * @var string
28
+     */
29
+    protected $state = null;
30
+
31
+    /**
32
+     * Whether the field should be displayed raw or not
33
+     *
34
+     * @var boolean
35
+     */
36
+    protected $raw = false;
37
+
38
+    /**
39
+     * The group label
40
+     *
41
+     * @var Element
42
+     */
43
+    protected $label;
44
+
45
+    /**
46
+     * The group help
47
+     *
48
+     * @var array
49
+     */
50
+    protected $help = array();
51
+
52
+    /**
53
+     * An array of elements to preprend the field
54
+     *
55
+     * @var array
56
+     */
57
+    protected $prepend = array();
58
+
59
+    /**
60
+     * An array of elements to append the field
61
+     *
62
+     * @var array
63
+     */
64
+    protected $append = array();
65
+
66
+    /**
67
+     * The field validations to be checked for errors
68
+     *
69
+     * @var array
70
+     */
71
+    protected $validations = array();
72
+
73
+    /**
74
+     * The group's element
75
+     *
76
+     * @var string
77
+     */
78
+    protected $element = 'div';
79
+
80
+    /**
81
+     * Whether a custom group is opened or not
82
+     *
83
+     * @var boolean
84
+     */
85
+    public static $opened = false;
86
+
87
+    /**
88
+     * The custom group that is open
89
+     *
90
+     * @var Former\Form\Group
91
+     */
92
+    public static $openGroup = null;
93
+
94
+    ////////////////////////////////////////////////////////////////////
95
+    /////////////////////////// CORE METHODS ///////////////////////////
96
+    ////////////////////////////////////////////////////////////////////
97
+
98
+    /**
99
+     * Creates a group
100
+     *
101
+     * @param string $label Its label
102
+     */
103
+    public function __construct(Container $app, $label, $validations = null)
104
+    {
105
+        // Get special classes
106
+        $this->app = $app;
107
+        $this->addClass($this->app['former.framework']->getGroupClasses());
108
+
109
+        // Invisible if Nude
110
+        if ($this->app['former.framework']->is('Nude')) {
111
+            $this->element = '';
112
+        }
113
+
114
+        // Set group label
115
+        if ($label) {
116
+            $this->setLabel($label);
117
+        }
118
+
119
+        // Set validations used to override groups own conclusions
120
+        $this->validations = (array) $validations;
121
+    }
122
+
123
+    /**
124
+     * Prints out the opening of the Control Group
125
+     *
126
+     * @return string A control group opening tag
127
+     */
128
+    public function __toString()
129
+    {
130
+        return $this->open().$this->getFormattedLabel();
131
+    }
132
+
133
+    /**
134
+     * Opens a group
135
+     *
136
+     * @return string Opening tag
137
+     */
138
+    public function open()
139
+    {
140
+        if ($this->getErrors()) {
141
+            $this->state($this->app['former.framework']->errorState());
142
+        }
143
+
144
+        // Retrieve state and append it to classes
145
+        if ($this->state) {
146
+            $this->addClass($this->state);
147
+        }
148
+
149
+        // Required state
150
+        if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) {
151
+            $this->addClass($this->app['former']->getOption('required_class'));
152
+        }
153
+
154
+        return parent::open();
155
+    }
156
+
157
+    /**
158
+     * Set the contents of the current group
159
+     *
160
+     * @param string $contents The group contents
161
+     *
162
+     * @return string A group
163
+     */
164
+    public function contents($contents)
165
+    {
166
+        return $this->wrap($contents, $this->getFormattedLabel());
167
+    }
168
+
169
+    /**
170
+     * Wrap a Field with the current group
171
+     *
172
+     * @param  \Former\Traits\Field $field A Field instance
173
+     *
174
+     * @return string        A group
175
+     */
176
+    public function wrapField($field)
177
+    {
178
+        $label = $this->getLabel($field);
179
+        $help = $this->getHelp();
180
+        if ($field->isCheckable() &&
181
+            in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])
182
+        ) {
183
+            $wrapperClass = null;
184
+            if ($this->app['former']->framework() === 'TwitterBootstrap4') {
185
+                $wrapperClass = $field->isInline() ? 'form-check form-check-inline' : 'form-check';
186
+            }
187
+            if ($this->app['former']->getErrors($field->getName())) {
188
+                $hiddenInput = Input::create('hidden')->addClass('form-check-input is-invalid');
189
+                $help = $hiddenInput.$help;
190
+            }
191
+            $help = $help ? Element::create('div', $help)->addClass($wrapperClass) : '';
192
+        }
193
+        $withFloatingLabel = $field->withFloatingLabel();
194
+
195
+        $field = $this->prependAppend($field);
196
+        $field .= $help;
197
+
198
+        if ($withFloatingLabel &&
199
+            $this->app['former']->framework() === 'TwitterBootstrap5'
200
+        ) {
201
+            return $this->wrapWithFloatingLabel($field, $label);
202
+        }
203
+
204
+        return $this->wrap($field, $label);
205
+    }
206
+
207
+    ////////////////////////////////////////////////////////////////////
208
+    //////////////////////////// FIELD METHODS /////////////////////////
209
+    ////////////////////////////////////////////////////////////////////
210
+
211
+    /**
212
+     * Set the state of the group
213
+     *
214
+     * @param  string $state A Bootstrap state class
215
+     */
216
+    public function state($state)
217
+    {
218
+        // Filter state
219
+        $state = $this->app['former.framework']->filterState($state);
220
+
221
+        $this->state = $state;
222
+    }
223
+
224
+    /**
225
+     * Set a class on the Group
226
+     *
227
+     * @param string $class The class(es) to add on the Group
228
+     */
229
+    public function addGroupClass($class)
230
+    {
231
+        $this->addClass($class);
232
+    }
233
+
234
+    /**
235
+     * Remove one or more classes on the Group
236
+     *
237
+     * @param string $class The class(es) to remove on the Group
238
+     */
239
+    public function removeGroupClass($class)
240
+    {
241
+        $this->removeClass($class);
242
+    }
243
+
244
+    /**
245
+     * Set a class on the Label
246
+     *
247
+     * @param string $class The class(es) to add on the Label
248
+     */
249
+    public function addLabelClass($class)
250
+    {
251
+        // Don't add a label class if it isn't an Element instance
252
+        if (!$this->label instanceof Element) {
253
+            return $this;
254
+        }
255
+
256
+        $this->label->addClass($class);
257
+
258
+        return $this;
259
+    }
260
+
261
+    /**
262
+     * Remove one or more classes on the Label
263
+     *
264
+     * @param string $class The class(es) to remove on the Label
265
+     */
266
+    public function removeLabelClass($class)
267
+    {
268
+        // Don't remove a label class if it isn't an Element instance
269
+        if (!$this->label instanceof Element) {
270
+            return $this;
271
+        }
272
+
273
+        $this->label->removeClass($class);
274
+
275
+        return $this;
276
+    }
277
+
278
+    /**
279
+     * Adds a label to the group
280
+     *
281
+     * @param  string $label A label
282
+     */
283
+    public function setLabel($label)
284
+    {
285
+        if (!$label instanceof Element) {
286
+            $label = Helpers::translate($label);
287
+            $label = Element::create('label', $label)->for($label);
288
+        }
289
+
290
+        $this->label = $label;
291
+    }
292
+
293
+    /**
294
+     * Get the formatted group label
295
+     *
296
+     * @return string|null
297
+     */
298
+    public function getFormattedLabel()
299
+    {
300
+        if (!$this->label) {
301
+            return false;
302
+        }
303
+
304
+        return $this->label->addClass($this->app['former.framework']->getLabelClasses());
305
+    }
306
+
307
+    /**
308
+     * Disables the control group for the current field
309
+     */
310
+    public function raw()
311
+    {
312
+        $this->raw = true;
313
+    }
314
+
315
+    /**
316
+     * Check if the current group is to be displayed or not
317
+     *
318
+     * @return boolean
319
+     */
320
+    public function isRaw()
321
+    {
322
+        return (bool) $this->raw;
323
+    }
324
+
325
+    ////////////////////////////////////////////////////////////////////
326
+    ///////////////////////////// HELP BLOCKS //////////////////////////
327
+    ////////////////////////////////////////////////////////////////////
328
+
329
+    /**
330
+     * Alias for inlineHelp
331
+     *
332
+     * @param  string $help       The help text
333
+     * @param  array  $attributes Facultative attributes
334
+     */
335
+    public function help($help, $attributes = array())
336
+    {
337
+        return $this->inlineHelp($help, $attributes);
338
+    }
339
+
340
+    /**
341
+     * Add an inline help
342
+     *
343
+     * @param  string $help       The help text
344
+     * @param  array  $attributes Facultative attributes
345
+     */
346
+    public function inlineHelp($help, $attributes = array())
347
+    {
348
+        // If no help text, do nothing
349
+        if (!$help) {
350
+            return false;
351
+        }
352
+
353
+        $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes);
354
+    }
355
+
356
+    /**
357
+     * Add an block help
358
+     *
359
+     * @param  string $help       The help text
360
+     * @param  array  $attributes Facultative attributes
361
+     */
362
+    public function blockHelp($help, $attributes = array())
363
+    {
364
+        // Reserved method
365
+        if ($this->app['former.framework']->isnt('TwitterBootstrap') &&
366
+            $this->app['former.framework']->isnt('TwitterBootstrap3') &&
367
+            $this->app['former.framework']->isnt('TwitterBootstrap4') &&
368
+            $this->app['former.framework']->isnt('TwitterBootstrap5')
369
+        ) {
370
+            throw new BadMethodCallException('This method is only available on the Bootstrap framework');
371
+        }
372
+
373
+        // If no help text, do nothing
374
+        if (!$help) {
375
+            return false;
376
+        }
377
+
378
+        $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes);
379
+    }
380
+
381
+    ////////////////////////////////////////////////////////////////////
382
+    ///////////////////////// PREPEND/APPEND METHODS ///////////////////
383
+    ////////////////////////////////////////////////////////////////////
384
+
385
+    /**
386
+     * Prepend elements to the field
387
+     */
388
+    public function prepend()
389
+    {
390
+        $this->placeAround(func_get_args(), 'prepend');
391
+    }
392
+
393
+    /**
394
+     * Append elements to the field
395
+     */
396
+    public function append()
397
+    {
398
+        $this->placeAround(func_get_args(), 'append');
399
+    }
400
+
401
+    /**
402
+     * Prepends an icon to a field
403
+     *
404
+     * @param string $icon       The icon to prepend
405
+     * @param array  $attributes Its attributes
406
+     */
407
+    public function prependIcon($icon, $attributes = array(), $iconSettings = array())
408
+    {
409
+        $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
410
+
411
+        $this->prepend($icon);
412
+    }
413
+
414
+    /**
415
+     * Append an icon to a field
416
+     *
417
+     * @param string $icon       The icon to prepend
418
+     * @param array  $attributes Its attributes
419
+     */
420
+    public function appendIcon($icon, $attributes = array(), $iconSettings = array())
421
+    {
422
+        $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
423
+
424
+        $this->append($icon);
425
+    }
426
+
427
+    ////////////////////////////////////////////////////////////////////
428
+    //////////////////////////////// HELPERS ///////////////////////////
429
+    ////////////////////////////////////////////////////////////////////
430
+
431
+    /**
432
+     * Get the errors for the group
433
+     *
434
+     * @return string
435
+     */
436
+    public function getErrors()
437
+    {
438
+        $errors = '';
439
+
440
+        if (!self::$opened) {
441
+
442
+            // for non-custom groups, normal error handling applies
443
+            $errors = $this->app['former']->getErrors();
444
+        } elseif (!empty($this->validations)) {
445
+
446
+            // error handling only when validations specified for custom groups
447
+            foreach ($this->validations as $validation) {
448
+                $errors .= $this->app['former']->getErrors($validation);
449
+            }
450
+        }
451
+
452
+        return $errors;
453
+    }
454
+
455
+    /**
456
+     * Wraps content in a group
457
+     *
458
+     * @param string $contents The content
459
+     * @param string $label    The label to add
460
+     *
461
+     * @return string A group
462
+     */
463
+    public function wrap($contents, $label = null)
464
+    {
465
+        $group = $this->open();
466
+        $group .= $label;
467
+        $group .= $this->app['former.framework']->wrapField($contents);
468
+        $group .= $this->close();
469
+
470
+        return $group;
471
+    }
472
+
473
+    /**
474
+     * Wraps content in a group with floating label
475
+     *
476
+     * @param string $contents The content
477
+     * @param string $label    The label to add
478
+     *
479
+     * @return string A group
480
+     */
481
+    public function wrapWithFloatingLabel($contents, $label = null)
482
+    {
483
+        $floatingLabelClass = $this->app['former.framework']->getFloatingLabelClass();
484
+        if ($floatingLabelClass) {
485
+            $this->addClass($floatingLabelClass);
486
+        }
487
+        return $this->wrap($label, $contents);
488
+    }
489
+
490
+    /**
491
+     * Prints out the current label
492
+     *
493
+     * @param  string $field The field to create a label for
494
+     *
495
+     * @return string        A <label> tag
496
+     */
497
+        protected function getLabel($field = null)
498
+        {
499
+            // Don't create a label if none exist
500
+            if (!$field or !$this->label) {
501
+                return null;
502
+            }
503
+
504
+            // Wrap label in framework classes
505
+            $labelClasses = $this->app['former.framework']->getLabelClasses();
506
+            if ($field->isCheckable() &&
507
+             in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5']) &&
508
+             $this->app['former.form']->isOfType('horizontal')
509
+            ) {
510
+                $labelClasses = array_merge($labelClasses, array('pt-0'));
511
+            }
512
+            $this->label->addClass($labelClasses);
513
+            $this->label = $this->app['former.framework']->createLabelOf($field, $this->label);
514
+            $this->label = $this->app['former.framework']->wrapLabel($this->label);
515
+
516
+            return $this->label;
517
+        }
518
+
519
+    /**
520
+     * Prints out the current help
521
+     *
522
+     * @return string A .help-block or .help-inline
523
+     */
524
+    protected function getHelp()
525
+    {
526
+        $inline = Arr::get($this->help, 'inline');
527
+        $block  = Arr::get($this->help, 'block');
528
+
529
+        // Replace help text with error if any found
530
+        $errors = $this->app['former']->getErrors();
531
+        if ($errors and $this->app['former']->getOption('error_messages')) {
532
+            $inline = $this->app['former.framework']->createValidationError($errors);
533
+        }
534
+
535
+        return implode('', array($inline, $block));
536
+    }
537
+
538
+    /**
539
+     * Format the field with prepended/appended elements
540
+     *
541
+     * @param  Field $field The field to format
542
+     *
543
+     * @return string        Field plus supplementary elements
544
+     */
545
+    protected function prependAppend($field)
546
+    {
547
+        if (!$this->prepend and !$this->append) {
548
+            return $field->render();
549
+        }
550
+
551
+        return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append);
552
+    }
553
+
554
+    /**
555
+     * Place elements around the field
556
+     *
557
+     * @param  array  $items An array of items to place
558
+     * @param  string $place Where they should end up (prepend|append)
559
+     */
560
+    protected function placeAround($items, $place)
561
+    {
562
+        // Iterate over the items and place them where they should
563
+        foreach ((array) $items as $item) {
564
+            $item             = $this->app['former.framework']->placeAround($item, $place);
565
+            $this->{$place}[] = $item;
566
+        }
567
+    }
568 568
 }
Please login to merge, or discard this patch.
src/Former/Form/Elements.php 1 patch
Indentation   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -12,106 +12,106 @@
 block discarded – undo
12 12
  */
13 13
 class Elements
14 14
 {
15
-	/**
16
-	 * The Container
17
-	 *
18
-	 * @var Container
19
-	 */
20
-	protected $app;
21
-
22
-	/**
23
-	 * The Session instance
24
-	 *
25
-	 * @var Session
26
-	 */
27
-	protected $session;
28
-
29
-	/**
30
-	 * Build a new Element
31
-	 *
32
-	 * @param Container $app
33
-	 */
34
-	public function __construct(Container $app, $session)
35
-	{
36
-		$this->app     = $app;
37
-		$this->session = $session;
38
-	}
39
-
40
-	/**
41
-	 * Generate a hidden field containing the current CSRF token.
42
-	 *
43
-	 * @return string
44
-	 */
45
-	public function token()
46
-	{
47
-		$csrf = method_exists($this->session, 'getToken') ? $this->session->getToken() : $this->session->token();
48
-
49
-		return (string) $this->app['former']->hidden('_token', $csrf);
50
-	}
51
-
52
-	/**
53
-	 * Creates a label tag
54
-	 *
55
-	 * @param  string $label      The label content
56
-	 * @param  string $for        The field the label's for
57
-	 * @param  array  $attributes The label's attributes
58
-	 *
59
-	 * @return Element             A <label> tag
60
-	 */
61
-	public function label($label, $for = null, $attributes = array())
62
-	{
63
-		if (!$label instanceof Htmlable) {
64
-			$oldLabel = (string) $label;
65
-			$label    = Helpers::translate($oldLabel, '');
66
-
67
-			// If there was no change to the label,
68
-			// then a Laravel translation did not occur
69
-			if (lcfirst($label) == $oldLabel) {
70
-				$label = str_replace('_', ' ', $label);
71
-			}
72
-		} else {
73
-			$label = $label->toHtml();
74
-		}
75
-
76
-		$attributes['for']             = $for;
77
-		$this->app['former']->labels[] = $for;
78
-
79
-		return Element::create('label', $label, $attributes);
80
-	}
81
-
82
-	/**
83
-	 * Creates a form legend
84
-	 *
85
-	 * @param  string $legend     The text
86
-	 * @param  array  $attributes Its attributes
87
-	 *
88
-	 * @return Element             A <legend> tag
89
-	 */
90
-	public function legend($legend, $attributes = array())
91
-	{
92
-		$legend = Helpers::translate($legend);
93
-
94
-		return Element::create('legend', $legend, $attributes);
95
-	}
96
-
97
-	/**
98
-	 * Close a field group
99
-	 *
100
-	 * @return string
101
-	 */
102
-	public function closeGroup()
103
-	{
104
-		$closing = '';
105
-		if (Group::$opened && isset(Group::$openGroup)) {
106
-			$closing = Group::$openGroup->close();
107
-		}
108
-
109
-		// Close custom group
110
-		Group::$opened = false;
111
-
112
-		// Reset custom group reference
113
-		Group::$openGroup = null;
114
-
115
-		return $closing;
116
-	}
15
+    /**
16
+     * The Container
17
+     *
18
+     * @var Container
19
+     */
20
+    protected $app;
21
+
22
+    /**
23
+     * The Session instance
24
+     *
25
+     * @var Session
26
+     */
27
+    protected $session;
28
+
29
+    /**
30
+     * Build a new Element
31
+     *
32
+     * @param Container $app
33
+     */
34
+    public function __construct(Container $app, $session)
35
+    {
36
+        $this->app     = $app;
37
+        $this->session = $session;
38
+    }
39
+
40
+    /**
41
+     * Generate a hidden field containing the current CSRF token.
42
+     *
43
+     * @return string
44
+     */
45
+    public function token()
46
+    {
47
+        $csrf = method_exists($this->session, 'getToken') ? $this->session->getToken() : $this->session->token();
48
+
49
+        return (string) $this->app['former']->hidden('_token', $csrf);
50
+    }
51
+
52
+    /**
53
+     * Creates a label tag
54
+     *
55
+     * @param  string $label      The label content
56
+     * @param  string $for        The field the label's for
57
+     * @param  array  $attributes The label's attributes
58
+     *
59
+     * @return Element             A <label> tag
60
+     */
61
+    public function label($label, $for = null, $attributes = array())
62
+    {
63
+        if (!$label instanceof Htmlable) {
64
+            $oldLabel = (string) $label;
65
+            $label    = Helpers::translate($oldLabel, '');
66
+
67
+            // If there was no change to the label,
68
+            // then a Laravel translation did not occur
69
+            if (lcfirst($label) == $oldLabel) {
70
+                $label = str_replace('_', ' ', $label);
71
+            }
72
+        } else {
73
+            $label = $label->toHtml();
74
+        }
75
+
76
+        $attributes['for']             = $for;
77
+        $this->app['former']->labels[] = $for;
78
+
79
+        return Element::create('label', $label, $attributes);
80
+    }
81
+
82
+    /**
83
+     * Creates a form legend
84
+     *
85
+     * @param  string $legend     The text
86
+     * @param  array  $attributes Its attributes
87
+     *
88
+     * @return Element             A <legend> tag
89
+     */
90
+    public function legend($legend, $attributes = array())
91
+    {
92
+        $legend = Helpers::translate($legend);
93
+
94
+        return Element::create('legend', $legend, $attributes);
95
+    }
96
+
97
+    /**
98
+     * Close a field group
99
+     *
100
+     * @return string
101
+     */
102
+    public function closeGroup()
103
+    {
104
+        $closing = '';
105
+        if (Group::$opened && isset(Group::$openGroup)) {
106
+            $closing = Group::$openGroup->close();
107
+        }
108
+
109
+        // Close custom group
110
+        Group::$opened = false;
111
+
112
+        // Reset custom group reference
113
+        Group::$openGroup = null;
114
+
115
+        return $closing;
116
+    }
117 117
 }
Please login to merge, or discard this patch.