Completed
Branch scrutinizer-patch-1 (42a7d0)
by Ben
02:17
created
src/Former/Form/Fields/File.php 1 patch
Indentation   +123 added lines, -123 removed lines patch added patch discarded remove patch
@@ -12,127 +12,127 @@
 block discarded – undo
12 12
 class File extends Field
13 13
 {
14 14
 
15
-	/**
16
-	 * The maximum file size
17
-	 *
18
-	 * @var integer
19
-	 */
20
-	private $maxSize;
21
-
22
-	/**
23
-	 * An array of mime groups to use as shortcuts
24
-	 *
25
-	 * @var array
26
-	 */
27
-	private $mimeGroups = array('audio', 'video', 'image');
28
-
29
-	/**
30
-	 * A list of properties to be injected in the attributes
31
-	 *
32
-	 * @var array
33
-	 */
34
-	protected $injectedProperties = array('type', 'name');
35
-
36
-	////////////////////////////////////////////////////////////////////
37
-	/////////////////////////// CORE METHODS ///////////////////////////
38
-	////////////////////////////////////////////////////////////////////
39
-
40
-	/**
41
-	 * Easier arguments order for hidden fields
42
-	 *
43
-	 * @param Container $app        The Illuminate Container
44
-	 * @param string    $type       file
45
-	 * @param string    $name       Field name
46
-	 * @param string    $label      Its label
47
-	 * @param string    $value      Its value
48
-	 * @param array     $attributes Attributes
49
-	 */
50
-	public function __construct(Container $app, $type, $name, $label, $value, $attributes)
51
-	{
52
-		// Multiple files field
53
-		if ($type == 'files') {
54
-			$attributes['multiple'] = 'true';
55
-			$type                   = 'file';
56
-			$name                   = $name.'[]';
57
-		}
58
-
59
-		parent::__construct($app, $type, $name, $label, $value, $attributes);
60
-	}
61
-
62
-	/**
63
-	 * Prints out the current tag
64
-	 *
65
-	 * @return string An input file tag
66
-	 */
67
-	public function render()
68
-	{
69
-		// Maximum file size
70
-		$hidden = $this->maxSize
71
-			? HtmlInput::hidden('MAX_FILE_SIZE', $this->maxSize)
72
-			: null;
73
-
74
-		return $hidden.parent::render();
75
-	}
76
-
77
-	////////////////////////////////////////////////////////////////////
78
-	////////////////////////// FIELD METHODS ///////////////////////////
79
-	////////////////////////////////////////////////////////////////////
80
-
81
-	/**
82
-	 * Set which types of files are accepted by the file input
83
-	 */
84
-	public function accept()
85
-	{
86
-		$mimes = array();
87
-
88
-		// Transform all extensions/groups to mime types
89
-		foreach (func_get_args() as $mime) {
90
-
91
-			// Shortcuts and extensions
92
-			if (in_array($mime, $this->mimeGroups)) {
93
-				$mime .= '/*';
94
-			}
95
-			$mime = LaravelFile::mime($mime, $mime);
96
-
97
-			$mimes[] = $mime;
98
-		}
99
-
100
-		// Add accept attribute by concatenating the mimes
101
-		$this->attributes['accept'] = implode(',', $mimes);
102
-
103
-		return $this;
104
-	}
105
-
106
-	/**
107
-	 * Set a maximum size for files
108
-	 *
109
-	 * @param integer $size  A maximum size
110
-	 * @param string  $units The size's unit
111
-	 */
112
-	public function max($size, $units = 'KB')
113
-	{
114
-		// Bytes or bits ?
115
-		$unit = substr($units, -1);
116
-		$base = 1024;
117
-		if ($unit == 'b') {
118
-			$size = $size / 8;
119
-		}
120
-
121
-		// Convert
122
-		switch ($units[0]) {
123
-			case 'K':
124
-				$size = $size * $base;
125
-				break;
126
-			case 'M':
127
-				$size = $size * pow($base, 2);
128
-				break;
129
-			case 'G':
130
-				$size = $size * pow($base, 3);
131
-				break;
132
-		}
133
-
134
-		$this->maxSize = (int) $size;
135
-
136
-		return $this;
137
-	}
15
+    /**
16
+     * The maximum file size
17
+     *
18
+     * @var integer
19
+     */
20
+    private $maxSize;
21
+
22
+    /**
23
+     * An array of mime groups to use as shortcuts
24
+     *
25
+     * @var array
26
+     */
27
+    private $mimeGroups = array('audio', 'video', 'image');
28
+
29
+    /**
30
+     * A list of properties to be injected in the attributes
31
+     *
32
+     * @var array
33
+     */
34
+    protected $injectedProperties = array('type', 'name');
35
+
36
+    ////////////////////////////////////////////////////////////////////
37
+    /////////////////////////// CORE METHODS ///////////////////////////
38
+    ////////////////////////////////////////////////////////////////////
39
+
40
+    /**
41
+     * Easier arguments order for hidden fields
42
+     *
43
+     * @param Container $app        The Illuminate Container
44
+     * @param string    $type       file
45
+     * @param string    $name       Field name
46
+     * @param string    $label      Its label
47
+     * @param string    $value      Its value
48
+     * @param array     $attributes Attributes
49
+     */
50
+    public function __construct(Container $app, $type, $name, $label, $value, $attributes)
51
+    {
52
+        // Multiple files field
53
+        if ($type == 'files') {
54
+            $attributes['multiple'] = 'true';
55
+            $type                   = 'file';
56
+            $name                   = $name.'[]';
57
+        }
58
+
59
+        parent::__construct($app, $type, $name, $label, $value, $attributes);
60
+    }
61
+
62
+    /**
63
+     * Prints out the current tag
64
+     *
65
+     * @return string An input file tag
66
+     */
67
+    public function render()
68
+    {
69
+        // Maximum file size
70
+        $hidden = $this->maxSize
71
+            ? HtmlInput::hidden('MAX_FILE_SIZE', $this->maxSize)
72
+            : null;
73
+
74
+        return $hidden.parent::render();
75
+    }
76
+
77
+    ////////////////////////////////////////////////////////////////////
78
+    ////////////////////////// FIELD METHODS ///////////////////////////
79
+    ////////////////////////////////////////////////////////////////////
80
+
81
+    /**
82
+     * Set which types of files are accepted by the file input
83
+     */
84
+    public function accept()
85
+    {
86
+        $mimes = array();
87
+
88
+        // Transform all extensions/groups to mime types
89
+        foreach (func_get_args() as $mime) {
90
+
91
+            // Shortcuts and extensions
92
+            if (in_array($mime, $this->mimeGroups)) {
93
+                $mime .= '/*';
94
+            }
95
+            $mime = LaravelFile::mime($mime, $mime);
96
+
97
+            $mimes[] = $mime;
98
+        }
99
+
100
+        // Add accept attribute by concatenating the mimes
101
+        $this->attributes['accept'] = implode(',', $mimes);
102
+
103
+        return $this;
104
+    }
105
+
106
+    /**
107
+     * Set a maximum size for files
108
+     *
109
+     * @param integer $size  A maximum size
110
+     * @param string  $units The size's unit
111
+     */
112
+    public function max($size, $units = 'KB')
113
+    {
114
+        // Bytes or bits ?
115
+        $unit = substr($units, -1);
116
+        $base = 1024;
117
+        if ($unit == 'b') {
118
+            $size = $size / 8;
119
+        }
120
+
121
+        // Convert
122
+        switch ($units[0]) {
123
+            case 'K':
124
+                $size = $size * $base;
125
+                break;
126
+            case 'M':
127
+                $size = $size * pow($base, 2);
128
+                break;
129
+            case 'G':
130
+                $size = $size * pow($base, 3);
131
+                break;
132
+        }
133
+
134
+        $this->maxSize = (int) $size;
135
+
136
+        return $this;
137
+    }
138 138
 }
Please login to merge, or discard this patch.
src/Former/Framework/Nude.php 1 patch
Indentation   +178 added lines, -178 removed lines patch added patch discarded remove patch
@@ -14,182 +14,182 @@
 block discarded – undo
14 14
 class Nude extends Framework implements FrameworkInterface
15 15
 {
16 16
 
17
-	/**
18
-	 * The field states available
19
-	 *
20
-	 * @var array
21
-	 */
22
-	protected $states = array(
23
-		'error',
24
-	);
25
-
26
-	/**
27
-	 * Create a new Nude instance
28
-	 *
29
-	 * @param Container $app
30
-	 */
31
-	public function __construct(Container $app)
32
-	{
33
-		$this->app = $app;
34
-		$this->setFrameworkDefaults();
35
-	}
36
-
37
-	////////////////////////////////////////////////////////////////////
38
-	/////////////////////////// FILTER ARRAYS //////////////////////////
39
-	////////////////////////////////////////////////////////////////////
40
-
41
-	public function filterButtonClasses($classes)
42
-	{
43
-		return $classes;
44
-	}
45
-
46
-	public function filterFieldClasses($classes)
47
-	{
48
-		return $classes;
49
-	}
50
-
51
-	////////////////////////////////////////////////////////////////////
52
-	///////////////////////////// ADD CLASSES //////////////////////////
53
-	////////////////////////////////////////////////////////////////////
54
-
55
-	public function getFieldClasses(Field $field, $classes = array())
56
-	{
57
-		$classes = $this->filterFieldClasses($classes);
58
-
59
-		// If we found any class, add them
60
-		if ($classes) {
61
-			$field->class(implode(' ', $classes));
62
-		}
63
-
64
-		return $field;
65
-	}
66
-
67
-	public function getGroupClasses()
68
-	{
69
-		return null;
70
-	}
71
-
72
-	public function getLabelClasses()
73
-	{
74
-		return null;
75
-	}
76
-
77
-	public function getUneditableClasses()
78
-	{
79
-		return null;
80
-	}
81
-
82
-	public function getPlainTextClasses()
83
-	{
84
-		return null;
85
-	}
86
-
87
-	public function getFormClasses($type)
88
-	{
89
-		return null;
90
-	}
91
-
92
-	public function getActionClasses()
93
-	{
94
-		return null;
95
-	}
96
-
97
-	////////////////////////////////////////////////////////////////////
98
-	//////////////////////////// RENDER BLOCKS /////////////////////////
99
-	////////////////////////////////////////////////////////////////////
100
-
101
-	/**
102
-	 * Create an help text
103
-	 */
104
-	public function createHelp($text, $attributes = array())
105
-	{
106
-		return Element::create('span', $text, $attributes)->addClass('help');
107
-	}
108
-
109
-	/**
110
-	 * Render a disabled field
111
-	 *
112
-	 * @param Field $field
113
-	 *
114
-	 * @return Input
115
-	 */
116
-	public function createDisabledField(Field $field)
117
-	{
118
-		$field->disabled();
119
-
120
-		return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
121
-	}
122
-
123
-	/**
124
-	 * Render a plain text field
125
-	 * Which fallback to a disabled field
126
-	 *
127
-	 * @param Field $field
128
-	 *
129
-	 * @return Input
130
-	 */
131
-	public function createPlainTextField(Field $field)
132
-	{
133
-		return $this->createDisabledField($field);
134
-	}
135
-
136
-	////////////////////////////////////////////////////////////////////
137
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
138
-	////////////////////////////////////////////////////////////////////
139
-
140
-	/**
141
-	 * Wrap an item to be prepended or appended to the current field
142
-	 *
143
-	 * @param  string $item
144
-	 *
145
-	 * @return Element A wrapped item
146
-	 */
147
-	public function placeAround($item)
148
-	{
149
-		return Element::create('span', $item);
150
-	}
151
-
152
-	/**
153
-	 * Wrap a field with prepended and appended items
154
-	 *
155
-	 * @param  Field $field
156
-	 * @param  array $prepend
157
-	 * @param  array $append
158
-	 *
159
-	 * @return string A field concatented with prepended and/or appended items
160
-	 */
161
-	public function prependAppend($field, $prepend, $append)
162
-	{
163
-		$return = '<div>';
164
-		$return .= join(null, $prepend);
165
-		$return .= $field->render();
166
-		$return .= join(null, $append);
167
-		$return .= '</div>';
168
-
169
-		return $return;
170
-	}
171
-
172
-	/**
173
-	 * Wraps all field contents with potential additional tags.
174
-	 *
175
-	 * @param  Field $field
176
-	 *
177
-	 * @return Field A wrapped field
178
-	 */
179
-	public function wrapField($field)
180
-	{
181
-		return $field;
182
-	}
183
-
184
-	/**
185
-	 * Wrap actions block with potential additional tags
186
-	 *
187
-	 * @param  Actions $actions
188
-	 *
189
-	 * @return string A wrapped actions block
190
-	 */
191
-	public function wrapActions($actions)
192
-	{
193
-		return $actions;
194
-	}
17
+    /**
18
+     * The field states available
19
+     *
20
+     * @var array
21
+     */
22
+    protected $states = array(
23
+        'error',
24
+    );
25
+
26
+    /**
27
+     * Create a new Nude instance
28
+     *
29
+     * @param Container $app
30
+     */
31
+    public function __construct(Container $app)
32
+    {
33
+        $this->app = $app;
34
+        $this->setFrameworkDefaults();
35
+    }
36
+
37
+    ////////////////////////////////////////////////////////////////////
38
+    /////////////////////////// FILTER ARRAYS //////////////////////////
39
+    ////////////////////////////////////////////////////////////////////
40
+
41
+    public function filterButtonClasses($classes)
42
+    {
43
+        return $classes;
44
+    }
45
+
46
+    public function filterFieldClasses($classes)
47
+    {
48
+        return $classes;
49
+    }
50
+
51
+    ////////////////////////////////////////////////////////////////////
52
+    ///////////////////////////// ADD CLASSES //////////////////////////
53
+    ////////////////////////////////////////////////////////////////////
54
+
55
+    public function getFieldClasses(Field $field, $classes = array())
56
+    {
57
+        $classes = $this->filterFieldClasses($classes);
58
+
59
+        // If we found any class, add them
60
+        if ($classes) {
61
+            $field->class(implode(' ', $classes));
62
+        }
63
+
64
+        return $field;
65
+    }
66
+
67
+    public function getGroupClasses()
68
+    {
69
+        return null;
70
+    }
71
+
72
+    public function getLabelClasses()
73
+    {
74
+        return null;
75
+    }
76
+
77
+    public function getUneditableClasses()
78
+    {
79
+        return null;
80
+    }
81
+
82
+    public function getPlainTextClasses()
83
+    {
84
+        return null;
85
+    }
86
+
87
+    public function getFormClasses($type)
88
+    {
89
+        return null;
90
+    }
91
+
92
+    public function getActionClasses()
93
+    {
94
+        return null;
95
+    }
96
+
97
+    ////////////////////////////////////////////////////////////////////
98
+    //////////////////////////// RENDER BLOCKS /////////////////////////
99
+    ////////////////////////////////////////////////////////////////////
100
+
101
+    /**
102
+     * Create an help text
103
+     */
104
+    public function createHelp($text, $attributes = array())
105
+    {
106
+        return Element::create('span', $text, $attributes)->addClass('help');
107
+    }
108
+
109
+    /**
110
+     * Render a disabled field
111
+     *
112
+     * @param Field $field
113
+     *
114
+     * @return Input
115
+     */
116
+    public function createDisabledField(Field $field)
117
+    {
118
+        $field->disabled();
119
+
120
+        return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
121
+    }
122
+
123
+    /**
124
+     * Render a plain text field
125
+     * Which fallback to a disabled field
126
+     *
127
+     * @param Field $field
128
+     *
129
+     * @return Input
130
+     */
131
+    public function createPlainTextField(Field $field)
132
+    {
133
+        return $this->createDisabledField($field);
134
+    }
135
+
136
+    ////////////////////////////////////////////////////////////////////
137
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
138
+    ////////////////////////////////////////////////////////////////////
139
+
140
+    /**
141
+     * Wrap an item to be prepended or appended to the current field
142
+     *
143
+     * @param  string $item
144
+     *
145
+     * @return Element A wrapped item
146
+     */
147
+    public function placeAround($item)
148
+    {
149
+        return Element::create('span', $item);
150
+    }
151
+
152
+    /**
153
+     * Wrap a field with prepended and appended items
154
+     *
155
+     * @param  Field $field
156
+     * @param  array $prepend
157
+     * @param  array $append
158
+     *
159
+     * @return string A field concatented with prepended and/or appended items
160
+     */
161
+    public function prependAppend($field, $prepend, $append)
162
+    {
163
+        $return = '<div>';
164
+        $return .= join(null, $prepend);
165
+        $return .= $field->render();
166
+        $return .= join(null, $append);
167
+        $return .= '</div>';
168
+
169
+        return $return;
170
+    }
171
+
172
+    /**
173
+     * Wraps all field contents with potential additional tags.
174
+     *
175
+     * @param  Field $field
176
+     *
177
+     * @return Field A wrapped field
178
+     */
179
+    public function wrapField($field)
180
+    {
181
+        return $field;
182
+    }
183
+
184
+    /**
185
+     * Wrap actions block with potential additional tags
186
+     *
187
+     * @param  Actions $actions
188
+     *
189
+     * @return string A wrapped actions block
190
+     */
191
+    public function wrapActions($actions)
192
+    {
193
+        return $actions;
194
+    }
195 195
 }
Please login to merge, or discard this patch.
src/Former/Framework/TwitterBootstrap.php 1 patch
Indentation   +370 added lines, -370 removed lines patch added patch discarded remove patch
@@ -13,374 +13,374 @@
 block discarded – undo
13 13
  */
14 14
 class TwitterBootstrap 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', 'search');
22
-
23
-	/**
24
-	 * The button types available
25
-	 *
26
-	 * @var array
27
-	 */
28
-	private $buttons = array(
29
-		'large',
30
-		'small',
31
-		'mini',
32
-		'block',
33
-		'danger',
34
-		'info',
35
-		'inverse',
36
-		'link',
37
-		'primary',
38
-		'success',
39
-		'warning',
40
-	);
41
-
42
-	/**
43
-	 * The field sizes available
44
-	 *
45
-	 * @var array
46
-	 */
47
-	private $fields = array(
48
-		'mini',
49
-		'small',
50
-		'medium',
51
-		'large',
52
-		'xlarge',
53
-		'xxlarge',
54
-		'span1',
55
-		'span2',
56
-		'span3',
57
-		'span4',
58
-		'span5',
59
-		'span6',
60
-		'span7',
61
-		'span8',
62
-		'span9',
63
-		'span10',
64
-		'span11',
65
-		'span12',
66
-	);
67
-
68
-	/**
69
-	 * The field states available
70
-	 *
71
-	 * @var array
72
-	 */
73
-	protected $states = array(
74
-		'success',
75
-		'warning',
76
-		'error',
77
-		'info',
78
-	);
79
-
80
-	/**
81
-	 * Create a new TwitterBootstrap instance
82
-	 *
83
-	 * @param \Illuminate\Container\Container $app
84
-	 */
85
-	public function __construct(Container $app)
86
-	{
87
-		$this->app = $app;
88
-		$this->setFrameworkDefaults();
89
-	}
90
-
91
-	////////////////////////////////////////////////////////////////////
92
-	/////////////////////////// FILTER ARRAYS //////////////////////////
93
-	////////////////////////////////////////////////////////////////////
94
-
95
-	/**
96
-	 * Filter buttons classes
97
-	 *
98
-	 * @param  array $classes An array of classes
99
-	 *
100
-	 * @return string[] A filtered array
101
-	 */
102
-	public function filterButtonClasses($classes)
103
-	{
104
-		// Filter classes
105
-		// $classes = array_intersect($classes, $this->buttons);
106
-
107
-		// Prepend button type
108
-		$classes   = $this->prependWith($classes, 'btn-');
109
-		$classes[] = 'btn';
110
-
111
-		return $classes;
112
-	}
113
-
114
-	/**
115
-	 * Filter field classes
116
-	 *
117
-	 * @param  array $classes An array of classes
118
-	 *
119
-	 * @return array A filtered array
120
-	 */
121
-	public function filterFieldClasses($classes)
122
-	{
123
-		// Filter classes
124
-		$classes = array_intersect($classes, $this->fields);
125
-
126
-		// Prepend field type
127
-		$classes = array_map(function($class) {
128
-			return Str::startsWith($class, 'span') ? $class : 'input-'.$class;
129
-		}, $classes);
130
-
131
-		return $classes;
132
-	}
133
-
134
-	////////////////////////////////////////////////////////////////////
135
-	///////////////////////////// ADD CLASSES //////////////////////////
136
-	////////////////////////////////////////////////////////////////////
137
-
138
-	/**
139
-	 * Add classes to a field
140
-	 *
141
-	 * @param Field $field
142
-	 * @param array $classes The possible classes to add
143
-	 *
144
-	 * @return Field
145
-	 */
146
-	public function getFieldClasses(Field $field, $classes)
147
-	{
148
-		// Add inline class for checkables
149
-		if ($field->isCheckable() and in_array('inline', $classes)) {
150
-			$field->inline();
151
-		}
152
-
153
-		// Filter classes according to field type
154
-		if ($field->isButton()) {
155
-			$classes = $this->filterButtonClasses($classes);
156
-		} else {
157
-			$classes = $this->filterFieldClasses($classes);
158
-		}
159
-
160
-		return $this->addClassesToField($field, $classes);
161
-	}
162
-
163
-	/**
164
-	 * Add group classes
165
-	 *
166
-	 * @return string A list of group classes
167
-	 */
168
-	public function getGroupClasses()
169
-	{
170
-		return 'control-group';
171
-	}
172
-
173
-	/**
174
-	 * Add label classes
175
-	 *
176
-	 * @return string An array of attributes with the label class
177
-	 */
178
-	public function getLabelClasses()
179
-	{
180
-		return 'control-label';
181
-	}
182
-
183
-	/**
184
-	 * Add uneditable field classes
185
-	 *
186
-	 * @return string An array of attributes with the uneditable class
187
-	 */
188
-	public function getUneditableClasses()
189
-	{
190
-		return 'uneditable-input';
191
-	}
192
-
193
-	public function getPlainTextClasses()
194
-	{
195
-		return null;
196
-	}
197
-
198
-	/**
199
-	 * Add form class
200
-	 *
201
-	 * @param  string $type The type of form to add
202
-	 *
203
-	 * @return string|null
204
-	 */
205
-	public function getFormClasses($type)
206
-	{
207
-		return $type ? 'form-'.$type : null;
208
-	}
209
-
210
-	/**
211
-	 * Add actions block class
212
-	 *
213
-	 * @return string
214
-	 */
215
-	public function getActionClasses()
216
-	{
217
-		return 'form-actions';
218
-	}
219
-
220
-	////////////////////////////////////////////////////////////////////
221
-	//////////////////////////// RENDER BLOCKS /////////////////////////
222
-	////////////////////////////////////////////////////////////////////
223
-
224
-	/**
225
-	 * Render an help text
226
-	 *
227
-	 * @param string $text
228
-	 * @param array  $attributes
229
-	 *
230
-	 * @return Element
231
-	 */
232
-	public function createHelp($text, $attributes = array())
233
-	{
234
-		return Element::create('span', $text, $attributes)->addClass('help-inline');
235
-	}
236
-
237
-	/**
238
-	 * Render a block help text
239
-	 *
240
-	 * @param string $text
241
-	 * @param array  $attributes
242
-	 *
243
-	 * @return Element
244
-	 */
245
-	public function createBlockHelp($text, $attributes = array())
246
-	{
247
-		return Element::create('p', $text, $attributes)->addClass('help-block');
248
-	}
249
-
250
-	/**
251
-	 * Render a disabled field
252
-	 *
253
-	 * @param Field $field
254
-	 *
255
-	 * @return Element
256
-	 */
257
-	public function createDisabledField(Field $field)
258
-	{
259
-		return Element::create('span', $field->getValue(), $field->getAttributes());
260
-	}
261
-
262
-	/**
263
-	 * Render a plain text field
264
-	 * Which fallback to a disabled field
265
-	 *
266
-	 * @param Field $field
267
-	 *
268
-	 * @return Element
269
-	 */
270
-	public function createPlainTextField(Field $field)
271
-	{
272
-		return $this->createDisabledField($field);
273
-	}
274
-
275
-	/**
276
-	 * Render an icon
277
-	 *
278
-	 * @param array $attributes Its general attributes
279
-	 *
280
-	 * @return string
281
-	 */
282
-	public function createIcon($iconType, $attributes = array(), $iconSettings = array())
283
-	{
284
-		// Check for empty icons
285
-		if (!$iconType) {
286
-			return false;
287
-		}
288
-
289
-		// Create tag
290
-		$tag  = array_get($iconSettings, 'tag', $this->iconTag);
291
-		$icon = Element::create($tag, null, $attributes);
292
-
293
-		// White icons ignore user overrides to use legacy Bootstrap styling
294
-		if (Str::contains($iconType, 'white')) {
295
-			$iconType = str_replace('white', '', $iconType);
296
-			$iconType = trim($iconType, '-');
297
-			$icon->addClass('icon-white');
298
-			$set    = null;
299
-			$prefix = 'icon';
300
-		} else {
301
-			$set    = array_get($iconSettings, 'set', $this->iconSet);
302
-			$prefix = array_get($iconSettings, 'prefix', $this->iconPrefix);
303
-		}
304
-		$icon->addClass("$set $prefix-$iconType");
305
-
306
-		return $icon;
307
-	}
308
-
309
-	////////////////////////////////////////////////////////////////////
310
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
311
-	////////////////////////////////////////////////////////////////////
312
-
313
-	/**
314
-	 * Wrap an item to be prepended or appended to the current field
315
-	 *
316
-	 * @param  string $item
317
-	 *
318
-	 * @return string A wrapped item
319
-	 */
320
-	public function placeAround($item)
321
-	{
322
-		// Render object
323
-		if (is_object($item) and method_exists($item, '__toString')) {
324
-			$item = $item->__toString();
325
-		}
326
-
327
-		// Return unwrapped if button
328
-		if (strpos($item, '<button') !== false) {
329
-			return $item;
330
-		}
331
-
332
-		return Element::create('span', $item)->addClass('add-on');
333
-	}
334
-
335
-	/**
336
-	 * Wrap a field with prepended and appended items
337
-	 *
338
-	 * @param  Field $field
339
-	 * @param  array $prepend
340
-	 * @param  array $append
341
-	 *
342
-	 * @return string A field concatented with prepended and/or appended items
343
-	 */
344
-	public function prependAppend($field, $prepend, $append)
345
-	{
346
-		$class = array();
347
-		if ($prepend) {
348
-			$class[] = 'input-prepend';
349
-		}
350
-		if ($append) {
351
-			$class[] = 'input-append';
352
-		}
353
-
354
-		$return = '<div class="'.join(' ', $class).'">';
355
-		$return .= join(null, $prepend);
356
-		$return .= $field->render();
357
-		$return .= join(null, $append);
358
-		$return .= '</div>';
359
-
360
-		return $return;
361
-	}
362
-
363
-	/**
364
-	 * Wrap a field with potential additional tags
365
-	 *
366
-	 * @param  Field $field
367
-	 *
368
-	 * @return Element A wrapped field
369
-	 */
370
-	public function wrapField($field)
371
-	{
372
-		return Element::create('div', $field)->addClass('controls');
373
-	}
374
-
375
-	/**
376
-	 * Wrap actions block with potential additional tags
377
-	 *
378
-	 * @param  Actions $actions
379
-	 *
380
-	 * @return string A wrapped actions block
381
-	 */
382
-	public function wrapActions($actions)
383
-	{
384
-		return $actions;
385
-	}
16
+    /**
17
+     * Form types that trigger special styling for this Framework
18
+     *
19
+     * @var array
20
+     */
21
+    protected $availableTypes = array('horizontal', 'vertical', 'inline', 'search');
22
+
23
+    /**
24
+     * The button types available
25
+     *
26
+     * @var array
27
+     */
28
+    private $buttons = array(
29
+        'large',
30
+        'small',
31
+        'mini',
32
+        'block',
33
+        'danger',
34
+        'info',
35
+        'inverse',
36
+        'link',
37
+        'primary',
38
+        'success',
39
+        'warning',
40
+    );
41
+
42
+    /**
43
+     * The field sizes available
44
+     *
45
+     * @var array
46
+     */
47
+    private $fields = array(
48
+        'mini',
49
+        'small',
50
+        'medium',
51
+        'large',
52
+        'xlarge',
53
+        'xxlarge',
54
+        'span1',
55
+        'span2',
56
+        'span3',
57
+        'span4',
58
+        'span5',
59
+        'span6',
60
+        'span7',
61
+        'span8',
62
+        'span9',
63
+        'span10',
64
+        'span11',
65
+        'span12',
66
+    );
67
+
68
+    /**
69
+     * The field states available
70
+     *
71
+     * @var array
72
+     */
73
+    protected $states = array(
74
+        'success',
75
+        'warning',
76
+        'error',
77
+        'info',
78
+    );
79
+
80
+    /**
81
+     * Create a new TwitterBootstrap instance
82
+     *
83
+     * @param \Illuminate\Container\Container $app
84
+     */
85
+    public function __construct(Container $app)
86
+    {
87
+        $this->app = $app;
88
+        $this->setFrameworkDefaults();
89
+    }
90
+
91
+    ////////////////////////////////////////////////////////////////////
92
+    /////////////////////////// FILTER ARRAYS //////////////////////////
93
+    ////////////////////////////////////////////////////////////////////
94
+
95
+    /**
96
+     * Filter buttons classes
97
+     *
98
+     * @param  array $classes An array of classes
99
+     *
100
+     * @return string[] A filtered array
101
+     */
102
+    public function filterButtonClasses($classes)
103
+    {
104
+        // Filter classes
105
+        // $classes = array_intersect($classes, $this->buttons);
106
+
107
+        // Prepend button type
108
+        $classes   = $this->prependWith($classes, 'btn-');
109
+        $classes[] = 'btn';
110
+
111
+        return $classes;
112
+    }
113
+
114
+    /**
115
+     * Filter field classes
116
+     *
117
+     * @param  array $classes An array of classes
118
+     *
119
+     * @return array A filtered array
120
+     */
121
+    public function filterFieldClasses($classes)
122
+    {
123
+        // Filter classes
124
+        $classes = array_intersect($classes, $this->fields);
125
+
126
+        // Prepend field type
127
+        $classes = array_map(function($class) {
128
+            return Str::startsWith($class, 'span') ? $class : 'input-'.$class;
129
+        }, $classes);
130
+
131
+        return $classes;
132
+    }
133
+
134
+    ////////////////////////////////////////////////////////////////////
135
+    ///////////////////////////// ADD CLASSES //////////////////////////
136
+    ////////////////////////////////////////////////////////////////////
137
+
138
+    /**
139
+     * Add classes to a field
140
+     *
141
+     * @param Field $field
142
+     * @param array $classes The possible classes to add
143
+     *
144
+     * @return Field
145
+     */
146
+    public function getFieldClasses(Field $field, $classes)
147
+    {
148
+        // Add inline class for checkables
149
+        if ($field->isCheckable() and in_array('inline', $classes)) {
150
+            $field->inline();
151
+        }
152
+
153
+        // Filter classes according to field type
154
+        if ($field->isButton()) {
155
+            $classes = $this->filterButtonClasses($classes);
156
+        } else {
157
+            $classes = $this->filterFieldClasses($classes);
158
+        }
159
+
160
+        return $this->addClassesToField($field, $classes);
161
+    }
162
+
163
+    /**
164
+     * Add group classes
165
+     *
166
+     * @return string A list of group classes
167
+     */
168
+    public function getGroupClasses()
169
+    {
170
+        return 'control-group';
171
+    }
172
+
173
+    /**
174
+     * Add label classes
175
+     *
176
+     * @return string An array of attributes with the label class
177
+     */
178
+    public function getLabelClasses()
179
+    {
180
+        return 'control-label';
181
+    }
182
+
183
+    /**
184
+     * Add uneditable field classes
185
+     *
186
+     * @return string An array of attributes with the uneditable class
187
+     */
188
+    public function getUneditableClasses()
189
+    {
190
+        return 'uneditable-input';
191
+    }
192
+
193
+    public function getPlainTextClasses()
194
+    {
195
+        return null;
196
+    }
197
+
198
+    /**
199
+     * Add form class
200
+     *
201
+     * @param  string $type The type of form to add
202
+     *
203
+     * @return string|null
204
+     */
205
+    public function getFormClasses($type)
206
+    {
207
+        return $type ? 'form-'.$type : null;
208
+    }
209
+
210
+    /**
211
+     * Add actions block class
212
+     *
213
+     * @return string
214
+     */
215
+    public function getActionClasses()
216
+    {
217
+        return 'form-actions';
218
+    }
219
+
220
+    ////////////////////////////////////////////////////////////////////
221
+    //////////////////////////// RENDER BLOCKS /////////////////////////
222
+    ////////////////////////////////////////////////////////////////////
223
+
224
+    /**
225
+     * Render an help text
226
+     *
227
+     * @param string $text
228
+     * @param array  $attributes
229
+     *
230
+     * @return Element
231
+     */
232
+    public function createHelp($text, $attributes = array())
233
+    {
234
+        return Element::create('span', $text, $attributes)->addClass('help-inline');
235
+    }
236
+
237
+    /**
238
+     * Render a block help text
239
+     *
240
+     * @param string $text
241
+     * @param array  $attributes
242
+     *
243
+     * @return Element
244
+     */
245
+    public function createBlockHelp($text, $attributes = array())
246
+    {
247
+        return Element::create('p', $text, $attributes)->addClass('help-block');
248
+    }
249
+
250
+    /**
251
+     * Render a disabled field
252
+     *
253
+     * @param Field $field
254
+     *
255
+     * @return Element
256
+     */
257
+    public function createDisabledField(Field $field)
258
+    {
259
+        return Element::create('span', $field->getValue(), $field->getAttributes());
260
+    }
261
+
262
+    /**
263
+     * Render a plain text field
264
+     * Which fallback to a disabled field
265
+     *
266
+     * @param Field $field
267
+     *
268
+     * @return Element
269
+     */
270
+    public function createPlainTextField(Field $field)
271
+    {
272
+        return $this->createDisabledField($field);
273
+    }
274
+
275
+    /**
276
+     * Render an icon
277
+     *
278
+     * @param array $attributes Its general attributes
279
+     *
280
+     * @return string
281
+     */
282
+    public function createIcon($iconType, $attributes = array(), $iconSettings = array())
283
+    {
284
+        // Check for empty icons
285
+        if (!$iconType) {
286
+            return false;
287
+        }
288
+
289
+        // Create tag
290
+        $tag  = array_get($iconSettings, 'tag', $this->iconTag);
291
+        $icon = Element::create($tag, null, $attributes);
292
+
293
+        // White icons ignore user overrides to use legacy Bootstrap styling
294
+        if (Str::contains($iconType, 'white')) {
295
+            $iconType = str_replace('white', '', $iconType);
296
+            $iconType = trim($iconType, '-');
297
+            $icon->addClass('icon-white');
298
+            $set    = null;
299
+            $prefix = 'icon';
300
+        } else {
301
+            $set    = array_get($iconSettings, 'set', $this->iconSet);
302
+            $prefix = array_get($iconSettings, 'prefix', $this->iconPrefix);
303
+        }
304
+        $icon->addClass("$set $prefix-$iconType");
305
+
306
+        return $icon;
307
+    }
308
+
309
+    ////////////////////////////////////////////////////////////////////
310
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
311
+    ////////////////////////////////////////////////////////////////////
312
+
313
+    /**
314
+     * Wrap an item to be prepended or appended to the current field
315
+     *
316
+     * @param  string $item
317
+     *
318
+     * @return string A wrapped item
319
+     */
320
+    public function placeAround($item)
321
+    {
322
+        // Render object
323
+        if (is_object($item) and method_exists($item, '__toString')) {
324
+            $item = $item->__toString();
325
+        }
326
+
327
+        // Return unwrapped if button
328
+        if (strpos($item, '<button') !== false) {
329
+            return $item;
330
+        }
331
+
332
+        return Element::create('span', $item)->addClass('add-on');
333
+    }
334
+
335
+    /**
336
+     * Wrap a field with prepended and appended items
337
+     *
338
+     * @param  Field $field
339
+     * @param  array $prepend
340
+     * @param  array $append
341
+     *
342
+     * @return string A field concatented with prepended and/or appended items
343
+     */
344
+    public function prependAppend($field, $prepend, $append)
345
+    {
346
+        $class = array();
347
+        if ($prepend) {
348
+            $class[] = 'input-prepend';
349
+        }
350
+        if ($append) {
351
+            $class[] = 'input-append';
352
+        }
353
+
354
+        $return = '<div class="'.join(' ', $class).'">';
355
+        $return .= join(null, $prepend);
356
+        $return .= $field->render();
357
+        $return .= join(null, $append);
358
+        $return .= '</div>';
359
+
360
+        return $return;
361
+    }
362
+
363
+    /**
364
+     * Wrap a field with potential additional tags
365
+     *
366
+     * @param  Field $field
367
+     *
368
+     * @return Element A wrapped field
369
+     */
370
+    public function wrapField($field)
371
+    {
372
+        return Element::create('div', $field)->addClass('controls');
373
+    }
374
+
375
+    /**
376
+     * Wrap actions block with potential additional tags
377
+     *
378
+     * @param  Actions $actions
379
+     *
380
+     * @return string A wrapped actions block
381
+     */
382
+    public function wrapActions($actions)
383
+    {
384
+        return $actions;
385
+    }
386 386
 }
Please login to merge, or discard this patch.
src/Former/Framework/ZurbFoundation5.php 1 patch
Indentation   +279 added lines, -279 removed lines patch added patch discarded remove patch
@@ -13,283 +13,283 @@
 block discarded – undo
13 13
  */
14 14
 class ZurbFoundation5 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');
22
-
23
-	/**
24
-	 * The button types available
25
-	 *
26
-	 * @var array
27
-	 */
28
-	private $buttons = array(
29
-		'tiny',
30
-		'small',
31
-		'medium',
32
-		'large',
33
-		'success',
34
-		'radius',
35
-		'round',
36
-		'disabled',
37
-		'prefix',
38
-		'postfix',
39
-	);
40
-
41
-	/**
42
-	 * The field sizes available
43
-	 * Zurb Foundation 5 does not apply sizes to the form element, but to the wrapper div
44
-	 *
45
-	 * @var array
46
-	 */
47
-	private $fields = array();
48
-
49
-	/**
50
-	 * The field states available
51
-	 *
52
-	 * @var array
53
-	 */
54
-	protected $states = array(
55
-		'error',
56
-	);
57
-
58
-	/**
59
-	 * Create a new ZurbFoundation instance
60
-	 *
61
-	 * @param \Illuminate\Container\Container $app
62
-	 */
63
-	public function __construct(Container $app)
64
-	{
65
-		$this->app = $app;
66
-		$this->setFrameworkDefaults();
67
-	}
68
-
69
-	////////////////////////////////////////////////////////////////////
70
-	/////////////////////////// FILTER ARRAYS //////////////////////////
71
-	////////////////////////////////////////////////////////////////////
72
-
73
-	public function filterButtonClasses($classes)
74
-	{
75
-		// Filter classes
76
-		$classes   = array_intersect($classes, $this->buttons);
77
-		$classes[] = 'button';
78
-
79
-		return $classes;
80
-	}
81
-
82
-	public function filterFieldClasses($classes)
83
-	{
84
-		return null;
85
-	}
86
-
87
-	////////////////////////////////////////////////////////////////////
88
-	///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
89
-	////////////////////////////////////////////////////////////////////
90
-
91
-	protected function setFieldWidths($labelWidths)
92
-	{
93
-		$labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
94
-
95
-		$viewports = $this->getFrameworkOption('viewports');
96
-
97
-		foreach ($labelWidths as $viewport => $columns) {
98
-			if ($viewport) {
99
-				$labelWidthClass .= $viewports[$viewport].'-'.$columns.' ';
100
-				$fieldWidthClass .= $viewports[$viewport].'-'.(12 - $columns).' ';
101
-				$fieldOffsetClass .= $viewports[$viewport].'-offset-'.$columns.' ';
102
-			}
103
-		}
104
-
105
-		$this->labelWidth  = $labelWidthClass.'columns';
106
-		$this->fieldWidth  = $fieldWidthClass.'columns';
107
-		$this->fieldOffset = $fieldOffsetClass.'columns';
108
-	}
109
-
110
-	////////////////////////////////////////////////////////////////////
111
-	///////////////////////////// ADD CLASSES //////////////////////////
112
-	////////////////////////////////////////////////////////////////////
113
-
114
-	public function getFieldClasses(Field $field, $classes = array())
115
-	{
116
-		if ($field->isButton()) {
117
-			$classes = $this->filterButtonClasses($classes);
118
-		} else {
119
-			$classes = $this->filterFieldClasses($classes);
120
-		}
121
-
122
-		return $this->addClassesToField($field, $classes);
123
-	}
124
-
125
-	public function getGroupClasses()
126
-	{
127
-		if ($this->app['former.form']->isOfType('horizontal')) {
128
-			return 'row';
129
-		} else {
130
-			return null;
131
-		}
132
-	}
133
-
134
-	/**
135
-	 * Add label classes
136
-	 *
137
-	 * @return string|null An array of attributes with the label class
138
-	 */
139
-	public function getLabelClasses()
140
-	{
141
-		if ($this->app['former.form']->isOfType('horizontal')) {
142
-			return $this->getFrameworkOption('wrappedLabelClasses');
143
-		} else {
144
-			return null;
145
-		}
146
-	}
147
-
148
-	public function getUneditableClasses()
149
-	{
150
-		return null;
151
-	}
152
-
153
-	public function getPlainTextClasses()
154
-	{
155
-		return null;
156
-	}
157
-
158
-	public function getFormClasses($type)
159
-	{
160
-		return null;
161
-	}
162
-
163
-	public function getActionClasses()
164
-	{
165
-		return null;
166
-	}
167
-
168
-	////////////////////////////////////////////////////////////////////
169
-	//////////////////////////// RENDER BLOCKS /////////////////////////
170
-	////////////////////////////////////////////////////////////////////
171
-
172
-	public function createHelp($text, $attributes = null)
173
-	{
174
-		if (is_null($attributes) or empty($attributes)) {
175
-			$attributes = $this->getFrameworkOption('error_classes');
176
-		}
177
-
178
-		return Element::create('small', $text, $attributes);
179
-	}
180
-
181
-	/**
182
-	 * Render a disabled field
183
-	 *
184
-	 * @param Field $field
185
-	 *
186
-	 * @return Input
187
-	 */
188
-	public function createDisabledField(Field $field)
189
-	{
190
-		$field->disabled();
191
-
192
-		return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
193
-	}
194
-
195
-	/**
196
-	 * Render a plain text field
197
-	 * Which fallback to a disabled field
198
-	 *
199
-	 * @param Field $field
200
-	 *
201
-	 * @return Input
202
-	 */
203
-	public function createPlainTextField(Field $field)
204
-	{
205
-		return $this->createDisabledField($field);
206
-	}
207
-
208
-	////////////////////////////////////////////////////////////////////
209
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
210
-	////////////////////////////////////////////////////////////////////
211
-
212
-	/**
213
-	 * Wrap an item to be prepended or appended to the current field.
214
-	 * For Zurb we return the item and handle the wrapping in prependAppend
215
-	 * as wrapping is dependent on whether we're prepending or appending.
216
-	 *
217
-	 * @return string A wrapped item
218
-	 */
219
-	public function placeAround($item)
220
-	{
221
-		return $item;
222
-	}
223
-
224
-	/**
225
-	 * Wrap a field with prepended and appended items
226
-	 *
227
-	 * @param  Field $field
228
-	 * @param  array $prepend
229
-	 * @param  array $append
230
-	 *
231
-	 * @return string A field concatented with prepended and/or appended items
232
-	 */
233
-	public function prependAppend($field, $prepend, $append)
234
-	{
235
-		$return = '<div class="row collapse">';
236
-
237
-		foreach ($prepend as $item) {
238
-			$return .= '<div class="large-2 small-3 columns"><span class="prefix">'.$item.'</span></div>';
239
-		}
240
-
241
-		$return .= '<div class="large-10 small-9 columns">'.$field->render().'</div>';
242
-
243
-		foreach ($append as $item) {
244
-			$return .= '<div class="large-2 small-3 columns"><span class="postfix">'.$item.'</span></div>';
245
-		}
246
-
247
-		$return .= '</div>';
248
-
249
-		return $return;
250
-	}
251
-
252
-	/**
253
-	 * Wraps all label contents with potential additional tags.
254
-	 *
255
-	 * @param  string $label
256
-	 *
257
-	 * @return string A wrapped label
258
-	 */
259
-	public function wrapLabel($label)
260
-	{
261
-		if ($this->app['former.form']->isOfType('horizontal')) {
262
-			return Element::create('div', $label)->addClass($this->labelWidth);
263
-		} else {
264
-			return $label;
265
-		}
266
-	}
267
-
268
-	/**
269
-	 * Wraps all field contents with potential additional tags.
270
-	 *
271
-	 * @param  Field $field
272
-	 *
273
-	 * @return Element A wrapped field
274
-	 */
275
-	public function wrapField($field)
276
-	{
277
-		if ($this->app['former.form']->isOfType('horizontal')) {
278
-			return Element::create('div', $field)->addClass($this->fieldWidth);
279
-		} else {
280
-			return $field;
281
-		}
282
-	}
283
-
284
-	/**
285
-	 * Wrap actions block with potential additional tags
286
-	 *
287
-	 * @param  Actions $actions
288
-	 *
289
-	 * @return string A wrapped actions block
290
-	 */
291
-	public function wrapActions($actions)
292
-	{
293
-		return $actions;
294
-	}
16
+    /**
17
+     * Form types that trigger special styling for this Framework
18
+     *
19
+     * @var array
20
+     */
21
+    protected $availableTypes = array('horizontal', 'vertical');
22
+
23
+    /**
24
+     * The button types available
25
+     *
26
+     * @var array
27
+     */
28
+    private $buttons = array(
29
+        'tiny',
30
+        'small',
31
+        'medium',
32
+        'large',
33
+        'success',
34
+        'radius',
35
+        'round',
36
+        'disabled',
37
+        'prefix',
38
+        'postfix',
39
+    );
40
+
41
+    /**
42
+     * The field sizes available
43
+     * Zurb Foundation 5 does not apply sizes to the form element, but to the wrapper div
44
+     *
45
+     * @var array
46
+     */
47
+    private $fields = array();
48
+
49
+    /**
50
+     * The field states available
51
+     *
52
+     * @var array
53
+     */
54
+    protected $states = array(
55
+        'error',
56
+    );
57
+
58
+    /**
59
+     * Create a new ZurbFoundation instance
60
+     *
61
+     * @param \Illuminate\Container\Container $app
62
+     */
63
+    public function __construct(Container $app)
64
+    {
65
+        $this->app = $app;
66
+        $this->setFrameworkDefaults();
67
+    }
68
+
69
+    ////////////////////////////////////////////////////////////////////
70
+    /////////////////////////// FILTER ARRAYS //////////////////////////
71
+    ////////////////////////////////////////////////////////////////////
72
+
73
+    public function filterButtonClasses($classes)
74
+    {
75
+        // Filter classes
76
+        $classes   = array_intersect($classes, $this->buttons);
77
+        $classes[] = 'button';
78
+
79
+        return $classes;
80
+    }
81
+
82
+    public function filterFieldClasses($classes)
83
+    {
84
+        return null;
85
+    }
86
+
87
+    ////////////////////////////////////////////////////////////////////
88
+    ///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
89
+    ////////////////////////////////////////////////////////////////////
90
+
91
+    protected function setFieldWidths($labelWidths)
92
+    {
93
+        $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
94
+
95
+        $viewports = $this->getFrameworkOption('viewports');
96
+
97
+        foreach ($labelWidths as $viewport => $columns) {
98
+            if ($viewport) {
99
+                $labelWidthClass .= $viewports[$viewport].'-'.$columns.' ';
100
+                $fieldWidthClass .= $viewports[$viewport].'-'.(12 - $columns).' ';
101
+                $fieldOffsetClass .= $viewports[$viewport].'-offset-'.$columns.' ';
102
+            }
103
+        }
104
+
105
+        $this->labelWidth  = $labelWidthClass.'columns';
106
+        $this->fieldWidth  = $fieldWidthClass.'columns';
107
+        $this->fieldOffset = $fieldOffsetClass.'columns';
108
+    }
109
+
110
+    ////////////////////////////////////////////////////////////////////
111
+    ///////////////////////////// ADD CLASSES //////////////////////////
112
+    ////////////////////////////////////////////////////////////////////
113
+
114
+    public function getFieldClasses(Field $field, $classes = array())
115
+    {
116
+        if ($field->isButton()) {
117
+            $classes = $this->filterButtonClasses($classes);
118
+        } else {
119
+            $classes = $this->filterFieldClasses($classes);
120
+        }
121
+
122
+        return $this->addClassesToField($field, $classes);
123
+    }
124
+
125
+    public function getGroupClasses()
126
+    {
127
+        if ($this->app['former.form']->isOfType('horizontal')) {
128
+            return 'row';
129
+        } else {
130
+            return null;
131
+        }
132
+    }
133
+
134
+    /**
135
+     * Add label classes
136
+     *
137
+     * @return string|null An array of attributes with the label class
138
+     */
139
+    public function getLabelClasses()
140
+    {
141
+        if ($this->app['former.form']->isOfType('horizontal')) {
142
+            return $this->getFrameworkOption('wrappedLabelClasses');
143
+        } else {
144
+            return null;
145
+        }
146
+    }
147
+
148
+    public function getUneditableClasses()
149
+    {
150
+        return null;
151
+    }
152
+
153
+    public function getPlainTextClasses()
154
+    {
155
+        return null;
156
+    }
157
+
158
+    public function getFormClasses($type)
159
+    {
160
+        return null;
161
+    }
162
+
163
+    public function getActionClasses()
164
+    {
165
+        return null;
166
+    }
167
+
168
+    ////////////////////////////////////////////////////////////////////
169
+    //////////////////////////// RENDER BLOCKS /////////////////////////
170
+    ////////////////////////////////////////////////////////////////////
171
+
172
+    public function createHelp($text, $attributes = null)
173
+    {
174
+        if (is_null($attributes) or empty($attributes)) {
175
+            $attributes = $this->getFrameworkOption('error_classes');
176
+        }
177
+
178
+        return Element::create('small', $text, $attributes);
179
+    }
180
+
181
+    /**
182
+     * Render a disabled field
183
+     *
184
+     * @param Field $field
185
+     *
186
+     * @return Input
187
+     */
188
+    public function createDisabledField(Field $field)
189
+    {
190
+        $field->disabled();
191
+
192
+        return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
193
+    }
194
+
195
+    /**
196
+     * Render a plain text field
197
+     * Which fallback to a disabled field
198
+     *
199
+     * @param Field $field
200
+     *
201
+     * @return Input
202
+     */
203
+    public function createPlainTextField(Field $field)
204
+    {
205
+        return $this->createDisabledField($field);
206
+    }
207
+
208
+    ////////////////////////////////////////////////////////////////////
209
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
210
+    ////////////////////////////////////////////////////////////////////
211
+
212
+    /**
213
+     * Wrap an item to be prepended or appended to the current field.
214
+     * For Zurb we return the item and handle the wrapping in prependAppend
215
+     * as wrapping is dependent on whether we're prepending or appending.
216
+     *
217
+     * @return string A wrapped item
218
+     */
219
+    public function placeAround($item)
220
+    {
221
+        return $item;
222
+    }
223
+
224
+    /**
225
+     * Wrap a field with prepended and appended items
226
+     *
227
+     * @param  Field $field
228
+     * @param  array $prepend
229
+     * @param  array $append
230
+     *
231
+     * @return string A field concatented with prepended and/or appended items
232
+     */
233
+    public function prependAppend($field, $prepend, $append)
234
+    {
235
+        $return = '<div class="row collapse">';
236
+
237
+        foreach ($prepend as $item) {
238
+            $return .= '<div class="large-2 small-3 columns"><span class="prefix">'.$item.'</span></div>';
239
+        }
240
+
241
+        $return .= '<div class="large-10 small-9 columns">'.$field->render().'</div>';
242
+
243
+        foreach ($append as $item) {
244
+            $return .= '<div class="large-2 small-3 columns"><span class="postfix">'.$item.'</span></div>';
245
+        }
246
+
247
+        $return .= '</div>';
248
+
249
+        return $return;
250
+    }
251
+
252
+    /**
253
+     * Wraps all label contents with potential additional tags.
254
+     *
255
+     * @param  string $label
256
+     *
257
+     * @return string A wrapped label
258
+     */
259
+    public function wrapLabel($label)
260
+    {
261
+        if ($this->app['former.form']->isOfType('horizontal')) {
262
+            return Element::create('div', $label)->addClass($this->labelWidth);
263
+        } else {
264
+            return $label;
265
+        }
266
+    }
267
+
268
+    /**
269
+     * Wraps all field contents with potential additional tags.
270
+     *
271
+     * @param  Field $field
272
+     *
273
+     * @return Element A wrapped field
274
+     */
275
+    public function wrapField($field)
276
+    {
277
+        if ($this->app['former.form']->isOfType('horizontal')) {
278
+            return Element::create('div', $field)->addClass($this->fieldWidth);
279
+        } else {
280
+            return $field;
281
+        }
282
+    }
283
+
284
+    /**
285
+     * Wrap actions block with potential additional tags
286
+     *
287
+     * @param  Actions $actions
288
+     *
289
+     * @return string A wrapped actions block
290
+     */
291
+    public function wrapActions($actions)
292
+    {
293
+        return $actions;
294
+    }
295 295
 }
Please login to merge, or discard this patch.
src/Former/Framework/TwitterBootstrap3.php 1 patch
Indentation   +433 added lines, -433 removed lines patch added patch discarded remove patch
@@ -13,437 +13,437 @@
 block discarded – undo
13 13
  */
14 14
 class TwitterBootstrap3 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
-		'default',
35
-		'primary',
36
-		'warning',
37
-		'danger',
38
-		'success',
39
-		'info',
40
-	);
41
-
42
-	/**
43
-	 * The field sizes available
44
-	 *
45
-	 * @var array
46
-	 */
47
-	private $fields = array(
48
-		'lg',
49
-		'sm',
50
-		// 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6',
51
-		// 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12',
52
-		// 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6',
53
-		// 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12',
54
-		// 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6',
55
-		// 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12',
56
-		// 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6',
57
-		// 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12',
58
-	);
59
-
60
-	/**
61
-	 * The field states available
62
-	 *
63
-	 * @var array
64
-	 */
65
-	protected $states = array(
66
-		'has-warning',
67
-		'has-error',
68
-		'has-success',
69
-	);
70
-
71
-	/**
72
-	 * The default HTML tag used for icons
73
-	 *
74
-	 * @var string
75
-	 */
76
-	protected $iconTag = 'span';
77
-
78
-	/**
79
-	 * The default set for icon fonts
80
-	 * By default Bootstrap 3 offers only 'glyphicon'
81
-	 * See Former docs to use 'social' and 'filetypes' sets for specific icons.
82
-	 *
83
-	 * @var string
84
-	 */
85
-	protected $iconSet = 'glyphicon';
86
-
87
-	/**
88
-	 * The default prefix icon names
89
-	 * "icon" works for Bootstrap 2 and Font-awesome
90
-	 *
91
-	 * @var string
92
-	 */
93
-	protected $iconPrefix = 'glyphicon';
94
-
95
-	/**
96
-	 * Create a new TwitterBootstrap instance
97
-	 *
98
-	 * @param \Illuminate\Container\Container $app
99
-	 */
100
-	public function __construct(Container $app)
101
-	{
102
-		$this->app = $app;
103
-		$this->setFrameworkDefaults();
104
-	}
105
-
106
-	////////////////////////////////////////////////////////////////////
107
-	/////////////////////////// FILTER ARRAYS //////////////////////////
108
-	////////////////////////////////////////////////////////////////////
109
-
110
-	/**
111
-	 * Filter buttons classes
112
-	 *
113
-	 * @param  array $classes An array of classes
114
-	 *
115
-	 * @return string[] A filtered array
116
-	 */
117
-	public function filterButtonClasses($classes)
118
-	{
119
-		// Filter classes
120
-		// $classes = array_intersect($classes, $this->buttons);
121
-
122
-		// Prepend button type
123
-		$classes   = $this->prependWith($classes, 'btn-');
124
-		$classes[] = 'btn';
125
-
126
-		return $classes;
127
-	}
128
-
129
-	/**
130
-	 * Filter field classes
131
-	 *
132
-	 * @param  array $classes An array of classes
133
-	 *
134
-	 * @return array A filtered array
135
-	 */
136
-	public function filterFieldClasses($classes)
137
-	{
138
-		// Filter classes
139
-		$classes = array_intersect($classes, $this->fields);
140
-
141
-		// Prepend field type
142
-		$classes = array_map(function($class) {
143
-			return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
144
-		}, $classes);
145
-
146
-		return $classes;
147
-	}
148
-
149
-	////////////////////////////////////////////////////////////////////
150
-	///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
151
-	////////////////////////////////////////////////////////////////////
152
-
153
-	/**
154
-	 * Framework error state
155
-	 *
156
-	 * @return string
157
-	 */
158
-	public function errorState()
159
-	{
160
-		return 'has-error';
161
-	}
162
-
163
-	/**
164
-	 * Returns corresponding inline class of a field
165
-	 *
166
-	 * @param Field $field
167
-	 *
168
-	 * @return string
169
-	 */
170
-	public function getInlineLabelClass($field)
171
-	{
172
-		$inlineClass = parent::getInlineLabelClass($field);
173
-		if ($field->isOfType('checkbox', 'checkboxes')) {
174
-			$inlineClass = 'checkbox-'.$inlineClass;
175
-		} elseif ($field->isOfType('radio', 'radios')) {
176
-			$inlineClass = 'radio-'.$inlineClass;
177
-		}
178
-
179
-		return $inlineClass;
180
-	}
181
-
182
-	/**
183
-	 * Set the fields width from a label width
184
-	 *
185
-	 * @param array $labelWidths
186
-	 */
187
-	protected function setFieldWidths($labelWidths)
188
-	{
189
-		$labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
190
-
191
-		$viewports = $this->getFrameworkOption('viewports');
192
-		foreach ($labelWidths as $viewport => $columns) {
193
-			if ($viewport) {
194
-				$labelWidthClass .= " col-$viewports[$viewport]-$columns";
195
-				$fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns);
196
-				$fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns";
197
-			}
198
-		}
199
-
200
-		$this->labelWidth  = ltrim($labelWidthClass);
201
-		$this->fieldWidth  = ltrim($fieldWidthClass);
202
-		$this->fieldOffset = ltrim($fieldOffsetClass);
203
-	}
204
-
205
-	////////////////////////////////////////////////////////////////////
206
-	///////////////////////////// ADD CLASSES //////////////////////////
207
-	////////////////////////////////////////////////////////////////////
208
-
209
-	/**
210
-	 * Add classes to a field
211
-	 *
212
-	 * @param Field $field
213
-	 * @param array $classes The possible classes to add
214
-	 *
215
-	 * @return Field
216
-	 */
217
-	public function getFieldClasses(Field $field, $classes)
218
-	{
219
-		// Add inline class for checkables
220
-		if ($field->isCheckable() and in_array('inline', $classes)) {
221
-			$field->inline();
222
-		}
223
-
224
-		// Filter classes according to field type
225
-		if ($field->isButton()) {
226
-			$classes = $this->filterButtonClasses($classes);
227
-		} else {
228
-			$classes = $this->filterFieldClasses($classes);
229
-		}
230
-
231
-		// Add form-control class for text-type, textarea and select fields
232
-		// As text-type is open-ended we instead exclude those that shouldn't receive the class
233
-		if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array(
234
-					'file',
235
-					'plaintext',
236
-				)) and !in_array('form-control', $classes)
237
-		) {
238
-			$classes[] = 'form-control';
239
-		}
240
-
241
-		return $this->addClassesToField($field, $classes);
242
-	}
243
-
244
-	/**
245
-	 * Add group classes
246
-	 *
247
-	 * @return string A list of group classes
248
-	 */
249
-	public function getGroupClasses()
250
-	{
251
-		return 'form-group';
252
-	}
253
-
254
-	/**
255
-	 * Add label classes
256
-	 *
257
-	 * @return string[] An array of attributes with the label class
258
-	 */
259
-	public function getLabelClasses()
260
-	{
261
-		if ($this->app['former.form']->isOfType('horizontal')) {
262
-			return array('control-label', $this->labelWidth);
263
-		} elseif ($this->app['former.form']->isOfType('inline')) {
264
-			return array('sr-only');
265
-		} else {
266
-			return array('control-label');
267
-		}
268
-	}
269
-
270
-	/**
271
-	 * Add uneditable field classes
272
-	 *
273
-	 * @return string An array of attributes with the uneditable class
274
-	 */
275
-	public function getUneditableClasses()
276
-	{
277
-		return '';
278
-	}
279
-
280
-	/**
281
-	 * Add plain text field classes
282
-	 *
283
-	 * @return string An array of attributes with the plain text class
284
-	 */
285
-	public function getPlainTextClasses()
286
-	{
287
-		return 'form-control-static';
288
-	}
289
-
290
-	/**
291
-	 * Add form class
292
-	 *
293
-	 * @param  string $type The type of form to add
294
-	 *
295
-	 * @return string|null
296
-	 */
297
-	public function getFormClasses($type)
298
-	{
299
-		return $type ? 'form-'.$type : null;
300
-	}
301
-
302
-	/**
303
-	 * Add actions block class
304
-	 *
305
-	 * @return string|null
306
-	 */
307
-	public function getActionClasses()
308
-	{
309
-		if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) {
310
-			return 'form-group';
311
-		}
312
-
313
-		return null;
314
-	}
315
-
316
-	////////////////////////////////////////////////////////////////////
317
-	//////////////////////////// RENDER BLOCKS /////////////////////////
318
-	////////////////////////////////////////////////////////////////////
319
-
320
-	/**
321
-	 * Render an help text
322
-	 *
323
-	 * @param string $text
324
-	 * @param array  $attributes
325
-	 *
326
-	 * @return Element
327
-	 */
328
-	public function createHelp($text, $attributes = array())
329
-	{
330
-		return Element::create('span', $text, $attributes)->addClass('help-block');
331
-	}
332
-
333
-	/**
334
-	 * Render an help text
335
-	 *
336
-	 * @param string $text
337
-	 * @param array  $attributes
338
-	 *
339
-	 * @return Element
340
-	 */
341
-	public function createBlockHelp($text, $attributes = array())
342
-	{
343
-		return Element::create('p', $text, $attributes)->addClass('help-block');
344
-	}
345
-
346
-	/**
347
-	 * Render a disabled field
348
-	 *
349
-	 * @param Field $field
350
-	 *
351
-	 * @return Element
352
-	 */
353
-	public function createDisabledField(Field $field)
354
-	{
355
-		return Element::create('span', $field->getValue(), $field->getAttributes());
356
-	}
357
-
358
-	/**
359
-	 * Render a plain text field
360
-	 *
361
-	 * @param Field $field
362
-	 *
363
-	 * @return Element
364
-	 */
365
-	public function createPlainTextField(Field $field)
366
-	{
367
-		$label = $field->getLabel();
368
-		if ($label) {
369
-			$label->for('');
370
-		}
371
-
372
-		return Element::create('div', $field->getValue(), $field->getAttributes());
373
-	}
374
-
375
-	////////////////////////////////////////////////////////////////////
376
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
377
-	////////////////////////////////////////////////////////////////////
378
-
379
-	/**
380
-	 * Wrap an item to be prepended or appended to the current field
381
-	 *
382
-	 * @return Element A wrapped item
383
-	 */
384
-	public function placeAround($item)
385
-	{
386
-		// Render object
387
-		if (is_object($item) and method_exists($item, '__toString')) {
388
-			$item = $item->__toString();
389
-		}
390
-
391
-		// Get class to use
392
-		$class = (strpos($item, '<button') !== false) ? 'btn' : 'addon';
393
-
394
-		return Element::create('span', $item)->addClass('input-group-'.$class);
395
-	}
396
-
397
-	/**
398
-	 * Wrap a field with prepended and appended items
399
-	 *
400
-	 * @param  Field $field
401
-	 * @param  array $prepend
402
-	 * @param  array $append
403
-	 *
404
-	 * @return string A field concatented with prepended and/or appended items
405
-	 */
406
-	public function prependAppend($field, $prepend, $append)
407
-	{
408
-		$return = '<div class="input-group">';
409
-		$return .= join(null, $prepend);
410
-		$return .= $field->render();
411
-		$return .= join(null, $append);
412
-		$return .= '</div>';
413
-
414
-		return $return;
415
-	}
416
-
417
-	/**
418
-	 * Wrap a field with potential additional tags
419
-	 *
420
-	 * @param  Field $field
421
-	 *
422
-	 * @return Element A wrapped field
423
-	 */
424
-	public function wrapField($field)
425
-	{
426
-		if ($this->app['former.form']->isOfType('horizontal')) {
427
-			return Element::create('div', $field)->addClass($this->fieldWidth);
428
-		}
429
-
430
-		return $field;
431
-	}
432
-
433
-	/**
434
-	 * Wrap actions block with potential additional tags
435
-	 *
436
-	 * @param  Actions $actions
437
-	 *
438
-	 * @return string A wrapped actions block
439
-	 */
440
-	public function wrapActions($actions)
441
-	{
442
-		// For horizontal forms, we wrap the actions in a div
443
-		if ($this->app['former.form']->isOfType('horizontal')) {
444
-			return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
445
-		}
446
-
447
-		return $actions;
448
-	}
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
+        'default',
35
+        'primary',
36
+        'warning',
37
+        'danger',
38
+        'success',
39
+        'info',
40
+    );
41
+
42
+    /**
43
+     * The field sizes available
44
+     *
45
+     * @var array
46
+     */
47
+    private $fields = array(
48
+        'lg',
49
+        'sm',
50
+        // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6',
51
+        // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12',
52
+        // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6',
53
+        // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12',
54
+        // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6',
55
+        // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12',
56
+        // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6',
57
+        // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12',
58
+    );
59
+
60
+    /**
61
+     * The field states available
62
+     *
63
+     * @var array
64
+     */
65
+    protected $states = array(
66
+        'has-warning',
67
+        'has-error',
68
+        'has-success',
69
+    );
70
+
71
+    /**
72
+     * The default HTML tag used for icons
73
+     *
74
+     * @var string
75
+     */
76
+    protected $iconTag = 'span';
77
+
78
+    /**
79
+     * The default set for icon fonts
80
+     * By default Bootstrap 3 offers only 'glyphicon'
81
+     * See Former docs to use 'social' and 'filetypes' sets for specific icons.
82
+     *
83
+     * @var string
84
+     */
85
+    protected $iconSet = 'glyphicon';
86
+
87
+    /**
88
+     * The default prefix icon names
89
+     * "icon" works for Bootstrap 2 and Font-awesome
90
+     *
91
+     * @var string
92
+     */
93
+    protected $iconPrefix = 'glyphicon';
94
+
95
+    /**
96
+     * Create a new TwitterBootstrap instance
97
+     *
98
+     * @param \Illuminate\Container\Container $app
99
+     */
100
+    public function __construct(Container $app)
101
+    {
102
+        $this->app = $app;
103
+        $this->setFrameworkDefaults();
104
+    }
105
+
106
+    ////////////////////////////////////////////////////////////////////
107
+    /////////////////////////// FILTER ARRAYS //////////////////////////
108
+    ////////////////////////////////////////////////////////////////////
109
+
110
+    /**
111
+     * Filter buttons classes
112
+     *
113
+     * @param  array $classes An array of classes
114
+     *
115
+     * @return string[] A filtered array
116
+     */
117
+    public function filterButtonClasses($classes)
118
+    {
119
+        // Filter classes
120
+        // $classes = array_intersect($classes, $this->buttons);
121
+
122
+        // Prepend button type
123
+        $classes   = $this->prependWith($classes, 'btn-');
124
+        $classes[] = 'btn';
125
+
126
+        return $classes;
127
+    }
128
+
129
+    /**
130
+     * Filter field classes
131
+     *
132
+     * @param  array $classes An array of classes
133
+     *
134
+     * @return array A filtered array
135
+     */
136
+    public function filterFieldClasses($classes)
137
+    {
138
+        // Filter classes
139
+        $classes = array_intersect($classes, $this->fields);
140
+
141
+        // Prepend field type
142
+        $classes = array_map(function($class) {
143
+            return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
144
+        }, $classes);
145
+
146
+        return $classes;
147
+    }
148
+
149
+    ////////////////////////////////////////////////////////////////////
150
+    ///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
151
+    ////////////////////////////////////////////////////////////////////
152
+
153
+    /**
154
+     * Framework error state
155
+     *
156
+     * @return string
157
+     */
158
+    public function errorState()
159
+    {
160
+        return 'has-error';
161
+    }
162
+
163
+    /**
164
+     * Returns corresponding inline class of a field
165
+     *
166
+     * @param Field $field
167
+     *
168
+     * @return string
169
+     */
170
+    public function getInlineLabelClass($field)
171
+    {
172
+        $inlineClass = parent::getInlineLabelClass($field);
173
+        if ($field->isOfType('checkbox', 'checkboxes')) {
174
+            $inlineClass = 'checkbox-'.$inlineClass;
175
+        } elseif ($field->isOfType('radio', 'radios')) {
176
+            $inlineClass = 'radio-'.$inlineClass;
177
+        }
178
+
179
+        return $inlineClass;
180
+    }
181
+
182
+    /**
183
+     * Set the fields width from a label width
184
+     *
185
+     * @param array $labelWidths
186
+     */
187
+    protected function setFieldWidths($labelWidths)
188
+    {
189
+        $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
190
+
191
+        $viewports = $this->getFrameworkOption('viewports');
192
+        foreach ($labelWidths as $viewport => $columns) {
193
+            if ($viewport) {
194
+                $labelWidthClass .= " col-$viewports[$viewport]-$columns";
195
+                $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns);
196
+                $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns";
197
+            }
198
+        }
199
+
200
+        $this->labelWidth  = ltrim($labelWidthClass);
201
+        $this->fieldWidth  = ltrim($fieldWidthClass);
202
+        $this->fieldOffset = ltrim($fieldOffsetClass);
203
+    }
204
+
205
+    ////////////////////////////////////////////////////////////////////
206
+    ///////////////////////////// ADD CLASSES //////////////////////////
207
+    ////////////////////////////////////////////////////////////////////
208
+
209
+    /**
210
+     * Add classes to a field
211
+     *
212
+     * @param Field $field
213
+     * @param array $classes The possible classes to add
214
+     *
215
+     * @return Field
216
+     */
217
+    public function getFieldClasses(Field $field, $classes)
218
+    {
219
+        // Add inline class for checkables
220
+        if ($field->isCheckable() and in_array('inline', $classes)) {
221
+            $field->inline();
222
+        }
223
+
224
+        // Filter classes according to field type
225
+        if ($field->isButton()) {
226
+            $classes = $this->filterButtonClasses($classes);
227
+        } else {
228
+            $classes = $this->filterFieldClasses($classes);
229
+        }
230
+
231
+        // Add form-control class for text-type, textarea and select fields
232
+        // As text-type is open-ended we instead exclude those that shouldn't receive the class
233
+        if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array(
234
+                    'file',
235
+                    'plaintext',
236
+                )) and !in_array('form-control', $classes)
237
+        ) {
238
+            $classes[] = 'form-control';
239
+        }
240
+
241
+        return $this->addClassesToField($field, $classes);
242
+    }
243
+
244
+    /**
245
+     * Add group classes
246
+     *
247
+     * @return string A list of group classes
248
+     */
249
+    public function getGroupClasses()
250
+    {
251
+        return 'form-group';
252
+    }
253
+
254
+    /**
255
+     * Add label classes
256
+     *
257
+     * @return string[] An array of attributes with the label class
258
+     */
259
+    public function getLabelClasses()
260
+    {
261
+        if ($this->app['former.form']->isOfType('horizontal')) {
262
+            return array('control-label', $this->labelWidth);
263
+        } elseif ($this->app['former.form']->isOfType('inline')) {
264
+            return array('sr-only');
265
+        } else {
266
+            return array('control-label');
267
+        }
268
+    }
269
+
270
+    /**
271
+     * Add uneditable field classes
272
+     *
273
+     * @return string An array of attributes with the uneditable class
274
+     */
275
+    public function getUneditableClasses()
276
+    {
277
+        return '';
278
+    }
279
+
280
+    /**
281
+     * Add plain text field classes
282
+     *
283
+     * @return string An array of attributes with the plain text class
284
+     */
285
+    public function getPlainTextClasses()
286
+    {
287
+        return 'form-control-static';
288
+    }
289
+
290
+    /**
291
+     * Add form class
292
+     *
293
+     * @param  string $type The type of form to add
294
+     *
295
+     * @return string|null
296
+     */
297
+    public function getFormClasses($type)
298
+    {
299
+        return $type ? 'form-'.$type : null;
300
+    }
301
+
302
+    /**
303
+     * Add actions block class
304
+     *
305
+     * @return string|null
306
+     */
307
+    public function getActionClasses()
308
+    {
309
+        if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) {
310
+            return 'form-group';
311
+        }
312
+
313
+        return null;
314
+    }
315
+
316
+    ////////////////////////////////////////////////////////////////////
317
+    //////////////////////////// RENDER BLOCKS /////////////////////////
318
+    ////////////////////////////////////////////////////////////////////
319
+
320
+    /**
321
+     * Render an help text
322
+     *
323
+     * @param string $text
324
+     * @param array  $attributes
325
+     *
326
+     * @return Element
327
+     */
328
+    public function createHelp($text, $attributes = array())
329
+    {
330
+        return Element::create('span', $text, $attributes)->addClass('help-block');
331
+    }
332
+
333
+    /**
334
+     * Render an help text
335
+     *
336
+     * @param string $text
337
+     * @param array  $attributes
338
+     *
339
+     * @return Element
340
+     */
341
+    public function createBlockHelp($text, $attributes = array())
342
+    {
343
+        return Element::create('p', $text, $attributes)->addClass('help-block');
344
+    }
345
+
346
+    /**
347
+     * Render a disabled field
348
+     *
349
+     * @param Field $field
350
+     *
351
+     * @return Element
352
+     */
353
+    public function createDisabledField(Field $field)
354
+    {
355
+        return Element::create('span', $field->getValue(), $field->getAttributes());
356
+    }
357
+
358
+    /**
359
+     * Render a plain text field
360
+     *
361
+     * @param Field $field
362
+     *
363
+     * @return Element
364
+     */
365
+    public function createPlainTextField(Field $field)
366
+    {
367
+        $label = $field->getLabel();
368
+        if ($label) {
369
+            $label->for('');
370
+        }
371
+
372
+        return Element::create('div', $field->getValue(), $field->getAttributes());
373
+    }
374
+
375
+    ////////////////////////////////////////////////////////////////////
376
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
377
+    ////////////////////////////////////////////////////////////////////
378
+
379
+    /**
380
+     * Wrap an item to be prepended or appended to the current field
381
+     *
382
+     * @return Element A wrapped item
383
+     */
384
+    public function placeAround($item)
385
+    {
386
+        // Render object
387
+        if (is_object($item) and method_exists($item, '__toString')) {
388
+            $item = $item->__toString();
389
+        }
390
+
391
+        // Get class to use
392
+        $class = (strpos($item, '<button') !== false) ? 'btn' : 'addon';
393
+
394
+        return Element::create('span', $item)->addClass('input-group-'.$class);
395
+    }
396
+
397
+    /**
398
+     * Wrap a field with prepended and appended items
399
+     *
400
+     * @param  Field $field
401
+     * @param  array $prepend
402
+     * @param  array $append
403
+     *
404
+     * @return string A field concatented with prepended and/or appended items
405
+     */
406
+    public function prependAppend($field, $prepend, $append)
407
+    {
408
+        $return = '<div class="input-group">';
409
+        $return .= join(null, $prepend);
410
+        $return .= $field->render();
411
+        $return .= join(null, $append);
412
+        $return .= '</div>';
413
+
414
+        return $return;
415
+    }
416
+
417
+    /**
418
+     * Wrap a field with potential additional tags
419
+     *
420
+     * @param  Field $field
421
+     *
422
+     * @return Element A wrapped field
423
+     */
424
+    public function wrapField($field)
425
+    {
426
+        if ($this->app['former.form']->isOfType('horizontal')) {
427
+            return Element::create('div', $field)->addClass($this->fieldWidth);
428
+        }
429
+
430
+        return $field;
431
+    }
432
+
433
+    /**
434
+     * Wrap actions block with potential additional tags
435
+     *
436
+     * @param  Actions $actions
437
+     *
438
+     * @return string A wrapped actions block
439
+     */
440
+    public function wrapActions($actions)
441
+    {
442
+        // For horizontal forms, we wrap the actions in a div
443
+        if ($this->app['former.form']->isOfType('horizontal')) {
444
+            return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
445
+        }
446
+
447
+        return $actions;
448
+    }
449 449
 }
Please login to merge, or discard this patch.
src/Former/Framework/ZurbFoundation.php 1 patch
Indentation   +270 added lines, -270 removed lines patch added patch discarded remove patch
@@ -13,274 +13,274 @@
 block discarded – undo
13 13
  */
14 14
 class ZurbFoundation 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');
22
-
23
-	/**
24
-	 * The field sizes available
25
-	 *
26
-	 * @var array
27
-	 */
28
-	private $fields = array(
29
-		1  => 'one',
30
-		2  => 'two',
31
-		3  => 'three',
32
-		4  => 'four',
33
-		5  => 'five',
34
-		6  => 'six',
35
-		7  => 'seven',
36
-		8  => 'eight',
37
-		9  => 'nine',
38
-		10 => 'ten',
39
-		11 => 'eleven',
40
-		12 => 'twelve',
41
-	);
42
-
43
-	/**
44
-	 * The field states available
45
-	 *
46
-	 * @var array
47
-	 */
48
-	protected $states = array(
49
-		'error',
50
-	);
51
-
52
-	/**
53
-	 * Create a new ZurbFoundation instance
54
-	 *
55
-	 * @param Container $app
56
-	 */
57
-	public function __construct(Container $app)
58
-	{
59
-		$this->app = $app;
60
-		$this->setFrameworkDefaults();
61
-	}
62
-
63
-	////////////////////////////////////////////////////////////////////
64
-	/////////////////////////// FILTER ARRAYS //////////////////////////
65
-	////////////////////////////////////////////////////////////////////
66
-
67
-	public function filterButtonClasses($classes)
68
-	{
69
-		return $classes;
70
-	}
71
-
72
-	public function filterFieldClasses($classes)
73
-	{
74
-		// Filter classes
75
-		$classes = array_intersect($classes, $this->fields);
76
-
77
-		return $classes;
78
-	}
79
-
80
-	////////////////////////////////////////////////////////////////////
81
-	///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
82
-	////////////////////////////////////////////////////////////////////
83
-
84
-	protected function setFieldWidths($labelWidths)
85
-	{
86
-		$labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
87
-
88
-		$viewports = $this->getFrameworkOption('viewports');
89
-
90
-		foreach ($labelWidths as $viewport => $columns) {
91
-			if ($viewport) {
92
-				$labelWidthClass .= $viewports[$viewport].$this->fields[$columns].' ';
93
-				$fieldWidthClass .= $viewports[$viewport].$this->fields[12 - $columns].' ';
94
-				$fieldOffsetClass .= $viewports[$viewport].'offset-by-'.$this->fields[$columns].' ';
95
-			}
96
-		}
97
-
98
-		$this->labelWidth  = $labelWidthClass.'columns';
99
-		$this->fieldWidth  = $fieldWidthClass.'columns';
100
-		$this->fieldOffset = $fieldOffsetClass.'columns';
101
-	}
102
-
103
-	////////////////////////////////////////////////////////////////////
104
-	///////////////////////////// ADD CLASSES //////////////////////////
105
-	////////////////////////////////////////////////////////////////////
106
-
107
-	public function getFieldClasses(Field $field, $classes = array())
108
-	{
109
-		$classes = $this->filterFieldClasses($classes);
110
-
111
-		return $this->addClassesToField($field, $classes);
112
-	}
113
-
114
-	public function getGroupClasses()
115
-	{
116
-		if ($this->app['former.form']->isOfType('horizontal')) {
117
-			return 'row';
118
-		} else {
119
-			return null;
120
-		}
121
-	}
122
-
123
-	/**
124
-	 * Add label classes
125
-	 *
126
-	 * @return string|null An array of attributes with the label class
127
-	 */
128
-	public function getLabelClasses()
129
-	{
130
-		if ($this->app['former.form']->isOfType('horizontal')) {
131
-			return $this->getFrameworkOption('wrappedLabelClasses');
132
-		} else {
133
-			return null;
134
-		}
135
-	}
136
-
137
-	public function getUneditableClasses()
138
-	{
139
-		return null;
140
-	}
141
-
142
-	public function getPlainTextClasses()
143
-	{
144
-		return null;
145
-	}
146
-
147
-	public function getFormClasses($type)
148
-	{
149
-		return null;
150
-	}
151
-
152
-	public function getActionClasses()
153
-	{
154
-		return null;
155
-	}
156
-
157
-	////////////////////////////////////////////////////////////////////
158
-	//////////////////////////// RENDER BLOCKS /////////////////////////
159
-	////////////////////////////////////////////////////////////////////
160
-
161
-	public function createHelp($text, $attributes = null)
162
-	{
163
-		if (is_null($attributes) or empty($attributes)) {
164
-			$attributes = $this->getFrameworkOption('error_classes');
165
-		}
166
-
167
-		return Element::create('span', $text, $attributes);
168
-	}
169
-
170
-	/**
171
-	 * Render a disabled field
172
-	 *
173
-	 * @param Field $field
174
-	 *
175
-	 * @return Input
176
-	 */
177
-	public function createDisabledField(Field $field)
178
-	{
179
-		$field->disabled();
180
-
181
-		return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
182
-	}
183
-
184
-	/**
185
-	 * Render a plain text field
186
-	 * Which fallback to a disabled field
187
-	 *
188
-	 * @param Field $field
189
-	 *
190
-	 * @return Input
191
-	 */
192
-	public function createPlainTextField(Field $field)
193
-	{
194
-		return $this->createDisabledField($field);
195
-	}
196
-
197
-	////////////////////////////////////////////////////////////////////
198
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
199
-	////////////////////////////////////////////////////////////////////
200
-
201
-	/**
202
-	 * Wrap an item to be prepended or appended to the current field.
203
-	 * For Zurb we return the item and handle the wrapping in prependAppend
204
-	 * as wrapping is dependent on whether we're prepending or appending.
205
-	 *
206
-	 * @return string A wrapped item
207
-	 */
208
-	public function placeAround($item)
209
-	{
210
-		return $item;
211
-	}
212
-
213
-	/**
214
-	 * Wrap a field with prepended and appended items
215
-	 *
216
-	 * @param  Field $field
217
-	 * @param  array $prepend
218
-	 * @param  array $append
219
-	 *
220
-	 * @return string A field concatented with prepended and/or appended items
221
-	 */
222
-	public function prependAppend($field, $prepend, $append)
223
-	{
224
-		$return = '';
225
-
226
-		foreach ($prepend as $item) {
227
-			$return .= '<div class="two mobile-one columns"><span class="prefix">'.$item.'</span></div>';
228
-		}
229
-
230
-		$return .= '<div class="ten mobile-three columns">'.$field->render().'</div>';
231
-
232
-		foreach ($append as $item) {
233
-			$return .= '<div class="two mobile-one columns"><span class="postfix">'.$item.'</span></div>';
234
-		}
235
-
236
-		return $return;
237
-	}
238
-
239
-	/**
240
-	 * Wraps all label contents with potential additional tags.
241
-	 *
242
-	 * @param  string $label
243
-	 *
244
-	 * @return string A wrapped label
245
-	 */
246
-	public function wrapLabel($label)
247
-	{
248
-		if ($this->app['former.form']->isOfType('horizontal')) {
249
-			return Element::create('div', $label)->addClass($this->labelWidth);
250
-		} else {
251
-			return $label;
252
-		}
253
-	}
254
-
255
-	/**
256
-	 * Wraps all field contents with potential additional tags.
257
-	 *
258
-	 * @param  Field $field
259
-	 *
260
-	 * @return Element A wrapped field
261
-	 */
262
-	public function wrapField($field)
263
-	{
264
-		if ($this->app['former.form']->isOfType('horizontal')) {
265
-			return Element::create('div', $field)->addClass($this->fieldWidth);
266
-		} else {
267
-			return $field;
268
-		}
269
-	}
270
-
271
-	/**
272
-	 * Wrap actions block with potential additional tags
273
-	 *
274
-	 * @param  Actions $actions
275
-	 *
276
-	 * @return string A wrapped actions block
277
-	 */
278
-	public function wrapActions($actions)
279
-	{
280
-		if ($this->app['former.form']->isOfType('horizontal')) {
281
-			return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
282
-		} else {
283
-			return $actions;
284
-		}
285
-	}
16
+    /**
17
+     * Form types that trigger special styling for this Framework
18
+     *
19
+     * @var array
20
+     */
21
+    protected $availableTypes = array('horizontal', 'vertical');
22
+
23
+    /**
24
+     * The field sizes available
25
+     *
26
+     * @var array
27
+     */
28
+    private $fields = array(
29
+        1  => 'one',
30
+        2  => 'two',
31
+        3  => 'three',
32
+        4  => 'four',
33
+        5  => 'five',
34
+        6  => 'six',
35
+        7  => 'seven',
36
+        8  => 'eight',
37
+        9  => 'nine',
38
+        10 => 'ten',
39
+        11 => 'eleven',
40
+        12 => 'twelve',
41
+    );
42
+
43
+    /**
44
+     * The field states available
45
+     *
46
+     * @var array
47
+     */
48
+    protected $states = array(
49
+        'error',
50
+    );
51
+
52
+    /**
53
+     * Create a new ZurbFoundation instance
54
+     *
55
+     * @param Container $app
56
+     */
57
+    public function __construct(Container $app)
58
+    {
59
+        $this->app = $app;
60
+        $this->setFrameworkDefaults();
61
+    }
62
+
63
+    ////////////////////////////////////////////////////////////////////
64
+    /////////////////////////// FILTER ARRAYS //////////////////////////
65
+    ////////////////////////////////////////////////////////////////////
66
+
67
+    public function filterButtonClasses($classes)
68
+    {
69
+        return $classes;
70
+    }
71
+
72
+    public function filterFieldClasses($classes)
73
+    {
74
+        // Filter classes
75
+        $classes = array_intersect($classes, $this->fields);
76
+
77
+        return $classes;
78
+    }
79
+
80
+    ////////////////////////////////////////////////////////////////////
81
+    ///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
82
+    ////////////////////////////////////////////////////////////////////
83
+
84
+    protected function setFieldWidths($labelWidths)
85
+    {
86
+        $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
87
+
88
+        $viewports = $this->getFrameworkOption('viewports');
89
+
90
+        foreach ($labelWidths as $viewport => $columns) {
91
+            if ($viewport) {
92
+                $labelWidthClass .= $viewports[$viewport].$this->fields[$columns].' ';
93
+                $fieldWidthClass .= $viewports[$viewport].$this->fields[12 - $columns].' ';
94
+                $fieldOffsetClass .= $viewports[$viewport].'offset-by-'.$this->fields[$columns].' ';
95
+            }
96
+        }
97
+
98
+        $this->labelWidth  = $labelWidthClass.'columns';
99
+        $this->fieldWidth  = $fieldWidthClass.'columns';
100
+        $this->fieldOffset = $fieldOffsetClass.'columns';
101
+    }
102
+
103
+    ////////////////////////////////////////////////////////////////////
104
+    ///////////////////////////// ADD CLASSES //////////////////////////
105
+    ////////////////////////////////////////////////////////////////////
106
+
107
+    public function getFieldClasses(Field $field, $classes = array())
108
+    {
109
+        $classes = $this->filterFieldClasses($classes);
110
+
111
+        return $this->addClassesToField($field, $classes);
112
+    }
113
+
114
+    public function getGroupClasses()
115
+    {
116
+        if ($this->app['former.form']->isOfType('horizontal')) {
117
+            return 'row';
118
+        } else {
119
+            return null;
120
+        }
121
+    }
122
+
123
+    /**
124
+     * Add label classes
125
+     *
126
+     * @return string|null An array of attributes with the label class
127
+     */
128
+    public function getLabelClasses()
129
+    {
130
+        if ($this->app['former.form']->isOfType('horizontal')) {
131
+            return $this->getFrameworkOption('wrappedLabelClasses');
132
+        } else {
133
+            return null;
134
+        }
135
+    }
136
+
137
+    public function getUneditableClasses()
138
+    {
139
+        return null;
140
+    }
141
+
142
+    public function getPlainTextClasses()
143
+    {
144
+        return null;
145
+    }
146
+
147
+    public function getFormClasses($type)
148
+    {
149
+        return null;
150
+    }
151
+
152
+    public function getActionClasses()
153
+    {
154
+        return null;
155
+    }
156
+
157
+    ////////////////////////////////////////////////////////////////////
158
+    //////////////////////////// RENDER BLOCKS /////////////////////////
159
+    ////////////////////////////////////////////////////////////////////
160
+
161
+    public function createHelp($text, $attributes = null)
162
+    {
163
+        if (is_null($attributes) or empty($attributes)) {
164
+            $attributes = $this->getFrameworkOption('error_classes');
165
+        }
166
+
167
+        return Element::create('span', $text, $attributes);
168
+    }
169
+
170
+    /**
171
+     * Render a disabled field
172
+     *
173
+     * @param Field $field
174
+     *
175
+     * @return Input
176
+     */
177
+    public function createDisabledField(Field $field)
178
+    {
179
+        $field->disabled();
180
+
181
+        return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
182
+    }
183
+
184
+    /**
185
+     * Render a plain text field
186
+     * Which fallback to a disabled field
187
+     *
188
+     * @param Field $field
189
+     *
190
+     * @return Input
191
+     */
192
+    public function createPlainTextField(Field $field)
193
+    {
194
+        return $this->createDisabledField($field);
195
+    }
196
+
197
+    ////////////////////////////////////////////////////////////////////
198
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
199
+    ////////////////////////////////////////////////////////////////////
200
+
201
+    /**
202
+     * Wrap an item to be prepended or appended to the current field.
203
+     * For Zurb we return the item and handle the wrapping in prependAppend
204
+     * as wrapping is dependent on whether we're prepending or appending.
205
+     *
206
+     * @return string A wrapped item
207
+     */
208
+    public function placeAround($item)
209
+    {
210
+        return $item;
211
+    }
212
+
213
+    /**
214
+     * Wrap a field with prepended and appended items
215
+     *
216
+     * @param  Field $field
217
+     * @param  array $prepend
218
+     * @param  array $append
219
+     *
220
+     * @return string A field concatented with prepended and/or appended items
221
+     */
222
+    public function prependAppend($field, $prepend, $append)
223
+    {
224
+        $return = '';
225
+
226
+        foreach ($prepend as $item) {
227
+            $return .= '<div class="two mobile-one columns"><span class="prefix">'.$item.'</span></div>';
228
+        }
229
+
230
+        $return .= '<div class="ten mobile-three columns">'.$field->render().'</div>';
231
+
232
+        foreach ($append as $item) {
233
+            $return .= '<div class="two mobile-one columns"><span class="postfix">'.$item.'</span></div>';
234
+        }
235
+
236
+        return $return;
237
+    }
238
+
239
+    /**
240
+     * Wraps all label contents with potential additional tags.
241
+     *
242
+     * @param  string $label
243
+     *
244
+     * @return string A wrapped label
245
+     */
246
+    public function wrapLabel($label)
247
+    {
248
+        if ($this->app['former.form']->isOfType('horizontal')) {
249
+            return Element::create('div', $label)->addClass($this->labelWidth);
250
+        } else {
251
+            return $label;
252
+        }
253
+    }
254
+
255
+    /**
256
+     * Wraps all field contents with potential additional tags.
257
+     *
258
+     * @param  Field $field
259
+     *
260
+     * @return Element A wrapped field
261
+     */
262
+    public function wrapField($field)
263
+    {
264
+        if ($this->app['former.form']->isOfType('horizontal')) {
265
+            return Element::create('div', $field)->addClass($this->fieldWidth);
266
+        } else {
267
+            return $field;
268
+        }
269
+    }
270
+
271
+    /**
272
+     * Wrap actions block with potential additional tags
273
+     *
274
+     * @param  Actions $actions
275
+     *
276
+     * @return string A wrapped actions block
277
+     */
278
+    public function wrapActions($actions)
279
+    {
280
+        if ($this->app['former.form']->isOfType('horizontal')) {
281
+            return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
282
+        } else {
283
+            return $actions;
284
+        }
285
+    }
286 286
 }
Please login to merge, or discard this patch.
src/Former/Framework/ZurbFoundation4.php 1 patch
Indentation   +277 added lines, -277 removed lines patch added patch discarded remove patch
@@ -13,281 +13,281 @@
 block discarded – undo
13 13
  */
14 14
 class ZurbFoundation4 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');
22
-
23
-	/**
24
-	 * The button types available
25
-	 *
26
-	 * @var array
27
-	 */
28
-	private $buttons = array(
29
-		'tiny',
30
-		'small',
31
-		'medium',
32
-		'large',
33
-		'success',
34
-		'radius',
35
-		'round',
36
-		'disabled',
37
-		'prefix',
38
-		'postfix',
39
-	);
40
-
41
-	/**
42
-	 * The field sizes available
43
-	 * Zurb Foundation 4 does not apply sizes to the form element, but to the wrapper div
44
-	 *
45
-	 * @var array
46
-	 */
47
-	private $fields = array();
48
-
49
-	/**
50
-	 * The field states available
51
-	 *
52
-	 * @var array
53
-	 */
54
-	protected $states = array(
55
-		'error',
56
-	);
57
-
58
-	/**
59
-	 * Create a new ZurbFoundation instance
60
-	 *
61
-	 * @param \Illuminate\Container\Container $app
62
-	 */
63
-	public function __construct(Container $app)
64
-	{
65
-		$this->app = $app;
66
-		$this->setFrameworkDefaults();
67
-	}
68
-
69
-	////////////////////////////////////////////////////////////////////
70
-	/////////////////////////// FILTER ARRAYS //////////////////////////
71
-	////////////////////////////////////////////////////////////////////
72
-
73
-	public function filterButtonClasses($classes)
74
-	{
75
-		// Filter classes
76
-		$classes   = array_intersect($classes, $this->buttons);
77
-		$classes[] = 'button';
78
-
79
-		return $classes;
80
-	}
81
-
82
-	public function filterFieldClasses($classes)
83
-	{
84
-		return null;
85
-	}
86
-
87
-	////////////////////////////////////////////////////////////////////
88
-	///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
89
-	////////////////////////////////////////////////////////////////////
90
-
91
-	protected function setFieldWidths($labelWidths)
92
-	{
93
-		$labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
94
-
95
-		$viewports = $this->getFrameworkOption('viewports');
96
-
97
-		foreach ($labelWidths as $viewport => $columns) {
98
-			if ($viewport) {
99
-				$labelWidthClass .= $viewports[$viewport].'-'.$columns.' ';
100
-				$fieldWidthClass .= $viewports[$viewport].'-'.(12 - $columns).' ';
101
-				$fieldOffsetClass .= $viewports[$viewport].'-offset-'.$columns.' ';
102
-			}
103
-		}
104
-
105
-		$this->labelWidth  = $labelWidthClass.'columns';
106
-		$this->fieldWidth  = $fieldWidthClass.'columns';
107
-		$this->fieldOffset = $fieldOffsetClass.'columns';
108
-	}
109
-
110
-	////////////////////////////////////////////////////////////////////
111
-	///////////////////////////// ADD CLASSES //////////////////////////
112
-	////////////////////////////////////////////////////////////////////
113
-
114
-	public function getFieldClasses(Field $field, $classes = array())
115
-	{
116
-		if ($field->isButton()) {
117
-			$classes = $this->filterButtonClasses($classes);
118
-		} else {
119
-			$classes = $this->filterFieldClasses($classes);
120
-		}
121
-
122
-		return $this->addClassesToField($field, $classes);
123
-	}
124
-
125
-	public function getGroupClasses()
126
-	{
127
-		if ($this->app['former.form']->isOfType('horizontal')) {
128
-			return 'row';
129
-		} else {
130
-			return null;
131
-		}
132
-	}
133
-
134
-	/**
135
-	 * Add label classes
136
-	 *
137
-	 * @return string|null An array of attributes with the label class
138
-	 */
139
-	public function getLabelClasses()
140
-	{
141
-		if ($this->app['former.form']->isOfType('horizontal')) {
142
-			return $this->getFrameworkOption('wrappedLabelClasses');
143
-		} else {
144
-			return null;
145
-		}
146
-	}
147
-
148
-	public function getUneditableClasses()
149
-	{
150
-		return null;
151
-	}
152
-
153
-	public function getPlainTextClasses()
154
-	{
155
-		return null;
156
-	}
157
-
158
-	public function getFormClasses($type)
159
-	{
160
-		return null;
161
-	}
162
-
163
-	public function getActionClasses()
164
-	{
165
-		return null;
166
-	}
167
-
168
-	////////////////////////////////////////////////////////////////////
169
-	//////////////////////////// RENDER BLOCKS /////////////////////////
170
-	////////////////////////////////////////////////////////////////////
171
-
172
-	public function createHelp($text, $attributes = null)
173
-	{
174
-		if (is_null($attributes) or empty($attributes)) {
175
-			$attributes = $this->getFrameworkOption('error_classes');
176
-		}
177
-
178
-		return Element::create('span', $text, $attributes);
179
-	}
180
-
181
-	/**
182
-	 * Render a disabled field
183
-	 *
184
-	 * @param Field $field
185
-	 *
186
-	 * @return Input
187
-	 */
188
-	public function createDisabledField(Field $field)
189
-	{
190
-		$field->disabled();
191
-
192
-		return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
193
-	}
194
-
195
-	/**
196
-	 * Render a plain text field
197
-	 * Which fallback to a disabled field
198
-	 *
199
-	 * @param Field $field
200
-	 *
201
-	 * @return Input
202
-	 */
203
-	public function createPlainTextField(Field $field)
204
-	{
205
-		return $this->createDisabledField($field);
206
-	}
207
-
208
-	////////////////////////////////////////////////////////////////////
209
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
210
-	////////////////////////////////////////////////////////////////////
211
-
212
-	/**
213
-	 * Wrap an item to be prepended or appended to the current field.
214
-	 * For Zurb we return the item and handle the wrapping in prependAppend
215
-	 * as wrapping is dependent on whether we're prepending or appending.
216
-	 *
217
-	 * @return string A wrapped item
218
-	 */
219
-	public function placeAround($item)
220
-	{
221
-		return $item;
222
-	}
223
-
224
-	/**
225
-	 * Wrap a field with prepended and appended items
226
-	 *
227
-	 * @param  Field $field
228
-	 * @param  array $prepend
229
-	 * @param  array $append
230
-	 *
231
-	 * @return string A field concatented with prepended and/or appended items
232
-	 */
233
-	public function prependAppend($field, $prepend, $append)
234
-	{
235
-		$return = '';
236
-
237
-		foreach ($prepend as $item) {
238
-			$return .= '<div class="large-2 small-3 columns"><span class="prefix">'.$item.'</span></div>';
239
-		}
240
-
241
-		$return .= '<div class="large-10 small-9 columns">'.$field->render().'</div>';
242
-
243
-		foreach ($append as $item) {
244
-			$return .= '<div class="large-2 small-3 columns"><span class="postfix">'.$item.'</span></div>';
245
-		}
246
-
247
-		return $return;
248
-	}
249
-
250
-	/**
251
-	 * Wraps all label contents with potential additional tags.
252
-	 *
253
-	 * @param  string $label
254
-	 *
255
-	 * @return string A wrapped label
256
-	 */
257
-	public function wrapLabel($label)
258
-	{
259
-		if ($this->app['former.form']->isOfType('horizontal')) {
260
-			return Element::create('div', $label)->addClass($this->labelWidth);
261
-		} else {
262
-			return $label;
263
-		}
264
-	}
265
-
266
-	/**
267
-	 * Wraps all field contents with potential additional tags.
268
-	 *
269
-	 * @param  Field $field
270
-	 *
271
-	 * @return Element A wrapped field
272
-	 */
273
-	public function wrapField($field)
274
-	{
275
-		if ($this->app['former.form']->isOfType('horizontal')) {
276
-			return Element::create('div', $field)->addClass($this->fieldWidth);
277
-		} else {
278
-			return $field;
279
-		}
280
-	}
281
-
282
-	/**
283
-	 * Wrap actions block with potential additional tags
284
-	 *
285
-	 * @param  Actions $actions
286
-	 *
287
-	 * @return string A wrapped actions block
288
-	 */
289
-	public function wrapActions($actions)
290
-	{
291
-		return $actions;
292
-	}
16
+    /**
17
+     * Form types that trigger special styling for this Framework
18
+     *
19
+     * @var array
20
+     */
21
+    protected $availableTypes = array('horizontal', 'vertical');
22
+
23
+    /**
24
+     * The button types available
25
+     *
26
+     * @var array
27
+     */
28
+    private $buttons = array(
29
+        'tiny',
30
+        'small',
31
+        'medium',
32
+        'large',
33
+        'success',
34
+        'radius',
35
+        'round',
36
+        'disabled',
37
+        'prefix',
38
+        'postfix',
39
+    );
40
+
41
+    /**
42
+     * The field sizes available
43
+     * Zurb Foundation 4 does not apply sizes to the form element, but to the wrapper div
44
+     *
45
+     * @var array
46
+     */
47
+    private $fields = array();
48
+
49
+    /**
50
+     * The field states available
51
+     *
52
+     * @var array
53
+     */
54
+    protected $states = array(
55
+        'error',
56
+    );
57
+
58
+    /**
59
+     * Create a new ZurbFoundation instance
60
+     *
61
+     * @param \Illuminate\Container\Container $app
62
+     */
63
+    public function __construct(Container $app)
64
+    {
65
+        $this->app = $app;
66
+        $this->setFrameworkDefaults();
67
+    }
68
+
69
+    ////////////////////////////////////////////////////////////////////
70
+    /////////////////////////// FILTER ARRAYS //////////////////////////
71
+    ////////////////////////////////////////////////////////////////////
72
+
73
+    public function filterButtonClasses($classes)
74
+    {
75
+        // Filter classes
76
+        $classes   = array_intersect($classes, $this->buttons);
77
+        $classes[] = 'button';
78
+
79
+        return $classes;
80
+    }
81
+
82
+    public function filterFieldClasses($classes)
83
+    {
84
+        return null;
85
+    }
86
+
87
+    ////////////////////////////////////////////////////////////////////
88
+    ///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
89
+    ////////////////////////////////////////////////////////////////////
90
+
91
+    protected function setFieldWidths($labelWidths)
92
+    {
93
+        $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
94
+
95
+        $viewports = $this->getFrameworkOption('viewports');
96
+
97
+        foreach ($labelWidths as $viewport => $columns) {
98
+            if ($viewport) {
99
+                $labelWidthClass .= $viewports[$viewport].'-'.$columns.' ';
100
+                $fieldWidthClass .= $viewports[$viewport].'-'.(12 - $columns).' ';
101
+                $fieldOffsetClass .= $viewports[$viewport].'-offset-'.$columns.' ';
102
+            }
103
+        }
104
+
105
+        $this->labelWidth  = $labelWidthClass.'columns';
106
+        $this->fieldWidth  = $fieldWidthClass.'columns';
107
+        $this->fieldOffset = $fieldOffsetClass.'columns';
108
+    }
109
+
110
+    ////////////////////////////////////////////////////////////////////
111
+    ///////////////////////////// ADD CLASSES //////////////////////////
112
+    ////////////////////////////////////////////////////////////////////
113
+
114
+    public function getFieldClasses(Field $field, $classes = array())
115
+    {
116
+        if ($field->isButton()) {
117
+            $classes = $this->filterButtonClasses($classes);
118
+        } else {
119
+            $classes = $this->filterFieldClasses($classes);
120
+        }
121
+
122
+        return $this->addClassesToField($field, $classes);
123
+    }
124
+
125
+    public function getGroupClasses()
126
+    {
127
+        if ($this->app['former.form']->isOfType('horizontal')) {
128
+            return 'row';
129
+        } else {
130
+            return null;
131
+        }
132
+    }
133
+
134
+    /**
135
+     * Add label classes
136
+     *
137
+     * @return string|null An array of attributes with the label class
138
+     */
139
+    public function getLabelClasses()
140
+    {
141
+        if ($this->app['former.form']->isOfType('horizontal')) {
142
+            return $this->getFrameworkOption('wrappedLabelClasses');
143
+        } else {
144
+            return null;
145
+        }
146
+    }
147
+
148
+    public function getUneditableClasses()
149
+    {
150
+        return null;
151
+    }
152
+
153
+    public function getPlainTextClasses()
154
+    {
155
+        return null;
156
+    }
157
+
158
+    public function getFormClasses($type)
159
+    {
160
+        return null;
161
+    }
162
+
163
+    public function getActionClasses()
164
+    {
165
+        return null;
166
+    }
167
+
168
+    ////////////////////////////////////////////////////////////////////
169
+    //////////////////////////// RENDER BLOCKS /////////////////////////
170
+    ////////////////////////////////////////////////////////////////////
171
+
172
+    public function createHelp($text, $attributes = null)
173
+    {
174
+        if (is_null($attributes) or empty($attributes)) {
175
+            $attributes = $this->getFrameworkOption('error_classes');
176
+        }
177
+
178
+        return Element::create('span', $text, $attributes);
179
+    }
180
+
181
+    /**
182
+     * Render a disabled field
183
+     *
184
+     * @param Field $field
185
+     *
186
+     * @return Input
187
+     */
188
+    public function createDisabledField(Field $field)
189
+    {
190
+        $field->disabled();
191
+
192
+        return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
193
+    }
194
+
195
+    /**
196
+     * Render a plain text field
197
+     * Which fallback to a disabled field
198
+     *
199
+     * @param Field $field
200
+     *
201
+     * @return Input
202
+     */
203
+    public function createPlainTextField(Field $field)
204
+    {
205
+        return $this->createDisabledField($field);
206
+    }
207
+
208
+    ////////////////////////////////////////////////////////////////////
209
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
210
+    ////////////////////////////////////////////////////////////////////
211
+
212
+    /**
213
+     * Wrap an item to be prepended or appended to the current field.
214
+     * For Zurb we return the item and handle the wrapping in prependAppend
215
+     * as wrapping is dependent on whether we're prepending or appending.
216
+     *
217
+     * @return string A wrapped item
218
+     */
219
+    public function placeAround($item)
220
+    {
221
+        return $item;
222
+    }
223
+
224
+    /**
225
+     * Wrap a field with prepended and appended items
226
+     *
227
+     * @param  Field $field
228
+     * @param  array $prepend
229
+     * @param  array $append
230
+     *
231
+     * @return string A field concatented with prepended and/or appended items
232
+     */
233
+    public function prependAppend($field, $prepend, $append)
234
+    {
235
+        $return = '';
236
+
237
+        foreach ($prepend as $item) {
238
+            $return .= '<div class="large-2 small-3 columns"><span class="prefix">'.$item.'</span></div>';
239
+        }
240
+
241
+        $return .= '<div class="large-10 small-9 columns">'.$field->render().'</div>';
242
+
243
+        foreach ($append as $item) {
244
+            $return .= '<div class="large-2 small-3 columns"><span class="postfix">'.$item.'</span></div>';
245
+        }
246
+
247
+        return $return;
248
+    }
249
+
250
+    /**
251
+     * Wraps all label contents with potential additional tags.
252
+     *
253
+     * @param  string $label
254
+     *
255
+     * @return string A wrapped label
256
+     */
257
+    public function wrapLabel($label)
258
+    {
259
+        if ($this->app['former.form']->isOfType('horizontal')) {
260
+            return Element::create('div', $label)->addClass($this->labelWidth);
261
+        } else {
262
+            return $label;
263
+        }
264
+    }
265
+
266
+    /**
267
+     * Wraps all field contents with potential additional tags.
268
+     *
269
+     * @param  Field $field
270
+     *
271
+     * @return Element A wrapped field
272
+     */
273
+    public function wrapField($field)
274
+    {
275
+        if ($this->app['former.form']->isOfType('horizontal')) {
276
+            return Element::create('div', $field)->addClass($this->fieldWidth);
277
+        } else {
278
+            return $field;
279
+        }
280
+    }
281
+
282
+    /**
283
+     * Wrap actions block with potential additional tags
284
+     *
285
+     * @param  Actions $actions
286
+     *
287
+     * @return string A wrapped actions block
288
+     */
289
+    public function wrapActions($actions)
290
+    {
291
+        return $actions;
292
+    }
293 293
 }
Please login to merge, or discard this patch.
src/Former/Former.php 1 patch
Indentation   +482 added lines, -482 removed lines patch added patch discarded remove patch
@@ -13,486 +13,486 @@
 block discarded – undo
13 13
  */
14 14
 class Former
15 15
 {
16
-	// Instances
17
-	////////////////////////////////////////////////////////////////////
18
-
19
-
20
-	/**
21
-	 * The current environment
22
-	 *
23
-	 * @var \Illuminate\Container\Container
24
-	 */
25
-	protected $app;
26
-
27
-	/**
28
-	 * The Method Dispatcher
29
-	 *
30
-	 * @var MethodDispatcher
31
-	 */
32
-	protected $dispatch;
33
-
34
-	// Informations
35
-	////////////////////////////////////////////////////////////////////
36
-
37
-	/**
38
-	 * The form's errors
39
-	 *
40
-	 * @var Message
41
-	 */
42
-	protected $errors;
43
-
44
-	/**
45
-	 * An array of rules to use
46
-	 *
47
-	 * @var array
48
-	 */
49
-	protected $rules = array();
50
-
51
-	/**
52
-	 * An array of field macros
53
-	 *
54
-	 * @var array
55
-	 */
56
-	protected $macros = array();
57
-
58
-	/**
59
-	 * The labels created so far
60
-	 *
61
-	 * @var array
62
-	 */
63
-	public $labels = array();
64
-
65
-	/**
66
-	 * The IDs created so far
67
-	 *
68
-	 * @var array
69
-	 */
70
-	public $ids = array();
71
-
72
-	/**
73
-	 * A lookup table where the key is the input name,
74
-	 * and the value is number of times seen. This is
75
-	 * used to calculate unique ids.
76
-	 *
77
-	 * @var array
78
-	 */
79
-	public $names = array();
80
-
81
-	// Namespaces
82
-	////////////////////////////////////////////////////////////////////
83
-
84
-	/**
85
-	 * The namespace of Form elements
86
-	 */
87
-	const FORMSPACE = 'Former\Form\\';
88
-
89
-	/**
90
-	 * The namespace of fields
91
-	 */
92
-	const FIELDSPACE = 'Former\Form\Fields\\';
93
-
94
-	/**
95
-	 * Build a new Former instance
96
-	 *
97
-	 * @param Container        $app
98
-	 * @param MethodDispatcher $dispatcher
99
-	 */
100
-	public function __construct(Container $app, MethodDispatcher $dispatcher)
101
-	{
102
-		$this->app      = $app;
103
-		$this->dispatch = $dispatcher;
104
-	}
105
-
106
-	////////////////////////////////////////////////////////////////////
107
-	//////////////////////////// INTERFACE /////////////////////////////
108
-	////////////////////////////////////////////////////////////////////
109
-
110
-	/**
111
-	 * Acts as a router that redirects methods to all of Former classes
112
-	 *
113
-	 * @param  string $method     The method called
114
-	 * @param  array  $parameters An array of parameters
115
-	 *
116
-	 * @return mixed
117
-	 */
118
-	public function __call($method, $parameters)
119
-	{
120
-		// Dispatch to Form\Elements
121
-		// Explicitly check false since closeGroup() may return an empty string
122
-		if (($element = $this->dispatch->toElements($method, $parameters)) !== false) {
123
-			return $element;
124
-		}
125
-
126
-		// Dispatch to Form\Form
127
-		if ($form = $this->dispatch->toForm($method, $parameters)) {
128
-			$this->app->instance('former.form', $form);
129
-
130
-			return $this->app['former.form'];
131
-		}
132
-
133
-		// Dispatch to Form\Group
134
-		if ($group = $this->dispatch->toGroup($method, $parameters)) {
135
-			return $group;
136
-		}
137
-
138
-		// Dispatch to Form\Actions
139
-		if ($actions = $this->dispatch->toActions($method, $parameters)) {
140
-			return $actions;
141
-		}
142
-
143
-		// Dispatch to macros
144
-		if ($macro = $this->dispatch->toMacros($method, $parameters)) {
145
-			return $macro;
146
-		}
147
-
148
-		// Checking for any supplementary classes
149
-		$classes = explode('_', $method);
150
-		$method  = array_pop($classes);
151
-
152
-		// Dispatch to the different Form\Fields
153
-		$framework = isset($this->app['former.form.framework']) ? $this->app['former.form.framework'] : $this->app['former.framework'];
154
-		$field     = $this->dispatch->toFields($method, $parameters);
155
-
156
-		if ($field instanceof Field) {
157
-			$field = $framework->getFieldClasses($field, $classes);
158
-		}
159
-
160
-		// Else bind field
161
-		$this->app->instance('former.field', $field);
162
-
163
-		return $this->app['former.field'];
164
-	}
165
-
166
-	////////////////////////////////////////////////////////////////////
167
-	//////////////////////////////// MACROS ////////////////////////////
168
-	////////////////////////////////////////////////////////////////////
169
-
170
-	/**
171
-	 * Register a macro with Former
172
-	 *
173
-	 * @param  string   $name  The name of the macro
174
-	 * @param  Callable $macro The macro itself
175
-	 *
176
-	 * @return mixed
177
-	 */
178
-	public function macro($name, $macro)
179
-	{
180
-		$this->macros[$name] = $macro;
181
-	}
182
-
183
-	/**
184
-	 * Check if a macro exists
185
-	 *
186
-	 * @param  string $name
187
-	 *
188
-	 * @return boolean
189
-	 */
190
-	public function hasMacro($name)
191
-	{
192
-		return isset($this->macros[$name]);
193
-	}
194
-
195
-	/**
196
-	 * Get a registered macro
197
-	 *
198
-	 * @param  string $name
199
-	 *
200
-	 * @return Closure
201
-	 */
202
-	public function getMacro($name)
203
-	{
204
-		return $this->macros[$name];
205
-	}
206
-
207
-	////////////////////////////////////////////////////////////////////
208
-	///////////////////////////// POPULATOR ////////////////////////////
209
-	////////////////////////////////////////////////////////////////////
210
-
211
-	/**
212
-	 * Add values to populate the array
213
-	 *
214
-	 * @param mixed $values Can be an Eloquent object or an array
215
-	 */
216
-	public function populate($values)
217
-	{
218
-		$this->app['former.populator']->replace($values);
219
-	}
220
-
221
-	/**
222
-	 * Set the value of a particular field
223
-	 *
224
-	 * @param string $field The field's name
225
-	 * @param mixed  $value Its new value
226
-	 */
227
-	public function populateField($field, $value)
228
-	{
229
-		$this->app['former.populator']->put($field, $value);
230
-	}
231
-
232
-	/**
233
-	 * Get the value of a field
234
-	 *
235
-	 * @param string $field The field's name
236
-	 * @param null   $fallback
237
-	 *
238
-	 * @return mixed
239
-	 */
240
-	public function getValue($field, $fallback = null)
241
-	{
242
-		return $this->app['former.populator']->get($field, $fallback);
243
-	}
244
-
245
-	/**
246
-	 * Fetch a field value from both the new and old POST array
247
-	 *
248
-	 * @param  string $name     A field name
249
-	 * @param  string $fallback A fallback if nothing was found
250
-	 *
251
-	 * @return string           The results
252
-	 */
253
-	public function getPost($name, $fallback = null)
254
-	{
255
-		$name     = str_replace(array('[', ']'), array('.', ''), $name);
256
-		$name     = trim($name, '.');
257
-		$oldValue = $this->app['request']->old($name, $fallback);
258
-
259
-		return $this->app['request']->input($name, $oldValue, true);
260
-	}
261
-
262
-	////////////////////////////////////////////////////////////////////
263
-	////////////////////////////// TOOLKIT /////////////////////////////
264
-	////////////////////////////////////////////////////////////////////
265
-
266
-	/**
267
-	 * Set the errors to use for validations
268
-	 *
269
-	 * @param Message $validator The result from a validation
270
-	 *
271
-	 * @return  void
272
-	 */
273
-	public function withErrors($validator = null)
274
-	{
275
-		// Try to get the errors form the session
276
-		if ($this->app['session']->has('errors')) {
277
-			$this->errors = $this->app['session']->get('errors');
278
-		}
279
-
280
-		// If we're given a raw Validator, go fetch the errors in it
281
-		if ($validator instanceof Validator) {
282
-			$this->errors = $validator->getMessageBag();
283
-		} else {
284
-			if ($validator instanceof MessageBag) {
285
-				$this->errors = $validator;
286
-			}
287
-		}
288
-
289
-		return $this->errors;
290
-	}
291
-
292
-	/**
293
-	 * Add live validation rules
294
-	 *
295
-	 * @param  array *$rules An array of Laravel rules
296
-	 *
297
-	 * @return  void
298
-	 */
299
-	public function withRules()
300
-	{
301
-		$rules = call_user_func_array('array_merge', func_get_args());
302
-
303
-		// Parse the rules according to Laravel conventions
304
-		foreach ($rules as $name => $fieldRules) {
305
-			$expFieldRules = $fieldRules;
306
-			if (!is_array($expFieldRules)) {
307
-				$expFieldRules = explode('|', $expFieldRules);
308
-				$expFieldRules = array_map('trim', $expFieldRules);
309
-			}
310
-
311
-			foreach ($expFieldRules as $rule) {
312
-
313
-				$parameters = null;
314
-
315
-				if (($colon = strpos($rule, ':')) !== false) {
316
-					$rulename = substr($rule, 0, $colon);
317
-
318
-					/**
319
-					 * Regular expressions may contain commas and should not be divided by str_getcsv.
320
-					 * For regular expressions we are just using the complete expression as a parameter.
321
-					 */
322
-					if ($rulename !== 'regex') {
323
-						$parameters = str_getcsv(substr($rule, $colon + 1));
324
-					} else {
325
-						$parameters = [substr($rule, $colon + 1)];
326
-					}
327
-				}
328
-
329
-				// Exclude unsupported rules
330
-				$rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule;
331
-
332
-				// Store processed rule in Former's array
333
-				if (!isset($parameters)) {
334
-					$parameters = array();
335
-				}
336
-
337
-				$this->rules[$name][$rule] = $parameters;
338
-			}
339
-		}
340
-	}
341
-
342
-	/**
343
-	 * Switch the framework used by Former
344
-	 *
345
-	 * @param string $framework The name of the framework to use
346
-	 */
347
-	public function framework($framework = null)
348
-	{
349
-		if (!$framework) {
350
-			return $this->app['former.framework']->current();
351
-		}
352
-
353
-		$this->setOption('framework', $framework);
354
-
355
-		$framework = $this->getFrameworkInstance($framework);
356
-		$this->app->bind('former.framework', function($app) use ($framework) {
357
-			return $framework;
358
-		});
359
-	}
360
-
361
-	/**
362
-	 * Get a new framework instance
363
-	 *
364
-	 * @param string $framework
365
-	 *
366
-	 * @throws Exceptions\InvalidFrameworkException
367
-	 * @return \Former\Interfaces\FrameworkInterface
368
-	 */
369
-	public function getFrameworkInstance($framework)
370
-	{
371
-		$formerClass = __NAMESPACE__.'\Framework\\'.$framework;
372
-
373
-		//get interfaces of the given framework
374
-		$interfaces = class_exists($framework) ? class_implements($framework) : array();
375
-
376
-		if (class_exists($formerClass)) {
377
-			$returnClass = $formerClass;
378
-		} elseif (class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) {
379
-			// We have some outside class, lets return it.
380
-			$returnClass = $framework;
381
-		} else {
382
-			throw (new InvalidFrameworkException())->setFramework($framework);
383
-		}
384
-
385
-		return new $returnClass($this->app);
386
-	}
387
-
388
-	/**
389
-	 * Get an option from the config
390
-	 *
391
-	 * @param string $option  The option
392
-	 * @param mixed  $default Optional fallback
393
-	 *
394
-	 * @return mixed
395
-	 */
396
-	public function getOption($option, $default = null)
397
-	{
398
-		return $this->app['config']->get('former.'.$option, $default);
399
-	}
400
-
401
-	/**
402
-	 * Set an option on the config
403
-	 *
404
-	 * @param string $option
405
-	 * @param string $value
406
-	 */
407
-	public function setOption($option, $value)
408
-	{
409
-		return $this->app['config']->set('former.'.$option, $value);
410
-	}
411
-
412
-	////////////////////////////////////////////////////////////////////
413
-	////////////////////////////// BUILDERS ////////////////////////////
414
-	////////////////////////////////////////////////////////////////////
415
-
416
-	/**
417
-	 * Closes a form
418
-	 *
419
-	 * @return string A form closing tag
420
-	 */
421
-	public function close()
422
-	{
423
-		if ($this->app->bound('former.form')) {
424
-			$closing = $this->app['former.form']->close();
425
-		}
426
-
427
-		// Destroy instances
428
-		$instances = array('former.form', 'former.form.framework');
429
-		foreach ($instances as $instance) {
430
-			$this->app[$instance] = null;
431
-			unset($this->app[$instance]);
432
-		}
433
-
434
-		// Reset populator
435
-		$this->app['former.populator']->reset();
436
-
437
-		// Reset all values
438
-		$this->errors = null;
439
-		$this->rules  = array();
440
-
441
-		return isset($closing) ? $closing : null;
442
-	}
443
-
444
-	////////////////////////////////////////////////////////////////////
445
-	////////////////////////////// HELPERS /////////////////////////////
446
-	////////////////////////////////////////////////////////////////////
447
-
448
-	/**
449
-	 * Get the errors for the current field
450
-	 *
451
-	 * @param  string $name A field name
452
-	 *
453
-	 * @return string       An error message
454
-	 */
455
-	public function getErrors($name = null)
456
-	{
457
-		// Get name and translate array notation
458
-		if (!$name and $this->app['former.field']) {
459
-			$name = $this->app['former.field']->getName();
460
-
461
-			// Always return empty string for anonymous fields (i.e. fields with no name/id)
462
-			if (!$name) {
463
-				return '';
464
-			}
465
-		}
466
-
467
-		if ($this->errors and $name) {
468
-			$name = str_replace(array('[', ']'), array('.', ''), $name);
469
-			$name = trim($name, '.');
470
-
471
-			return $this->errors->first($name);
472
-		}
473
-
474
-		return $this->errors;
475
-	}
476
-
477
-	/**
478
-	 * Get a rule from the Rules array
479
-	 *
480
-	 * @param  string $name The field to fetch
481
-	 *
482
-	 * @return array        An array of rules
483
-	 */
484
-	public function getRules($name)
485
-	{
486
-		// Check the rules for the name as given
487
-		$ruleset = array_get($this->rules, $name);
488
-
489
-		// If no rules found, convert to dot notation and try again
490
-		if (is_null($ruleset)) {
491
-			$name = str_replace(array('[', ']'), array('.', ''), $name);
492
-			$name = trim($name, '.');
493
-			$ruleset = array_get($this->rules, $name);
494
-		}
495
-
496
-		return $ruleset;
497
-	}
16
+    // Instances
17
+    ////////////////////////////////////////////////////////////////////
18
+
19
+
20
+    /**
21
+     * The current environment
22
+     *
23
+     * @var \Illuminate\Container\Container
24
+     */
25
+    protected $app;
26
+
27
+    /**
28
+     * The Method Dispatcher
29
+     *
30
+     * @var MethodDispatcher
31
+     */
32
+    protected $dispatch;
33
+
34
+    // Informations
35
+    ////////////////////////////////////////////////////////////////////
36
+
37
+    /**
38
+     * The form's errors
39
+     *
40
+     * @var Message
41
+     */
42
+    protected $errors;
43
+
44
+    /**
45
+     * An array of rules to use
46
+     *
47
+     * @var array
48
+     */
49
+    protected $rules = array();
50
+
51
+    /**
52
+     * An array of field macros
53
+     *
54
+     * @var array
55
+     */
56
+    protected $macros = array();
57
+
58
+    /**
59
+     * The labels created so far
60
+     *
61
+     * @var array
62
+     */
63
+    public $labels = array();
64
+
65
+    /**
66
+     * The IDs created so far
67
+     *
68
+     * @var array
69
+     */
70
+    public $ids = array();
71
+
72
+    /**
73
+     * A lookup table where the key is the input name,
74
+     * and the value is number of times seen. This is
75
+     * used to calculate unique ids.
76
+     *
77
+     * @var array
78
+     */
79
+    public $names = array();
80
+
81
+    // Namespaces
82
+    ////////////////////////////////////////////////////////////////////
83
+
84
+    /**
85
+     * The namespace of Form elements
86
+     */
87
+    const FORMSPACE = 'Former\Form\\';
88
+
89
+    /**
90
+     * The namespace of fields
91
+     */
92
+    const FIELDSPACE = 'Former\Form\Fields\\';
93
+
94
+    /**
95
+     * Build a new Former instance
96
+     *
97
+     * @param Container        $app
98
+     * @param MethodDispatcher $dispatcher
99
+     */
100
+    public function __construct(Container $app, MethodDispatcher $dispatcher)
101
+    {
102
+        $this->app      = $app;
103
+        $this->dispatch = $dispatcher;
104
+    }
105
+
106
+    ////////////////////////////////////////////////////////////////////
107
+    //////////////////////////// INTERFACE /////////////////////////////
108
+    ////////////////////////////////////////////////////////////////////
109
+
110
+    /**
111
+     * Acts as a router that redirects methods to all of Former classes
112
+     *
113
+     * @param  string $method     The method called
114
+     * @param  array  $parameters An array of parameters
115
+     *
116
+     * @return mixed
117
+     */
118
+    public function __call($method, $parameters)
119
+    {
120
+        // Dispatch to Form\Elements
121
+        // Explicitly check false since closeGroup() may return an empty string
122
+        if (($element = $this->dispatch->toElements($method, $parameters)) !== false) {
123
+            return $element;
124
+        }
125
+
126
+        // Dispatch to Form\Form
127
+        if ($form = $this->dispatch->toForm($method, $parameters)) {
128
+            $this->app->instance('former.form', $form);
129
+
130
+            return $this->app['former.form'];
131
+        }
132
+
133
+        // Dispatch to Form\Group
134
+        if ($group = $this->dispatch->toGroup($method, $parameters)) {
135
+            return $group;
136
+        }
137
+
138
+        // Dispatch to Form\Actions
139
+        if ($actions = $this->dispatch->toActions($method, $parameters)) {
140
+            return $actions;
141
+        }
142
+
143
+        // Dispatch to macros
144
+        if ($macro = $this->dispatch->toMacros($method, $parameters)) {
145
+            return $macro;
146
+        }
147
+
148
+        // Checking for any supplementary classes
149
+        $classes = explode('_', $method);
150
+        $method  = array_pop($classes);
151
+
152
+        // Dispatch to the different Form\Fields
153
+        $framework = isset($this->app['former.form.framework']) ? $this->app['former.form.framework'] : $this->app['former.framework'];
154
+        $field     = $this->dispatch->toFields($method, $parameters);
155
+
156
+        if ($field instanceof Field) {
157
+            $field = $framework->getFieldClasses($field, $classes);
158
+        }
159
+
160
+        // Else bind field
161
+        $this->app->instance('former.field', $field);
162
+
163
+        return $this->app['former.field'];
164
+    }
165
+
166
+    ////////////////////////////////////////////////////////////////////
167
+    //////////////////////////////// MACROS ////////////////////////////
168
+    ////////////////////////////////////////////////////////////////////
169
+
170
+    /**
171
+     * Register a macro with Former
172
+     *
173
+     * @param  string   $name  The name of the macro
174
+     * @param  Callable $macro The macro itself
175
+     *
176
+     * @return mixed
177
+     */
178
+    public function macro($name, $macro)
179
+    {
180
+        $this->macros[$name] = $macro;
181
+    }
182
+
183
+    /**
184
+     * Check if a macro exists
185
+     *
186
+     * @param  string $name
187
+     *
188
+     * @return boolean
189
+     */
190
+    public function hasMacro($name)
191
+    {
192
+        return isset($this->macros[$name]);
193
+    }
194
+
195
+    /**
196
+     * Get a registered macro
197
+     *
198
+     * @param  string $name
199
+     *
200
+     * @return Closure
201
+     */
202
+    public function getMacro($name)
203
+    {
204
+        return $this->macros[$name];
205
+    }
206
+
207
+    ////////////////////////////////////////////////////////////////////
208
+    ///////////////////////////// POPULATOR ////////////////////////////
209
+    ////////////////////////////////////////////////////////////////////
210
+
211
+    /**
212
+     * Add values to populate the array
213
+     *
214
+     * @param mixed $values Can be an Eloquent object or an array
215
+     */
216
+    public function populate($values)
217
+    {
218
+        $this->app['former.populator']->replace($values);
219
+    }
220
+
221
+    /**
222
+     * Set the value of a particular field
223
+     *
224
+     * @param string $field The field's name
225
+     * @param mixed  $value Its new value
226
+     */
227
+    public function populateField($field, $value)
228
+    {
229
+        $this->app['former.populator']->put($field, $value);
230
+    }
231
+
232
+    /**
233
+     * Get the value of a field
234
+     *
235
+     * @param string $field The field's name
236
+     * @param null   $fallback
237
+     *
238
+     * @return mixed
239
+     */
240
+    public function getValue($field, $fallback = null)
241
+    {
242
+        return $this->app['former.populator']->get($field, $fallback);
243
+    }
244
+
245
+    /**
246
+     * Fetch a field value from both the new and old POST array
247
+     *
248
+     * @param  string $name     A field name
249
+     * @param  string $fallback A fallback if nothing was found
250
+     *
251
+     * @return string           The results
252
+     */
253
+    public function getPost($name, $fallback = null)
254
+    {
255
+        $name     = str_replace(array('[', ']'), array('.', ''), $name);
256
+        $name     = trim($name, '.');
257
+        $oldValue = $this->app['request']->old($name, $fallback);
258
+
259
+        return $this->app['request']->input($name, $oldValue, true);
260
+    }
261
+
262
+    ////////////////////////////////////////////////////////////////////
263
+    ////////////////////////////// TOOLKIT /////////////////////////////
264
+    ////////////////////////////////////////////////////////////////////
265
+
266
+    /**
267
+     * Set the errors to use for validations
268
+     *
269
+     * @param Message $validator The result from a validation
270
+     *
271
+     * @return  void
272
+     */
273
+    public function withErrors($validator = null)
274
+    {
275
+        // Try to get the errors form the session
276
+        if ($this->app['session']->has('errors')) {
277
+            $this->errors = $this->app['session']->get('errors');
278
+        }
279
+
280
+        // If we're given a raw Validator, go fetch the errors in it
281
+        if ($validator instanceof Validator) {
282
+            $this->errors = $validator->getMessageBag();
283
+        } else {
284
+            if ($validator instanceof MessageBag) {
285
+                $this->errors = $validator;
286
+            }
287
+        }
288
+
289
+        return $this->errors;
290
+    }
291
+
292
+    /**
293
+     * Add live validation rules
294
+     *
295
+     * @param  array *$rules An array of Laravel rules
296
+     *
297
+     * @return  void
298
+     */
299
+    public function withRules()
300
+    {
301
+        $rules = call_user_func_array('array_merge', func_get_args());
302
+
303
+        // Parse the rules according to Laravel conventions
304
+        foreach ($rules as $name => $fieldRules) {
305
+            $expFieldRules = $fieldRules;
306
+            if (!is_array($expFieldRules)) {
307
+                $expFieldRules = explode('|', $expFieldRules);
308
+                $expFieldRules = array_map('trim', $expFieldRules);
309
+            }
310
+
311
+            foreach ($expFieldRules as $rule) {
312
+
313
+                $parameters = null;
314
+
315
+                if (($colon = strpos($rule, ':')) !== false) {
316
+                    $rulename = substr($rule, 0, $colon);
317
+
318
+                    /**
319
+                     * Regular expressions may contain commas and should not be divided by str_getcsv.
320
+                     * For regular expressions we are just using the complete expression as a parameter.
321
+                     */
322
+                    if ($rulename !== 'regex') {
323
+                        $parameters = str_getcsv(substr($rule, $colon + 1));
324
+                    } else {
325
+                        $parameters = [substr($rule, $colon + 1)];
326
+                    }
327
+                }
328
+
329
+                // Exclude unsupported rules
330
+                $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule;
331
+
332
+                // Store processed rule in Former's array
333
+                if (!isset($parameters)) {
334
+                    $parameters = array();
335
+                }
336
+
337
+                $this->rules[$name][$rule] = $parameters;
338
+            }
339
+        }
340
+    }
341
+
342
+    /**
343
+     * Switch the framework used by Former
344
+     *
345
+     * @param string $framework The name of the framework to use
346
+     */
347
+    public function framework($framework = null)
348
+    {
349
+        if (!$framework) {
350
+            return $this->app['former.framework']->current();
351
+        }
352
+
353
+        $this->setOption('framework', $framework);
354
+
355
+        $framework = $this->getFrameworkInstance($framework);
356
+        $this->app->bind('former.framework', function($app) use ($framework) {
357
+            return $framework;
358
+        });
359
+    }
360
+
361
+    /**
362
+     * Get a new framework instance
363
+     *
364
+     * @param string $framework
365
+     *
366
+     * @throws Exceptions\InvalidFrameworkException
367
+     * @return \Former\Interfaces\FrameworkInterface
368
+     */
369
+    public function getFrameworkInstance($framework)
370
+    {
371
+        $formerClass = __NAMESPACE__.'\Framework\\'.$framework;
372
+
373
+        //get interfaces of the given framework
374
+        $interfaces = class_exists($framework) ? class_implements($framework) : array();
375
+
376
+        if (class_exists($formerClass)) {
377
+            $returnClass = $formerClass;
378
+        } elseif (class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) {
379
+            // We have some outside class, lets return it.
380
+            $returnClass = $framework;
381
+        } else {
382
+            throw (new InvalidFrameworkException())->setFramework($framework);
383
+        }
384
+
385
+        return new $returnClass($this->app);
386
+    }
387
+
388
+    /**
389
+     * Get an option from the config
390
+     *
391
+     * @param string $option  The option
392
+     * @param mixed  $default Optional fallback
393
+     *
394
+     * @return mixed
395
+     */
396
+    public function getOption($option, $default = null)
397
+    {
398
+        return $this->app['config']->get('former.'.$option, $default);
399
+    }
400
+
401
+    /**
402
+     * Set an option on the config
403
+     *
404
+     * @param string $option
405
+     * @param string $value
406
+     */
407
+    public function setOption($option, $value)
408
+    {
409
+        return $this->app['config']->set('former.'.$option, $value);
410
+    }
411
+
412
+    ////////////////////////////////////////////////////////////////////
413
+    ////////////////////////////// BUILDERS ////////////////////////////
414
+    ////////////////////////////////////////////////////////////////////
415
+
416
+    /**
417
+     * Closes a form
418
+     *
419
+     * @return string A form closing tag
420
+     */
421
+    public function close()
422
+    {
423
+        if ($this->app->bound('former.form')) {
424
+            $closing = $this->app['former.form']->close();
425
+        }
426
+
427
+        // Destroy instances
428
+        $instances = array('former.form', 'former.form.framework');
429
+        foreach ($instances as $instance) {
430
+            $this->app[$instance] = null;
431
+            unset($this->app[$instance]);
432
+        }
433
+
434
+        // Reset populator
435
+        $this->app['former.populator']->reset();
436
+
437
+        // Reset all values
438
+        $this->errors = null;
439
+        $this->rules  = array();
440
+
441
+        return isset($closing) ? $closing : null;
442
+    }
443
+
444
+    ////////////////////////////////////////////////////////////////////
445
+    ////////////////////////////// HELPERS /////////////////////////////
446
+    ////////////////////////////////////////////////////////////////////
447
+
448
+    /**
449
+     * Get the errors for the current field
450
+     *
451
+     * @param  string $name A field name
452
+     *
453
+     * @return string       An error message
454
+     */
455
+    public function getErrors($name = null)
456
+    {
457
+        // Get name and translate array notation
458
+        if (!$name and $this->app['former.field']) {
459
+            $name = $this->app['former.field']->getName();
460
+
461
+            // Always return empty string for anonymous fields (i.e. fields with no name/id)
462
+            if (!$name) {
463
+                return '';
464
+            }
465
+        }
466
+
467
+        if ($this->errors and $name) {
468
+            $name = str_replace(array('[', ']'), array('.', ''), $name);
469
+            $name = trim($name, '.');
470
+
471
+            return $this->errors->first($name);
472
+        }
473
+
474
+        return $this->errors;
475
+    }
476
+
477
+    /**
478
+     * Get a rule from the Rules array
479
+     *
480
+     * @param  string $name The field to fetch
481
+     *
482
+     * @return array        An array of rules
483
+     */
484
+    public function getRules($name)
485
+    {
486
+        // Check the rules for the name as given
487
+        $ruleset = array_get($this->rules, $name);
488
+
489
+        // If no rules found, convert to dot notation and try again
490
+        if (is_null($ruleset)) {
491
+            $name = str_replace(array('[', ']'), array('.', ''), $name);
492
+            $name = trim($name, '.');
493
+            $ruleset = array_get($this->rules, $name);
494
+        }
495
+
496
+        return $ruleset;
497
+    }
498 498
 }
Please login to merge, or discard this patch.
src/Former/Helpers.php 1 patch
Indentation   +187 added lines, -187 removed lines patch added patch discarded remove patch
@@ -9,191 +9,191 @@
 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 $value The string to encode
33
-	 *
34
-	 * @return string
35
-	 */
36
-	public static function encode($value)
37
-	{
38
-		return htmlentities($value, ENT_QUOTES, 'UTF-8', true);
39
-	}
40
-
41
-	////////////////////////////////////////////////////////////////////
42
-	///////////////////////// LOCALIZATION HELPERS /////////////////////
43
-	////////////////////////////////////////////////////////////////////
44
-
45
-	/**
46
-	 * Translates a string by trying several fallbacks
47
-	 *
48
-	 * @param  string $key      The key to translate
49
-	 * @param  string $fallback The ultimate fallback
50
-	 *
51
-	 * @return string           A translated string
52
-	 */
53
-	public static function translate($key, $fallback = null)
54
-	{
55
-		// If nothing was given, return nothing, bitch
56
-		if (!$key) {
57
-			return null;
58
-		}
59
-
60
-		// If no fallback, use the key
61
-		if (!$fallback) {
62
-			$fallback = $key;
63
-		}
64
-
65
-		// Assure we don't already have a Lang object
66
-		if (is_object($key) and method_exists($key, 'get')) {
67
-			return $key->get();
68
-		}
69
-
70
-		$translation   = null;
71
-		$translateFrom = static::$app['former']->getOption('translate_from');
72
-		if (substr($translateFrom, -1) !== '/') {
73
-			$translateFrom .= '.';
74
-		}
75
-		$translateFrom .= $key;
76
-
77
-		// Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']]
78
-		if (strpos($translateFrom, ']') !== false) {
79
-			$translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom);
80
-		}
81
-
82
-		// Search for the key itself, if it is valid
83
-		$validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1;
84
-		if ($validKey && static::$app['translator']->has($key)) {
85
-			$translation = static::$app['translator']->get($key);
86
-		} elseif (static::$app['translator']->has($translateFrom)) {
87
-			$translation = static::$app['translator']->get($translateFrom);
88
-		}
89
-
90
-		// Replace by fallback if invalid
91
-		if (!$translation or is_array($translation)) {
92
-			$translation = $fallback;
93
-		}
94
-
95
-		// Capitalize
96
-		$capitalize = static::$app['former']->getOption('capitalize_translations');
97
-
98
-		return $capitalize ? ucfirst($translation) : $translation;
99
-	}
100
-
101
-	////////////////////////////////////////////////////////////////////
102
-	////////////////////////// DATABASE HELPERS ////////////////////////
103
-	////////////////////////////////////////////////////////////////////
104
-
105
-	/**
106
-	 * Transforms an array of models into an associative array
107
-	 *
108
-	 * @param  array|object    $query      The array of results
109
-	 * @param  string|function $text       The value to use as text
110
-	 * @param  string|array    $attributes The data to use as attributes
111
-	 *
112
-	 * @return array               A data array
113
-	 */
114
-	public static function queryToArray($query, $text = null, $attributes = null)
115
-	{
116
-		// Automatically fetch Lang objects for people who store translated options lists
117
-		// Same of unfetched queries
118
-		if (!$query instanceof Collection) {
119
-			if (method_exists($query, 'get')) {
120
-				$query = $query->get();
121
-			}
122
-			if (!is_array($query)) {
123
-				$query = (array) $query;
124
-			}
125
-		}
126
-
127
-		//Convert parametrs of old format to new format
128
-		if (!is_callable($text)) {
129
-			$optionTextValue = $text;
130
-			$text = function($model) use($optionTextValue) {
131
-				if ($optionTextValue and isset($model->$optionTextValue)) {
132
-					return $model->$optionTextValue;
133
-				} elseif (method_exists($model, '__toString')) {
134
-					return  $model->__toString();
135
-				} else {
136
-					return null;
137
-				}
138
-			};
139
-		}
140
-
141
-		if (!is_array($attributes)) {
142
-			if (is_string($attributes)) {
143
-				$attributes = ['value' => $attributes];
144
-			} else {
145
-				$attributes = ['value' => null];
146
-			}
147
-		}
148
-
149
-		if (!isset($attributes['value'])) {
150
-			$attributes['value'] = null;
151
-		}
152
-		//-------------------------------------------------
153
-
154
-		// Populates the new options
155
-		foreach ($query as $model) {
156
-			// If it's an array, convert to object
157
-			if (is_array($model)) {
158
-				$model = (object) $model;
159
-			}
160
-
161
-			// Calculate option text
162
-			$optionText = $text($model);
163
-
164
-			// Skip if no text value found
165
-			if (!$optionText) {
166
-				continue;
167
-			}
168
-
169
-			//Collect option attributes
170
-			foreach ($attributes as $optionAttributeName => $modelAttributeName) {
171
-				if (is_callable($modelAttributeName)) {
172
-					$optionAttributeValue = $modelAttributeName($model);
173
-				} elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
174
-					$optionAttributeValue = $model->$modelAttributeName;
175
-				} elseif ($optionAttributeName === 'value') {
176
-					//For backward compatibility
177
-					if (method_exists($model, 'getKey')) {
178
-						$optionAttributeValue = $model->getKey();
179
-					} elseif (isset($model->id)) {
180
-						$optionAttributeValue = $model->id;
181
-					} else {
182
-						$optionAttributeValue = $optionText;
183
-					}
184
-				} else {
185
-					$optionAttributeValue = '';
186
-				}
187
-
188
-				//For backward compatibility
189
-				if (count($attributes) === 1) {
190
-					$array[$optionAttributeValue] = (string) $optionText;
191
-				} else {
192
-					$array[$optionText][$optionAttributeName] = (string) $optionAttributeValue;
193
-				}
194
-			}
195
-		}
196
-
197
-		return !empty($array) ? $array : $query;
198
-	}
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 $value The string to encode
33
+     *
34
+     * @return string
35
+     */
36
+    public static function encode($value)
37
+    {
38
+        return htmlentities($value, ENT_QUOTES, 'UTF-8', true);
39
+    }
40
+
41
+    ////////////////////////////////////////////////////////////////////
42
+    ///////////////////////// LOCALIZATION HELPERS /////////////////////
43
+    ////////////////////////////////////////////////////////////////////
44
+
45
+    /**
46
+     * Translates a string by trying several fallbacks
47
+     *
48
+     * @param  string $key      The key to translate
49
+     * @param  string $fallback The ultimate fallback
50
+     *
51
+     * @return string           A translated string
52
+     */
53
+    public static function translate($key, $fallback = null)
54
+    {
55
+        // If nothing was given, return nothing, bitch
56
+        if (!$key) {
57
+            return null;
58
+        }
59
+
60
+        // If no fallback, use the key
61
+        if (!$fallback) {
62
+            $fallback = $key;
63
+        }
64
+
65
+        // Assure we don't already have a Lang object
66
+        if (is_object($key) and method_exists($key, 'get')) {
67
+            return $key->get();
68
+        }
69
+
70
+        $translation   = null;
71
+        $translateFrom = static::$app['former']->getOption('translate_from');
72
+        if (substr($translateFrom, -1) !== '/') {
73
+            $translateFrom .= '.';
74
+        }
75
+        $translateFrom .= $key;
76
+
77
+        // Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']]
78
+        if (strpos($translateFrom, ']') !== false) {
79
+            $translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom);
80
+        }
81
+
82
+        // Search for the key itself, if it is valid
83
+        $validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1;
84
+        if ($validKey && static::$app['translator']->has($key)) {
85
+            $translation = static::$app['translator']->get($key);
86
+        } elseif (static::$app['translator']->has($translateFrom)) {
87
+            $translation = static::$app['translator']->get($translateFrom);
88
+        }
89
+
90
+        // Replace by fallback if invalid
91
+        if (!$translation or is_array($translation)) {
92
+            $translation = $fallback;
93
+        }
94
+
95
+        // Capitalize
96
+        $capitalize = static::$app['former']->getOption('capitalize_translations');
97
+
98
+        return $capitalize ? ucfirst($translation) : $translation;
99
+    }
100
+
101
+    ////////////////////////////////////////////////////////////////////
102
+    ////////////////////////// DATABASE HELPERS ////////////////////////
103
+    ////////////////////////////////////////////////////////////////////
104
+
105
+    /**
106
+     * Transforms an array of models into an associative array
107
+     *
108
+     * @param  array|object    $query      The array of results
109
+     * @param  string|function $text       The value to use as text
110
+     * @param  string|array    $attributes The data to use as attributes
111
+     *
112
+     * @return array               A data array
113
+     */
114
+    public static function queryToArray($query, $text = null, $attributes = null)
115
+    {
116
+        // Automatically fetch Lang objects for people who store translated options lists
117
+        // Same of unfetched queries
118
+        if (!$query instanceof Collection) {
119
+            if (method_exists($query, 'get')) {
120
+                $query = $query->get();
121
+            }
122
+            if (!is_array($query)) {
123
+                $query = (array) $query;
124
+            }
125
+        }
126
+
127
+        //Convert parametrs of old format to new format
128
+        if (!is_callable($text)) {
129
+            $optionTextValue = $text;
130
+            $text = function($model) use($optionTextValue) {
131
+                if ($optionTextValue and isset($model->$optionTextValue)) {
132
+                    return $model->$optionTextValue;
133
+                } elseif (method_exists($model, '__toString')) {
134
+                    return  $model->__toString();
135
+                } else {
136
+                    return null;
137
+                }
138
+            };
139
+        }
140
+
141
+        if (!is_array($attributes)) {
142
+            if (is_string($attributes)) {
143
+                $attributes = ['value' => $attributes];
144
+            } else {
145
+                $attributes = ['value' => null];
146
+            }
147
+        }
148
+
149
+        if (!isset($attributes['value'])) {
150
+            $attributes['value'] = null;
151
+        }
152
+        //-------------------------------------------------
153
+
154
+        // Populates the new options
155
+        foreach ($query as $model) {
156
+            // If it's an array, convert to object
157
+            if (is_array($model)) {
158
+                $model = (object) $model;
159
+            }
160
+
161
+            // Calculate option text
162
+            $optionText = $text($model);
163
+
164
+            // Skip if no text value found
165
+            if (!$optionText) {
166
+                continue;
167
+            }
168
+
169
+            //Collect option attributes
170
+            foreach ($attributes as $optionAttributeName => $modelAttributeName) {
171
+                if (is_callable($modelAttributeName)) {
172
+                    $optionAttributeValue = $modelAttributeName($model);
173
+                } elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
174
+                    $optionAttributeValue = $model->$modelAttributeName;
175
+                } elseif ($optionAttributeName === 'value') {
176
+                    //For backward compatibility
177
+                    if (method_exists($model, 'getKey')) {
178
+                        $optionAttributeValue = $model->getKey();
179
+                    } elseif (isset($model->id)) {
180
+                        $optionAttributeValue = $model->id;
181
+                    } else {
182
+                        $optionAttributeValue = $optionText;
183
+                    }
184
+                } else {
185
+                    $optionAttributeValue = '';
186
+                }
187
+
188
+                //For backward compatibility
189
+                if (count($attributes) === 1) {
190
+                    $array[$optionAttributeValue] = (string) $optionText;
191
+                } else {
192
+                    $array[$optionText][$optionAttributeName] = (string) $optionAttributeValue;
193
+                }
194
+            }
195
+        }
196
+
197
+        return !empty($array) ? $array : $query;
198
+    }
199 199
 }
Please login to merge, or discard this patch.