Completed
Push — master ( c9f7e7...e4067b )
by Alex
45s queued 16s
created
src/Former/Exceptions/InvalidFrameworkException.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php namespace Former\Exceptions;
2 2
 
3
-class InvalidFrameworkException extends \RuntimeException{
3
+class InvalidFrameworkException extends \RuntimeException {
4 4
 
5 5
 	/**
6 6
 	 * reference to framework class
Please login to merge, or discard this patch.
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -2,33 +2,33 @@
 block discarded – undo
2 2
 
3 3
 class InvalidFrameworkException extends \RuntimeException{
4 4
 
5
-	/**
6
-	 * reference to framework class
7
-	 *
8
-	 * @var
9
-	 */
10
-	private $framework;
5
+    /**
6
+     * reference to framework class
7
+     *
8
+     * @var
9
+     */
10
+    private $framework;
11 11
 
12
-	/**
13
-	 * Set framework
14
-	 *
15
-	 * @param string $framework
16
-	 * @return $this
17
-	 */
18
-	public function setFramework($framework)
19
-	{
20
-		$this->framework = $framework;
21
-		$this->message = "Framework was not found [{$this->framework}]";
12
+    /**
13
+     * Set framework
14
+     *
15
+     * @param string $framework
16
+     * @return $this
17
+     */
18
+    public function setFramework($framework)
19
+    {
20
+        $this->framework = $framework;
21
+        $this->message = "Framework was not found [{$this->framework}]";
22 22
 
23
-		return $this;
24
-	}
25
-	/**
26
-	 * Gets the errors object.
27
-	 *
28
-	 * @return string
29
-	 */
30
-	public function getFramework()
31
-	{
32
-		return $this->framework;
33
-	}
23
+        return $this;
24
+    }
25
+    /**
26
+     * Gets the errors object.
27
+     *
28
+     * @return string
29
+     */
30
+    public function getFramework()
31
+    {
32
+        return $this->framework;
33
+    }
34 34
 }
Please login to merge, or discard this patch.
src/Former/Form/Actions.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -51,7 +51,7 @@
 block discarded – undo
51 51
 	 */
52 52
 	public function getContent()
53 53
 	{
54
-		$content = array_map(function ($content) {
54
+		$content = array_map(function($content) {
55 55
 			return method_exists($content, '__toString') ? (string) $content : $content;
56 56
 		}, $this->value);
57 57
 
Please login to merge, or discard this patch.
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -12,110 +12,110 @@
 block discarded – undo
12 12
  */
13 13
 class Actions extends FormerObject
14 14
 {
15
-	/**
16
-	 * The Container
17
-	 *
18
-	 * @var Container
19
-	 */
20
-	protected $app;
21
-
22
-	/**
23
-	 * The Actions element
24
-	 *
25
-	 * @var string
26
-	 */
27
-	protected $element = 'div';
28
-
29
-	////////////////////////////////////////////////////////////////////
30
-	/////////////////////////// CORE METHODS ///////////////////////////
31
-	////////////////////////////////////////////////////////////////////
32
-
33
-	/**
34
-	 * Constructs a new Actions block
35
-	 *
36
-	 * @param Container $app
37
-	 * @param array     $value The block content
38
-	 */
39
-	public function __construct(Container $app, $value)
40
-	{
41
-		$this->app   = $app;
42
-		$this->value = $value;
43
-
44
-		// Add specific actions classes to the actions block
45
-		$this->addClass($this->app['former.framework']->getActionClasses());
46
-	}
47
-
48
-	/**
49
-	 * Get the content of the Actions block
50
-	 *
51
-	 * @return string
52
-	 */
53
-	public function getContent()
54
-	{
55
-		$content = array_map(function ($content) {
56
-			return method_exists($content, '__toString') ? (string) $content : $content;
57
-		}, $this->value);
58
-
59
-		return $this->app['former.framework']->wrapActions(implode(' ', $content));
60
-	}
61
-
62
-	/**
63
-	 * Dynamically append actions to the block
64
-	 *
65
-	 * @param string $method     The method
66
-	 * @param array  $parameters Its parameters
67
-	 *
68
-	 * @return Actions
69
-	 */
70
-	public function __call($method, $parameters)
71
-	{
72
-		// Dynamically add buttons to an actions block
73
-		if ($this->isButtonMethod($method)) {
74
-			$text       = Arr::get($parameters, 0);
75
-			$link       = Arr::get($parameters, 1);
76
-			$attributes = Arr::get($parameters, 2);
77
-			if (!$attributes and is_array($link)) {
78
-				$attributes = $link;
79
-			}
80
-
81
-			return $this->createButtonOfType($method, $text, $link, $attributes);
82
-		}
83
-
84
-		return parent::__call($method, $parameters);
85
-	}
86
-
87
-	////////////////////////////////////////////////////////////////////
88
-	////////////////////////////// HELPERS /////////////////////////////
89
-	////////////////////////////////////////////////////////////////////
90
-
91
-	/**
92
-	 * Create a new Button and add it to the actions
93
-	 *
94
-	 * @param string $type       The button type
95
-	 * @param string $name       Its name
96
-	 * @param string $link       A link to point to
97
-	 * @param array  $attributes Its attributes
98
-	 *
99
-	 * @return Actions
100
-	 */
101
-	private function createButtonOfType($type, $name, $link, $attributes)
102
-	{
103
-		$this->value[] = $this->app['former']->$type($name, $link, $attributes)->__toString();
104
-
105
-		return $this;
106
-	}
107
-
108
-	/**
109
-	 * Check if a given method calls a button or not
110
-	 *
111
-	 * @param string $method The method to check
112
-	 *
113
-	 * @return boolean
114
-	 */
115
-	private function isButtonMethod($method)
116
-	{
117
-		$buttons = array('button', 'submit', 'link', 'reset');
118
-
119
-		return (bool) Str::contains($method, $buttons);
120
-	}
15
+    /**
16
+     * The Container
17
+     *
18
+     * @var Container
19
+     */
20
+    protected $app;
21
+
22
+    /**
23
+     * The Actions element
24
+     *
25
+     * @var string
26
+     */
27
+    protected $element = 'div';
28
+
29
+    ////////////////////////////////////////////////////////////////////
30
+    /////////////////////////// CORE METHODS ///////////////////////////
31
+    ////////////////////////////////////////////////////////////////////
32
+
33
+    /**
34
+     * Constructs a new Actions block
35
+     *
36
+     * @param Container $app
37
+     * @param array     $value The block content
38
+     */
39
+    public function __construct(Container $app, $value)
40
+    {
41
+        $this->app   = $app;
42
+        $this->value = $value;
43
+
44
+        // Add specific actions classes to the actions block
45
+        $this->addClass($this->app['former.framework']->getActionClasses());
46
+    }
47
+
48
+    /**
49
+     * Get the content of the Actions block
50
+     *
51
+     * @return string
52
+     */
53
+    public function getContent()
54
+    {
55
+        $content = array_map(function ($content) {
56
+            return method_exists($content, '__toString') ? (string) $content : $content;
57
+        }, $this->value);
58
+
59
+        return $this->app['former.framework']->wrapActions(implode(' ', $content));
60
+    }
61
+
62
+    /**
63
+     * Dynamically append actions to the block
64
+     *
65
+     * @param string $method     The method
66
+     * @param array  $parameters Its parameters
67
+     *
68
+     * @return Actions
69
+     */
70
+    public function __call($method, $parameters)
71
+    {
72
+        // Dynamically add buttons to an actions block
73
+        if ($this->isButtonMethod($method)) {
74
+            $text       = Arr::get($parameters, 0);
75
+            $link       = Arr::get($parameters, 1);
76
+            $attributes = Arr::get($parameters, 2);
77
+            if (!$attributes and is_array($link)) {
78
+                $attributes = $link;
79
+            }
80
+
81
+            return $this->createButtonOfType($method, $text, $link, $attributes);
82
+        }
83
+
84
+        return parent::__call($method, $parameters);
85
+    }
86
+
87
+    ////////////////////////////////////////////////////////////////////
88
+    ////////////////////////////// HELPERS /////////////////////////////
89
+    ////////////////////////////////////////////////////////////////////
90
+
91
+    /**
92
+     * Create a new Button and add it to the actions
93
+     *
94
+     * @param string $type       The button type
95
+     * @param string $name       Its name
96
+     * @param string $link       A link to point to
97
+     * @param array  $attributes Its attributes
98
+     *
99
+     * @return Actions
100
+     */
101
+    private function createButtonOfType($type, $name, $link, $attributes)
102
+    {
103
+        $this->value[] = $this->app['former']->$type($name, $link, $attributes)->__toString();
104
+
105
+        return $this;
106
+    }
107
+
108
+    /**
109
+     * Check if a given method calls a button or not
110
+     *
111
+     * @param string $method The method to check
112
+     *
113
+     * @return boolean
114
+     */
115
+    private function isButtonMethod($method)
116
+    {
117
+        $buttons = array('button', 'submit', 'link', 'reset');
118
+
119
+        return (bool) Str::contains($method, $buttons);
120
+    }
121 121
 }
Please login to merge, or discard this patch.
src/Former/Form/Fields/Select.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
 			$this->name .= '[]';
91 91
 		}
92 92
 
93
-		if ( ! $this->value instanceOf \ArrayAccess) {
93
+		if (!$this->value instanceOf \ArrayAccess) {
94 94
 			$this->value = (array) $this->value;
95 95
 		}
96 96
 
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
 		foreach ($parent->getChildren() as $child) {
133 133
 			// Search by value
134 134
 
135
-			if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int)$value ) {
135
+			if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int) $value) {
136 136
 				$child->selected('selected');
137 137
 			}
138 138
 
Please login to merge, or discard this patch.
Indentation   +293 added lines, -293 removed lines patch added patch discarded remove patch
@@ -13,297 +13,297 @@
 block discarded – undo
13 13
 class Select extends Field
14 14
 {
15 15
 
16
-	/**
17
-	 * The select's placeholder
18
-	 *
19
-	 * @var string
20
-	 */
21
-	private $placeholder = null;
22
-
23
-	/**
24
-	 * The Select's options
25
-	 *
26
-	 * @var array
27
-	 */
28
-	protected $options;
29
-
30
-	/**
31
-	 * The select's element
32
-	 *
33
-	 * @var string
34
-	 */
35
-	protected $element = 'select';
36
-
37
-	/**
38
-	 * The select's self-closing state
39
-	 *
40
-	 * @var boolean
41
-	 */
42
-	protected $isSelfClosing = false;
43
-
44
-	////////////////////////////////////////////////////////////////////
45
-	/////////////////////////// CORE METHODS ///////////////////////////
46
-	////////////////////////////////////////////////////////////////////
47
-
48
-	/**
49
-	 * Easier arguments order for selects
50
-	 *
51
-	 * @param Container $app        The Container instance
52
-	 * @param string    $type       select
53
-	 * @param string    $name       Field name
54
-	 * @param string    $label      Its label
55
-	 * @param array     $options    The select's options
56
-	 * @param string    $selected   The selected option
57
-	 * @param array     $attributes Attributes
58
-	 */
59
-	public function __construct(Container $app, $type, $name, $label, $options, $selected, $attributes)
60
-	{
61
-		if ($selected) {
62
-			$this->value = $selected;
63
-		}
64
-		if ($options) {
65
-			$this->options($options);
66
-		}
67
-
68
-		parent::__construct($app, $type, $name, $label, $selected, $attributes);
69
-
70
-		// Nested models population
71
-		if (Str::contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) {
72
-			$this->fromQuery($this->value);
73
-			$this->value = $selected ?: null;
74
-		}
75
-	}
76
-
77
-	/**
78
-	 * Renders the select
79
-	 *
80
-	 * @return string A <select> tag
81
-	 */
82
-	public function render()
83
-	{
84
-		// Multiselects
85
-		if ($this->isOfType('multiselect')) {
86
-			if (!isset($this->attributes['id'])) {
87
-				$this->setAttribute('id', $this->name);
88
-			}
89
-
90
-			$this->multiple();
91
-			$this->name .= '[]';
92
-		}
93
-
94
-		if ( ! $this->value instanceOf \ArrayAccess) {
95
-			$this->value = (array) $this->value;
96
-		}
97
-
98
-		// Mark selected values as selected
99
-		if ($this->hasChildren() and !empty($this->value)) {
100
-			foreach ($this->value as $value) {
101
-				if (is_object($value) && method_exists($value, 'getKey')) {
102
-					$value = $value->getKey();
103
-				}
104
-				$this->selectValue($value);
105
-			}
106
-		}
107
-
108
-		// Add placeholder text if any
109
-		if ($placeholder = $this->getPlaceholder()) {
110
-			array_unshift($this->children, $placeholder);
111
-		}
112
-
113
-		$this->value = null;
114
-
115
-		return parent::render();
116
-	}
117
-
118
-	/**
119
-	 * Select a value in the field's children
120
-	 *
121
-	 * @param mixed   $value
122
-	 * @param Element $parent
123
-	 *
124
-	 * @return void
125
-	 */
126
-	protected function selectValue($value, $parent = null)
127
-	{
128
-		// If no parent element defined, use direct children
129
-		if (!$parent) {
130
-			$parent = $this;
131
-		}
132
-
133
-		foreach ($parent->getChildren() as $child) {
134
-			// Search by value
135
-
136
-			if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int)$value ) {
137
-				$child->selected('selected');
138
-			}
139
-
140
-			// Else iterate over subchilds
141
-			if ($child->hasChildren()) {
142
-				$this->selectValue($value, $child);
143
-			}
144
-		}
145
-	}
146
-
147
-	/**
148
-	 * Get the Select's placeholder
149
-	 *
150
-	 * @return Element
151
-	 */
152
-	protected function getPlaceholder()
153
-	{
154
-		if (!$this->placeholder) {
155
-			return false;
156
-		}
157
-
158
-		$attributes = array('value' => '', 'disabled' => 'disabled');
159
-		if (!$this->value) {
160
-			$attributes['selected'] = 'selected';
161
-		}
162
-
163
-		return Element::create('option', $this->placeholder, $attributes);
164
-	}
165
-
166
-	////////////////////////////////////////////////////////////////////
167
-	////////////////////////// FIELD METHODS ///////////////////////////
168
-	////////////////////////////////////////////////////////////////////
169
-
170
-	/**
171
-	 * Set the select options
172
-	 *
173
-	 * @param  array   $_options     The options as an array
174
-	 * @param  mixed   $selected     Facultative selected entry
175
-	 * @param  boolean $valuesAsKeys Whether the array's values should be used as
176
-	 *                               the option's values instead of the array's keys
177
-	 */
178
-	public function options($_options, $selected = null, $valuesAsKeys = false)
179
-	{
180
-		$options = array();
181
-
182
-		// If valuesAsKeys is true, use the values as keys
183
-		if ($valuesAsKeys) {
184
-			foreach ($_options as $v) {
185
-				$options[$v] = $v;
186
-			}
187
-		} else {
188
-			$options = $_options;
189
-		}
190
-
191
-		// Add the various options
192
-		foreach ($options as $value => $text) {
193
-			if (is_array($text) and isset($text['value'])) {
194
-				$attributes = $text;
195
-				$text       = $value;
196
-				$value      = null;
197
-			} else {
198
-				$attributes = array();
199
-			}
200
-			$this->addOption($text, $value, $attributes);
201
-		}
202
-
203
-		// Set the selected value
204
-		if (!is_null($selected)) {
205
-			$this->select($selected);
206
-		}
207
-
208
-		return $this;
209
-	}
210
-
211
-	/**
212
-	 * Creates a list of options from a range
213
-	 *
214
-	 * @param  integer $from
215
-	 * @param  integer $to
216
-	 * @param  integer $step
217
-	 */
218
-	public function range($from, $to, $step = 1)
219
-	{
220
-		$range = range($from, $to, $step);
221
-		$this->options($range, null, true);
222
-
223
-		return $this;
224
-	}
225
-
226
-	/**
227
-	 * Add an option to the Select's options
228
-	 *
229
-	 * @param array|string $text       It's value or an array of values
230
-	 * @param string       $value      It's text
231
-	 * @param array        $attributes The option's attributes
232
-	 */
233
-	public function addOption($text = null, $value = null, $attributes = array())
234
-	{
235
-		// Get the option's value
236
-		$childrenKey = !is_null($value) ? $value : sizeof($this->children);
237
-
238
-		// If we passed an options group
239
-		if (is_array($text)) {
240
-			$this->children[$childrenKey] = Element::create('optgroup')->label($value);
241
-			foreach ($text as $key => $value) {
242
-				$option = Element::create('option', $value)->setAttribute('value', $key);
243
-				$this->children[$childrenKey]->nest($option);
244
-			}
245
-			// Else if it's a simple option
246
-		} else {
247
-			if (!isset($attributes['value'])) {
248
-				$attributes['value'] = $value;
249
-			}
250
-
251
-			$this->children[$attributes['value']] = Element::create('option', $text)->setAttributes($attributes);
252
-		}
253
-
254
-		return $this;
255
-	}
256
-
257
-	/**
258
-	 * Use the results from a Fluent/Eloquent query as options
259
-	 *
260
-	 * @param  array           $results    An array of Eloquent models
261
-	 * @param  string|function $text       The value to use as text
262
-	 * @param  string|array    $attributes The data to use as attributes
263
-	 * @param  string	   $selected   The default selected item
264
-	 */
265
-	public function fromQuery($results, $text = null, $attributes = null, $selected = null)
266
-	{
267
-		$this->options(Helpers::queryToArray($results, $text, $attributes), $selected);
268
-
269
-		return $this;
270
-	}
271
-
272
-	/**
273
-	 * Select a particular list item
274
-	 *
275
-	 * @param  mixed $selected Selected item
276
-	 */
277
-	public function select($selected)
278
-	{
279
-		$this->value = $selected;
280
-
281
-		return $this;
282
-	}
283
-
284
-	/**
285
-	 * Add a placeholder to the current select
286
-	 *
287
-	 * @param  string $placeholder The placeholder text
288
-	 */
289
-	public function placeholder($placeholder)
290
-	{
291
-		$this->placeholder = Helpers::translate($placeholder);
292
-
293
-		return $this;
294
-	}
295
-
296
-	////////////////////////////////////////////////////////////////////
297
-	////////////////////////////// HELPERS /////////////////////////////
298
-	////////////////////////////////////////////////////////////////////
299
-
300
-	/**
301
-	 * Returns the current options in memory for manipulations
302
-	 *
303
-	 * @return array The current options array
304
-	 */
305
-	public function getOptions()
306
-	{
307
-		return $this->children;
308
-	}
16
+    /**
17
+     * The select's placeholder
18
+     *
19
+     * @var string
20
+     */
21
+    private $placeholder = null;
22
+
23
+    /**
24
+     * The Select's options
25
+     *
26
+     * @var array
27
+     */
28
+    protected $options;
29
+
30
+    /**
31
+     * The select's element
32
+     *
33
+     * @var string
34
+     */
35
+    protected $element = 'select';
36
+
37
+    /**
38
+     * The select's self-closing state
39
+     *
40
+     * @var boolean
41
+     */
42
+    protected $isSelfClosing = false;
43
+
44
+    ////////////////////////////////////////////////////////////////////
45
+    /////////////////////////// CORE METHODS ///////////////////////////
46
+    ////////////////////////////////////////////////////////////////////
47
+
48
+    /**
49
+     * Easier arguments order for selects
50
+     *
51
+     * @param Container $app        The Container instance
52
+     * @param string    $type       select
53
+     * @param string    $name       Field name
54
+     * @param string    $label      Its label
55
+     * @param array     $options    The select's options
56
+     * @param string    $selected   The selected option
57
+     * @param array     $attributes Attributes
58
+     */
59
+    public function __construct(Container $app, $type, $name, $label, $options, $selected, $attributes)
60
+    {
61
+        if ($selected) {
62
+            $this->value = $selected;
63
+        }
64
+        if ($options) {
65
+            $this->options($options);
66
+        }
67
+
68
+        parent::__construct($app, $type, $name, $label, $selected, $attributes);
69
+
70
+        // Nested models population
71
+        if (Str::contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) {
72
+            $this->fromQuery($this->value);
73
+            $this->value = $selected ?: null;
74
+        }
75
+    }
76
+
77
+    /**
78
+     * Renders the select
79
+     *
80
+     * @return string A <select> tag
81
+     */
82
+    public function render()
83
+    {
84
+        // Multiselects
85
+        if ($this->isOfType('multiselect')) {
86
+            if (!isset($this->attributes['id'])) {
87
+                $this->setAttribute('id', $this->name);
88
+            }
89
+
90
+            $this->multiple();
91
+            $this->name .= '[]';
92
+        }
93
+
94
+        if ( ! $this->value instanceOf \ArrayAccess) {
95
+            $this->value = (array) $this->value;
96
+        }
97
+
98
+        // Mark selected values as selected
99
+        if ($this->hasChildren() and !empty($this->value)) {
100
+            foreach ($this->value as $value) {
101
+                if (is_object($value) && method_exists($value, 'getKey')) {
102
+                    $value = $value->getKey();
103
+                }
104
+                $this->selectValue($value);
105
+            }
106
+        }
107
+
108
+        // Add placeholder text if any
109
+        if ($placeholder = $this->getPlaceholder()) {
110
+            array_unshift($this->children, $placeholder);
111
+        }
112
+
113
+        $this->value = null;
114
+
115
+        return parent::render();
116
+    }
117
+
118
+    /**
119
+     * Select a value in the field's children
120
+     *
121
+     * @param mixed   $value
122
+     * @param Element $parent
123
+     *
124
+     * @return void
125
+     */
126
+    protected function selectValue($value, $parent = null)
127
+    {
128
+        // If no parent element defined, use direct children
129
+        if (!$parent) {
130
+            $parent = $this;
131
+        }
132
+
133
+        foreach ($parent->getChildren() as $child) {
134
+            // Search by value
135
+
136
+            if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int)$value ) {
137
+                $child->selected('selected');
138
+            }
139
+
140
+            // Else iterate over subchilds
141
+            if ($child->hasChildren()) {
142
+                $this->selectValue($value, $child);
143
+            }
144
+        }
145
+    }
146
+
147
+    /**
148
+     * Get the Select's placeholder
149
+     *
150
+     * @return Element
151
+     */
152
+    protected function getPlaceholder()
153
+    {
154
+        if (!$this->placeholder) {
155
+            return false;
156
+        }
157
+
158
+        $attributes = array('value' => '', 'disabled' => 'disabled');
159
+        if (!$this->value) {
160
+            $attributes['selected'] = 'selected';
161
+        }
162
+
163
+        return Element::create('option', $this->placeholder, $attributes);
164
+    }
165
+
166
+    ////////////////////////////////////////////////////////////////////
167
+    ////////////////////////// FIELD METHODS ///////////////////////////
168
+    ////////////////////////////////////////////////////////////////////
169
+
170
+    /**
171
+     * Set the select options
172
+     *
173
+     * @param  array   $_options     The options as an array
174
+     * @param  mixed   $selected     Facultative selected entry
175
+     * @param  boolean $valuesAsKeys Whether the array's values should be used as
176
+     *                               the option's values instead of the array's keys
177
+     */
178
+    public function options($_options, $selected = null, $valuesAsKeys = false)
179
+    {
180
+        $options = array();
181
+
182
+        // If valuesAsKeys is true, use the values as keys
183
+        if ($valuesAsKeys) {
184
+            foreach ($_options as $v) {
185
+                $options[$v] = $v;
186
+            }
187
+        } else {
188
+            $options = $_options;
189
+        }
190
+
191
+        // Add the various options
192
+        foreach ($options as $value => $text) {
193
+            if (is_array($text) and isset($text['value'])) {
194
+                $attributes = $text;
195
+                $text       = $value;
196
+                $value      = null;
197
+            } else {
198
+                $attributes = array();
199
+            }
200
+            $this->addOption($text, $value, $attributes);
201
+        }
202
+
203
+        // Set the selected value
204
+        if (!is_null($selected)) {
205
+            $this->select($selected);
206
+        }
207
+
208
+        return $this;
209
+    }
210
+
211
+    /**
212
+     * Creates a list of options from a range
213
+     *
214
+     * @param  integer $from
215
+     * @param  integer $to
216
+     * @param  integer $step
217
+     */
218
+    public function range($from, $to, $step = 1)
219
+    {
220
+        $range = range($from, $to, $step);
221
+        $this->options($range, null, true);
222
+
223
+        return $this;
224
+    }
225
+
226
+    /**
227
+     * Add an option to the Select's options
228
+     *
229
+     * @param array|string $text       It's value or an array of values
230
+     * @param string       $value      It's text
231
+     * @param array        $attributes The option's attributes
232
+     */
233
+    public function addOption($text = null, $value = null, $attributes = array())
234
+    {
235
+        // Get the option's value
236
+        $childrenKey = !is_null($value) ? $value : sizeof($this->children);
237
+
238
+        // If we passed an options group
239
+        if (is_array($text)) {
240
+            $this->children[$childrenKey] = Element::create('optgroup')->label($value);
241
+            foreach ($text as $key => $value) {
242
+                $option = Element::create('option', $value)->setAttribute('value', $key);
243
+                $this->children[$childrenKey]->nest($option);
244
+            }
245
+            // Else if it's a simple option
246
+        } else {
247
+            if (!isset($attributes['value'])) {
248
+                $attributes['value'] = $value;
249
+            }
250
+
251
+            $this->children[$attributes['value']] = Element::create('option', $text)->setAttributes($attributes);
252
+        }
253
+
254
+        return $this;
255
+    }
256
+
257
+    /**
258
+     * Use the results from a Fluent/Eloquent query as options
259
+     *
260
+     * @param  array           $results    An array of Eloquent models
261
+     * @param  string|function $text       The value to use as text
262
+     * @param  string|array    $attributes The data to use as attributes
263
+     * @param  string	   $selected   The default selected item
264
+     */
265
+    public function fromQuery($results, $text = null, $attributes = null, $selected = null)
266
+    {
267
+        $this->options(Helpers::queryToArray($results, $text, $attributes), $selected);
268
+
269
+        return $this;
270
+    }
271
+
272
+    /**
273
+     * Select a particular list item
274
+     *
275
+     * @param  mixed $selected Selected item
276
+     */
277
+    public function select($selected)
278
+    {
279
+        $this->value = $selected;
280
+
281
+        return $this;
282
+    }
283
+
284
+    /**
285
+     * Add a placeholder to the current select
286
+     *
287
+     * @param  string $placeholder The placeholder text
288
+     */
289
+    public function placeholder($placeholder)
290
+    {
291
+        $this->placeholder = Helpers::translate($placeholder);
292
+
293
+        return $this;
294
+    }
295
+
296
+    ////////////////////////////////////////////////////////////////////
297
+    ////////////////////////////// HELPERS /////////////////////////////
298
+    ////////////////////////////////////////////////////////////////////
299
+
300
+    /**
301
+     * Returns the current options in memory for manipulations
302
+     *
303
+     * @return array The current options array
304
+     */
305
+    public function getOptions()
306
+    {
307
+        return $this->children;
308
+    }
309 309
 }
Please login to merge, or discard this patch.
src/Former/Form/Form.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
 		$this->url       = $url;
97 97
 		$this->populator = $populator;
98 98
 
99
-		$this->app->singleton('former.form.framework', function ($app) {
99
+		$this->app->singleton('former.form.framework', function($app) {
100 100
 			return clone $app['former.framework'];
101 101
 		});
102 102
 	}
@@ -402,7 +402,7 @@  discard block
 block discarded – undo
402 402
 
403 403
 		// If raw form
404 404
 		if ($type == 'raw') {
405
-			$this->app->bind('former.form.framework', function ($app) {
405
+			$this->app->bind('former.form.framework', function($app) {
406 406
 				return $app['former']->getFrameworkInstance('Nude');
407 407
 			});
408 408
 		}
Please login to merge, or discard this patch.
Indentation   +403 added lines, -403 removed lines patch added patch discarded remove patch
@@ -12,407 +12,407 @@
 block discarded – undo
12 12
  */
13 13
 class Form extends FormerObject
14 14
 {
15
-	/**
16
-	 * The IoC Container
17
-	 *
18
-	 * @var Container
19
-	 */
20
-	protected $app;
21
-
22
-	/**
23
-	 * The URL generator
24
-	 *
25
-	 * @var UrlGenerator
26
-	 */
27
-	protected $url;
28
-
29
-	/**
30
-	 * The Populator
31
-	 *
32
-	 * @var Populator
33
-	 */
34
-	protected $populator;
35
-
36
-	/**
37
-	 * The Form type
38
-	 *
39
-	 * @var string
40
-	 */
41
-	protected $type = null;
42
-
43
-	/**
44
-	 * The destination of the current form
45
-	 *
46
-	 * @var string
47
-	 */
48
-	protected $action;
49
-
50
-	/**
51
-	 * The form method
52
-	 *
53
-	 * @var string
54
-	 */
55
-	protected $method;
56
-
57
-	/**
58
-	 * Whether the form should be secured or not
59
-	 *
60
-	 * @var boolean
61
-	 */
62
-	protected $secure;
63
-
64
-	/**
65
-	 * The form element
66
-	 *
67
-	 * @var string
68
-	 */
69
-	protected $element = 'form';
70
-
71
-	/**
72
-	 * A list of injected properties
73
-	 *
74
-	 * @var array
75
-	 */
76
-	protected $injectedProperties = array('method', 'action');
77
-
78
-	/**
79
-	 * Whether a form is opened or not
80
-	 *
81
-	 * @var boolean
82
-	 */
83
-	protected static $opened = false;
84
-
85
-	////////////////////////////////////////////////////////////////////
86
-	/////////////////////////// CORE METHODS ///////////////////////////
87
-	////////////////////////////////////////////////////////////////////
88
-
89
-	/**
90
-	 * Build a new Form instance
91
-	 *
92
-	 * @param UrlGenerator $url
93
-	 */
94
-	public function __construct(Container $app, $url, Populator $populator)
95
-	{
96
-		$this->app       = $app;
97
-		$this->url       = $url;
98
-		$this->populator = $populator;
99
-
100
-		$this->app->singleton('former.form.framework', function ($app) {
101
-			return clone $app['former.framework'];
102
-		});
103
-	}
104
-
105
-	/**
106
-	 * Opens up magically a form
107
-	 *
108
-	 * @param  string $type       The form type asked
109
-	 * @param  array  $parameters Parameters passed
110
-	 *
111
-	 * @return Form             A form opening tag
112
-	 */
113
-	public function openForm($type, $parameters)
114
-	{
115
-		$action     = Arr::get($parameters, 0);
116
-		$method     = Arr::get($parameters, 1, 'POST');
117
-		$attributes = Arr::get($parameters, 2, array());
118
-		$secure     = Arr::get($parameters, 3, null);
119
-
120
-		// Fetch errors if asked for
121
-		if ($this->app['former']->getOption('fetch_errors')) {
122
-			$this->app['former']->withErrors();
123
-		}
124
-
125
-		// Open the form
126
-		$this->action($action);
127
-		$this->attributes = $attributes;
128
-		$this->method     = strtoupper($method);
129
-		$this->secure     = $secure;
130
-
131
-		// Add any effect of the form type
132
-		$type       = Str::snake($type);
133
-		$this->type = $this->applyType($type);
134
-
135
-		// Add enctype
136
-		if (!array_key_exists('accept-charset', $attributes)) {
137
-			$this->attributes['accept-charset'] = 'utf-8';
138
-		}
139
-
140
-		// Add supplementary classes
141
-		if ($this->type !== 'raw') {
142
-			$this->addClass($this->app['former.form.framework']->getFormClasses($this->type));
143
-		}
144
-
145
-		return $this;
146
-	}
147
-
148
-	/**
149
-	 * Closes a Form
150
-	 *
151
-	 * @return string A closing <form> tag
152
-	 */
153
-	public function close()
154
-	{
155
-		static::$opened = false;
156
-
157
-		// Add token if necessary
158
-		$closing = '</form>';
159
-		if ($this->method != 'GET') {
160
-			$closing = $this->app['former']->token().$closing;
161
-		}
162
-
163
-		return $closing;
164
-	}
165
-
166
-	////////////////////////////////////////////////////////////////////
167
-	//////////////////////////// STATIC HELPERS ////////////////////////
168
-	////////////////////////////////////////////////////////////////////
169
-
170
-	/**
171
-	 * Whether a form is currently opened or not
172
-	 *
173
-	 * @return boolean
174
-	 */
175
-	public static function hasInstanceOpened()
176
-	{
177
-		return static::$opened;
178
-	}
179
-
180
-	////////////////////////////////////////////////////////////////////
181
-	/////////////////////////////// SETTER /////////////////////////////
182
-	////////////////////////////////////////////////////////////////////
183
-
184
-	/**
185
-	 * Change the form's action
186
-	 *
187
-	 * @param  string $action The new action
188
-	 *
189
-	 * @return $this
190
-	 */
191
-	public function action($action)
192
-	{
193
-		$this->action = $action ? $this->url->to($action, array(), $this->secure) : null;
194
-
195
-		return $this;
196
-	}
197
-
198
-	/**
199
-	 * Change the form's method
200
-	 *
201
-	 * @param  string $method The method to use
202
-	 *
203
-	 * @return $this
204
-	 */
205
-	public function method($method)
206
-	{
207
-		$this->method = strtoupper($method);
208
-
209
-		return $this;
210
-	}
211
-
212
-	/**
213
-	 * Whether the form should be secure
214
-	 *
215
-	 * @param  boolean $secure Secure or not
216
-	 *
217
-	 * @return $this
218
-	 */
219
-	public function secure($secure = true)
220
-	{
221
-		$this->secure = $secure;
222
-
223
-		return $this;
224
-	}
225
-
226
-	/**
227
-	 * Change the form's action and method to a route
228
-	 *
229
-	 * @param  string $name   The name of the route to use
230
-	 * @param  array  $params Any route parameters
231
-	 *
232
-	 * @return Form
233
-	 */
234
-	public function route($name, $params = array())
235
-	{
236
-		return $this->setRouteOrAction($name, $params, 'route');
237
-	}
238
-
239
-	/**
240
-	 * Change the form's action to a controller method
241
-	 *
242
-	 * @param  string $name   The controller and method
243
-	 * @param  array  $params Any method parameters
244
-	 *
245
-	 * @return Form
246
-	 */
247
-	public function controller($name, $params = array())
248
-	{
249
-		return $this->setRouteOrAction($name, $params, 'action');
250
-	}
251
-
252
-	/**
253
-	 * Outputs the current form opened
254
-	 *
255
-	 * @return string A <form> opening tag
256
-	 */
257
-	public function __toString()
258
-	{
259
-		// Mark the form as opened
260
-		static::$opened = true;
261
-
262
-		// Add name to attributes
263
-		$this->attributes['name'] = $this->name;
264
-
265
-		// Add spoof method
266
-		if (in_array($this->method, array('PUT', 'PATCH', 'DELETE'))) {
267
-			$spoof        = $this->app['former']->hidden('_method', $this->method);
268
-			$this->method = 'POST';
269
-		} else {
270
-			$spoof = null;
271
-		}
272
-
273
-		return $this->open().$spoof;
274
-	}
275
-
276
-	////////////////////////////////////////////////////////////////////
277
-	////////////////////////// PUBLIC HELPERS //////////////////////////
278
-	////////////////////////////////////////////////////////////////////
279
-
280
-	/**
281
-	 * Alias for $this->app['former']->withRules
282
-	 */
283
-	public function rules()
284
-	{
285
-		call_user_func_array(array($this->app['former'], 'withRules'), func_get_args());
286
-
287
-		return $this;
288
-	}
289
-
290
-	/**
291
-	 * Populate a form with specific values
292
-	 *
293
-	 * @param array|object $values
294
-	 *
295
-	 * @return $this
296
-	 */
297
-	public function populate($values)
298
-	{
299
-		$this->populator->replace($values);
300
-
301
-		return $this;
302
-	}
303
-
304
-	/**
305
-	 * Get the Populator binded to the Form
306
-	 *
307
-	 * @return Populator
308
-	 */
309
-	public function getPopulator()
310
-	{
311
-		return $this->populator;
312
-	}
313
-
314
-	////////////////////////////////////////////////////////////////////
315
-	////////////////////////////// HELPERS /////////////////////////////
316
-	////////////////////////////////////////////////////////////////////
317
-
318
-	/**
319
-	 * Find the method of a route by its _uses or name
320
-	 *
321
-	 * @param  string $name
322
-	 *
323
-	 * @return string
324
-	 */
325
-	protected function findRouteMethod($name)
326
-	{
327
-		if (!$this->app->bound('router')) {
328
-			return;
329
-		}
330
-
331
-		// Get string by name
332
-		if (!Str::contains($name, '@')) {
333
-			$routes = $this->app['router']->getRoutes();
334
-			$route  = method_exists($routes, 'getByName') ? $routes->getByName($name) : $routes->get($name);
335
-			// Get string by uses
336
-		} else {
337
-			foreach ($this->app['router']->getRoutes() as $route) {
338
-				$routeUses = method_exists($route, 'getOption') ? $route->getOption('_uses') : Arr::get($route->getAction(), 'controller');
339
-				if ($action = $routeUses) {
340
-					if ($action == $name) {
341
-						break;
342
-					}
343
-				}
344
-			}
345
-		}
346
-
347
-		// Get method
348
-		$methods = method_exists($route, 'getMethods') ? $route->getMethods() : $route->methods();
349
-		$method  = Arr::get($methods, 0);
350
-
351
-		return $method;
352
-	}
353
-
354
-	/**
355
-	 * @param $name
356
-	 * @param $params
357
-	 * @param $type
358
-	 *
359
-	 * @return $this
360
-	 */
361
-	protected function setRouteOrAction($name, $params, $type)
362
-	{
363
-		// Set the form action
364
-		$this->action = $this->url->$type($name, $params);
365
-
366
-		// Set the proper method
367
-		if ($method = $this->findRouteMethod($name)) {
368
-			$this->method($method);
369
-		}
370
-
371
-		return $this;
372
-	}
373
-
374
-	/**
375
-	 * Apply various parameters according to form type
376
-	 *
377
-	 * @param  string $type The original form type provided
378
-	 *
379
-	 * @return string The final form type
380
-	 */
381
-	private function applyType($type)
382
-	{
383
-		// If classic form
384
-		if ($type == 'open') {
385
-			return $this->app['former']->getOption('default_form_type');
386
-		}
387
-
388
-		// Look for HTTPS form
389
-		if (Str::contains($type, 'secure')) {
390
-			$type         = str_replace('secure', '', $type);
391
-			$this->secure = true;
392
-		}
393
-
394
-		// Look for file form
395
-		if (Str::contains($type, 'for_files')) {
396
-			$type                        = str_replace('for_files', '', $type);
397
-			$this->attributes['enctype'] = 'multipart/form-data';
398
-		}
399
-
400
-		// Calculate form type
401
-		$type = str_replace('open', '', $type);
402
-		$type = trim($type, '_');
403
-
404
-		// If raw form
405
-		if ($type == 'raw') {
406
-			$this->app->bind('former.form.framework', function ($app) {
407
-				return $app['former']->getFrameworkInstance('Nude');
408
-			});
409
-		}
410
-
411
-		// Use default form type if the one provided is invalid
412
-		if ($type !== 'raw' and !in_array($type, $this->app['former.form.framework']->availableTypes())) {
413
-			$type = $this->app['former']->getOption('default_form_type');
414
-		}
415
-
416
-		return $type;
417
-	}
15
+    /**
16
+     * The IoC Container
17
+     *
18
+     * @var Container
19
+     */
20
+    protected $app;
21
+
22
+    /**
23
+     * The URL generator
24
+     *
25
+     * @var UrlGenerator
26
+     */
27
+    protected $url;
28
+
29
+    /**
30
+     * The Populator
31
+     *
32
+     * @var Populator
33
+     */
34
+    protected $populator;
35
+
36
+    /**
37
+     * The Form type
38
+     *
39
+     * @var string
40
+     */
41
+    protected $type = null;
42
+
43
+    /**
44
+     * The destination of the current form
45
+     *
46
+     * @var string
47
+     */
48
+    protected $action;
49
+
50
+    /**
51
+     * The form method
52
+     *
53
+     * @var string
54
+     */
55
+    protected $method;
56
+
57
+    /**
58
+     * Whether the form should be secured or not
59
+     *
60
+     * @var boolean
61
+     */
62
+    protected $secure;
63
+
64
+    /**
65
+     * The form element
66
+     *
67
+     * @var string
68
+     */
69
+    protected $element = 'form';
70
+
71
+    /**
72
+     * A list of injected properties
73
+     *
74
+     * @var array
75
+     */
76
+    protected $injectedProperties = array('method', 'action');
77
+
78
+    /**
79
+     * Whether a form is opened or not
80
+     *
81
+     * @var boolean
82
+     */
83
+    protected static $opened = false;
84
+
85
+    ////////////////////////////////////////////////////////////////////
86
+    /////////////////////////// CORE METHODS ///////////////////////////
87
+    ////////////////////////////////////////////////////////////////////
88
+
89
+    /**
90
+     * Build a new Form instance
91
+     *
92
+     * @param UrlGenerator $url
93
+     */
94
+    public function __construct(Container $app, $url, Populator $populator)
95
+    {
96
+        $this->app       = $app;
97
+        $this->url       = $url;
98
+        $this->populator = $populator;
99
+
100
+        $this->app->singleton('former.form.framework', function ($app) {
101
+            return clone $app['former.framework'];
102
+        });
103
+    }
104
+
105
+    /**
106
+     * Opens up magically a form
107
+     *
108
+     * @param  string $type       The form type asked
109
+     * @param  array  $parameters Parameters passed
110
+     *
111
+     * @return Form             A form opening tag
112
+     */
113
+    public function openForm($type, $parameters)
114
+    {
115
+        $action     = Arr::get($parameters, 0);
116
+        $method     = Arr::get($parameters, 1, 'POST');
117
+        $attributes = Arr::get($parameters, 2, array());
118
+        $secure     = Arr::get($parameters, 3, null);
119
+
120
+        // Fetch errors if asked for
121
+        if ($this->app['former']->getOption('fetch_errors')) {
122
+            $this->app['former']->withErrors();
123
+        }
124
+
125
+        // Open the form
126
+        $this->action($action);
127
+        $this->attributes = $attributes;
128
+        $this->method     = strtoupper($method);
129
+        $this->secure     = $secure;
130
+
131
+        // Add any effect of the form type
132
+        $type       = Str::snake($type);
133
+        $this->type = $this->applyType($type);
134
+
135
+        // Add enctype
136
+        if (!array_key_exists('accept-charset', $attributes)) {
137
+            $this->attributes['accept-charset'] = 'utf-8';
138
+        }
139
+
140
+        // Add supplementary classes
141
+        if ($this->type !== 'raw') {
142
+            $this->addClass($this->app['former.form.framework']->getFormClasses($this->type));
143
+        }
144
+
145
+        return $this;
146
+    }
147
+
148
+    /**
149
+     * Closes a Form
150
+     *
151
+     * @return string A closing <form> tag
152
+     */
153
+    public function close()
154
+    {
155
+        static::$opened = false;
156
+
157
+        // Add token if necessary
158
+        $closing = '</form>';
159
+        if ($this->method != 'GET') {
160
+            $closing = $this->app['former']->token().$closing;
161
+        }
162
+
163
+        return $closing;
164
+    }
165
+
166
+    ////////////////////////////////////////////////////////////////////
167
+    //////////////////////////// STATIC HELPERS ////////////////////////
168
+    ////////////////////////////////////////////////////////////////////
169
+
170
+    /**
171
+     * Whether a form is currently opened or not
172
+     *
173
+     * @return boolean
174
+     */
175
+    public static function hasInstanceOpened()
176
+    {
177
+        return static::$opened;
178
+    }
179
+
180
+    ////////////////////////////////////////////////////////////////////
181
+    /////////////////////////////// SETTER /////////////////////////////
182
+    ////////////////////////////////////////////////////////////////////
183
+
184
+    /**
185
+     * Change the form's action
186
+     *
187
+     * @param  string $action The new action
188
+     *
189
+     * @return $this
190
+     */
191
+    public function action($action)
192
+    {
193
+        $this->action = $action ? $this->url->to($action, array(), $this->secure) : null;
194
+
195
+        return $this;
196
+    }
197
+
198
+    /**
199
+     * Change the form's method
200
+     *
201
+     * @param  string $method The method to use
202
+     *
203
+     * @return $this
204
+     */
205
+    public function method($method)
206
+    {
207
+        $this->method = strtoupper($method);
208
+
209
+        return $this;
210
+    }
211
+
212
+    /**
213
+     * Whether the form should be secure
214
+     *
215
+     * @param  boolean $secure Secure or not
216
+     *
217
+     * @return $this
218
+     */
219
+    public function secure($secure = true)
220
+    {
221
+        $this->secure = $secure;
222
+
223
+        return $this;
224
+    }
225
+
226
+    /**
227
+     * Change the form's action and method to a route
228
+     *
229
+     * @param  string $name   The name of the route to use
230
+     * @param  array  $params Any route parameters
231
+     *
232
+     * @return Form
233
+     */
234
+    public function route($name, $params = array())
235
+    {
236
+        return $this->setRouteOrAction($name, $params, 'route');
237
+    }
238
+
239
+    /**
240
+     * Change the form's action to a controller method
241
+     *
242
+     * @param  string $name   The controller and method
243
+     * @param  array  $params Any method parameters
244
+     *
245
+     * @return Form
246
+     */
247
+    public function controller($name, $params = array())
248
+    {
249
+        return $this->setRouteOrAction($name, $params, 'action');
250
+    }
251
+
252
+    /**
253
+     * Outputs the current form opened
254
+     *
255
+     * @return string A <form> opening tag
256
+     */
257
+    public function __toString()
258
+    {
259
+        // Mark the form as opened
260
+        static::$opened = true;
261
+
262
+        // Add name to attributes
263
+        $this->attributes['name'] = $this->name;
264
+
265
+        // Add spoof method
266
+        if (in_array($this->method, array('PUT', 'PATCH', 'DELETE'))) {
267
+            $spoof        = $this->app['former']->hidden('_method', $this->method);
268
+            $this->method = 'POST';
269
+        } else {
270
+            $spoof = null;
271
+        }
272
+
273
+        return $this->open().$spoof;
274
+    }
275
+
276
+    ////////////////////////////////////////////////////////////////////
277
+    ////////////////////////// PUBLIC HELPERS //////////////////////////
278
+    ////////////////////////////////////////////////////////////////////
279
+
280
+    /**
281
+     * Alias for $this->app['former']->withRules
282
+     */
283
+    public function rules()
284
+    {
285
+        call_user_func_array(array($this->app['former'], 'withRules'), func_get_args());
286
+
287
+        return $this;
288
+    }
289
+
290
+    /**
291
+     * Populate a form with specific values
292
+     *
293
+     * @param array|object $values
294
+     *
295
+     * @return $this
296
+     */
297
+    public function populate($values)
298
+    {
299
+        $this->populator->replace($values);
300
+
301
+        return $this;
302
+    }
303
+
304
+    /**
305
+     * Get the Populator binded to the Form
306
+     *
307
+     * @return Populator
308
+     */
309
+    public function getPopulator()
310
+    {
311
+        return $this->populator;
312
+    }
313
+
314
+    ////////////////////////////////////////////////////////////////////
315
+    ////////////////////////////// HELPERS /////////////////////////////
316
+    ////////////////////////////////////////////////////////////////////
317
+
318
+    /**
319
+     * Find the method of a route by its _uses or name
320
+     *
321
+     * @param  string $name
322
+     *
323
+     * @return string
324
+     */
325
+    protected function findRouteMethod($name)
326
+    {
327
+        if (!$this->app->bound('router')) {
328
+            return;
329
+        }
330
+
331
+        // Get string by name
332
+        if (!Str::contains($name, '@')) {
333
+            $routes = $this->app['router']->getRoutes();
334
+            $route  = method_exists($routes, 'getByName') ? $routes->getByName($name) : $routes->get($name);
335
+            // Get string by uses
336
+        } else {
337
+            foreach ($this->app['router']->getRoutes() as $route) {
338
+                $routeUses = method_exists($route, 'getOption') ? $route->getOption('_uses') : Arr::get($route->getAction(), 'controller');
339
+                if ($action = $routeUses) {
340
+                    if ($action == $name) {
341
+                        break;
342
+                    }
343
+                }
344
+            }
345
+        }
346
+
347
+        // Get method
348
+        $methods = method_exists($route, 'getMethods') ? $route->getMethods() : $route->methods();
349
+        $method  = Arr::get($methods, 0);
350
+
351
+        return $method;
352
+    }
353
+
354
+    /**
355
+     * @param $name
356
+     * @param $params
357
+     * @param $type
358
+     *
359
+     * @return $this
360
+     */
361
+    protected function setRouteOrAction($name, $params, $type)
362
+    {
363
+        // Set the form action
364
+        $this->action = $this->url->$type($name, $params);
365
+
366
+        // Set the proper method
367
+        if ($method = $this->findRouteMethod($name)) {
368
+            $this->method($method);
369
+        }
370
+
371
+        return $this;
372
+    }
373
+
374
+    /**
375
+     * Apply various parameters according to form type
376
+     *
377
+     * @param  string $type The original form type provided
378
+     *
379
+     * @return string The final form type
380
+     */
381
+    private function applyType($type)
382
+    {
383
+        // If classic form
384
+        if ($type == 'open') {
385
+            return $this->app['former']->getOption('default_form_type');
386
+        }
387
+
388
+        // Look for HTTPS form
389
+        if (Str::contains($type, 'secure')) {
390
+            $type         = str_replace('secure', '', $type);
391
+            $this->secure = true;
392
+        }
393
+
394
+        // Look for file form
395
+        if (Str::contains($type, 'for_files')) {
396
+            $type                        = str_replace('for_files', '', $type);
397
+            $this->attributes['enctype'] = 'multipart/form-data';
398
+        }
399
+
400
+        // Calculate form type
401
+        $type = str_replace('open', '', $type);
402
+        $type = trim($type, '_');
403
+
404
+        // If raw form
405
+        if ($type == 'raw') {
406
+            $this->app->bind('former.form.framework', function ($app) {
407
+                return $app['former']->getFrameworkInstance('Nude');
408
+            });
409
+        }
410
+
411
+        // Use default form type if the one provided is invalid
412
+        if ($type !== 'raw' and !in_array($type, $this->app['former.form.framework']->availableTypes())) {
413
+            $type = $this->app['former']->getOption('default_form_type');
414
+        }
415
+
416
+        return $type;
417
+    }
418 418
 }
Please login to merge, or discard this patch.
src/Former/Traits/Framework.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -239,7 +239,7 @@
 block discarded – undo
239 239
 	 */
240 240
 	protected function prependWith($classes, $with)
241 241
 	{
242
-		return array_map(function ($class) use ($with) {
242
+		return array_map(function($class) use ($with) {
243 243
 			return $with.$class;
244 244
 		}, $classes);
245 245
 	}
Please login to merge, or discard this patch.
Indentation   +318 added lines, -318 removed lines patch added patch discarded remove patch
@@ -10,322 +10,322 @@
 block discarded – undo
10 10
  */
11 11
 abstract class Framework implements FrameworkInterface
12 12
 {
13
-	/**
14
-	 * The Container
15
-	 *
16
-	 * @var Container
17
-	 */
18
-	protected $app;
19
-
20
-	/**
21
-	 * Form types that trigger special styling
22
-	 *
23
-	 * @var array
24
-	 */
25
-	protected $availableTypes = array();
26
-
27
-	/**
28
-	 * The field states available
29
-	 *
30
-	 * @var array
31
-	 */
32
-	protected $states = array();
33
-
34
-	/**
35
-	 * The default label width (for horizontal forms)
36
-	 *
37
-	 * @var string
38
-	 */
39
-	protected $labelWidth;
40
-
41
-	/**
42
-	 * The default field width (for horizontal forms)
43
-	 *
44
-	 * @var string
45
-	 */
46
-	protected $fieldWidth;
47
-
48
-	/**
49
-	 * The default offset for fields (for horizontal form fields
50
-	 * with no label, so usually equal to the default label width)
51
-	 *
52
-	 * @var string
53
-	 */
54
-	protected $fieldOffset;
55
-
56
-	/**
57
-	 * The default HTML tag used for icons
58
-	 *
59
-	 * @var string
60
-	 */
61
-	protected $iconTag;
62
-
63
-	/**
64
-	 * The default set for icon fonts
65
-	 *
66
-	 * @var string
67
-	 */
68
-	protected $iconSet;
69
-
70
-	/**
71
-	 * The default prefix icon names
72
-	 *
73
-	 * @var string
74
-	 */
75
-	protected $iconPrefix;
76
-
77
-	////////////////////////////////////////////////////////////////////
78
-	//////////////////////// CURRENT FRAMEWORK /////////////////////////
79
-	////////////////////////////////////////////////////////////////////
80
-
81
-	/**
82
-	 * Get the name of the current framework
83
-	 *
84
-	 * @return string
85
-	 */
86
-	public function current()
87
-	{
88
-		return basename(str_replace('\\', '/', get_class($this)));
89
-	}
90
-
91
-	/**
92
-	 * Check if the current framework matches something
93
-	 *
94
-	 * @param  string $framework
95
-	 *
96
-	 * @return boolean
97
-	 */
98
-	public function is($framework)
99
-	{
100
-		return $framework == $this->current();
101
-	}
102
-
103
-	/**
104
-	 * Check if the current framework doesn't match something
105
-	 *
106
-	 * @param  string $framework
107
-	 *
108
-	 * @return boolean
109
-	 */
110
-	public function isnt($framework)
111
-	{
112
-		return $framework != $this->current();
113
-	}
114
-
115
-	////////////////////////////////////////////////////////////////////
116
-	/////////////////////////// COMMON METHODS /////////////////////////
117
-	////////////////////////////////////////////////////////////////////
118
-
119
-	/**
120
-	 * List form types triggered special styling form current framework
121
-	 *
122
-	 * @return array
123
-	 */
124
-	public function availableTypes()
125
-	{
126
-		return $this->availableTypes;
127
-	}
128
-
129
-	/**
130
-	 * Filter a field state
131
-	 *
132
-	 * @param string $state
133
-	 *
134
-	 * @return string
135
-	 */
136
-	public function filterState($state)
137
-	{
138
-		// Filter out wrong states
139
-		return in_array($state, $this->states) ? $state : null;
140
-	}
141
-
142
-	/**
143
-	 * Framework error state
144
-	 *
145
-	 * @return string
146
-	 */
147
-	public function errorState()
148
-	{
149
-		return 'error';
150
-	}
151
-
152
-	/**
153
-	 * Returns corresponding inline class of a field
154
-	 *
155
-	 * @param Field $field
156
-	 *
157
-	 * @return string
158
-	 */
159
-	public function getInlineLabelClass($field)
160
-	{
161
-		return 'inline';
162
-	}
163
-
164
-	/**
165
-	 * Set framework defaults from its config file
166
-	 */
167
-	protected function setFrameworkDefaults()
168
-	{
169
-		$this->setFieldWidths($this->getFrameworkOption('labelWidths'));
170
-		$this->setIconDefaults();
171
-	}
172
-
173
-	/**
174
-	 * @param string $widths
175
-	 */
176
-	protected function setFieldWidths($widths)
177
-	{
178
-	}
179
-
180
-	/**
181
-	 * Override framework defaults for icons with config values where set
182
-	 */
183
-	protected function setIconDefaults()
184
-	{
185
-		$this->iconTag    = $this->getFrameworkOption('icon.tag');
186
-		$this->iconSet    = $this->getFrameworkOption('icon.set');
187
-		$this->iconPrefix = $this->getFrameworkOption('icon.prefix');
188
-	}
189
-
190
-	/**
191
-	 * Render an icon
192
-	 *
193
-	 * @param array $attributes Its general attributes
194
-	 *
195
-	 * @return string
196
-	 */
197
-	public function createIcon($iconType, $attributes = array(), $iconSettings = array())
198
-	{
199
-		// Check for empty icons
200
-		if (!$iconType) {
201
-			return false;
202
-		}
203
-
204
-		// icon settings can be overridden for a specific icon
205
-		$tag    = Arr::get($iconSettings, 'tag', $this->iconTag);
206
-		$set    = Arr::get($iconSettings, 'set', $this->iconSet);
207
-		$prefix = Arr::get($iconSettings, 'prefix', $this->iconPrefix);
208
-
209
-		return Element::create($tag, null, $attributes)->addClass("$set $prefix-$iconType");
210
-	}
211
-
212
-	////////////////////////////////////////////////////////////////////
213
-	///////////////////////////// HELPERS //////////////////////////////
214
-	////////////////////////////////////////////////////////////////////
215
-
216
-	/**
217
-	 * Add classes to a field
218
-	 *
219
-	 * @param Field $field
220
-	 * @param array $classes
221
-	 *
222
-	 * @return \Former\Traits\Field
223
-	 */
224
-	protected function addClassesToField($field, $classes)
225
-	{
226
-		// If we found any class, add them
227
-		if ($classes) {
228
-			$field->addClass(implode(' ', $classes));
229
-		}
230
-
231
-		return $field;
232
-	}
233
-
234
-	/**
235
-	 * Prepend an array of classes with a string
236
-	 *
237
-	 * @param array  $classes The classes to prepend
238
-	 * @param string $with    The string to prepend them with
239
-	 *
240
-	 * @return array A prepended array
241
-	 */
242
-	protected function prependWith($classes, $with)
243
-	{
244
-		return array_map(function ($class) use ($with) {
245
-			return $with.$class;
246
-		}, $classes);
247
-	}
248
-
249
-	/**
250
-	 * Create a label for a field
251
-	 *
252
-	 * @param Field   $field
253
-	 * @param Element $label The field label if non provided
254
-	 *
255
-	 * @return string A label
256
-	 */
257
-	public function createLabelOf(Field $field, Element $label = null)
258
-	{
259
-		// Get the label and its informations
260
-		if (!$label) {
261
-			$label = $field->getLabel();
262
-		}
263
-
264
-		// Get label "for"
265
-		$for = $field->id ?: $field->getName();
266
-
267
-		// Get label text
268
-		$text = $label->getValue();
269
-		if (!$text) {
270
-			return false;
271
-		}
272
-
273
-		// Append required text
274
-		if ($field->isRequired()) {
275
-			$text .= $this->app['former']->getOption('required_text');
276
-		}
277
-
278
-		// Render plain label if checkable, else a classic one
279
-		$label->setValue($text);
280
-		if (!$field->isCheckable()) {
281
-			$label->for($for);
282
-		}
283
-
284
-		return $label;
285
-	}
286
-
287
-	/**
288
-	 * Get an option for the current framework
289
-	 *
290
-	 * @param string $option
291
-	 *
292
-	 * @return string
293
-	 */
294
-	protected function getFrameworkOption($option)
295
-	{
296
-		return $this->app['config']->get("former.{$this->current()}.$option");
297
-	}
298
-
299
-	////////////////////////////////////////////////////////////////////
300
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
301
-	////////////////////////////////////////////////////////////////////
302
-
303
-	/**
304
-	 * Wraps all label contents with potential additional tags.
305
-	 *
306
-	 * @param  string $label
307
-	 *
308
-	 * @return string A wrapped label
309
-	 */
310
-	public function wrapLabel($label)
311
-	{
312
-		return $label;
313
-	}
314
-
315
-	////////////////////////////////////////////////////////////////////
316
-	//////////////////////////// RENDER BLOCKS /////////////////////////
317
-	////////////////////////////////////////////////////////////////////
318
-
319
-	/**
320
-	 * Render an validation error text
321
-	 *
322
-	 * @param string $text
323
-	 * @param array  $attributes
324
-	 *
325
-	 * @return string
326
-	 */
327
-	public function createValidationError($text, $attributes = array())
328
-	{
329
-		return $this->createHelp($text, $attributes);
330
-	}
13
+    /**
14
+     * The Container
15
+     *
16
+     * @var Container
17
+     */
18
+    protected $app;
19
+
20
+    /**
21
+     * Form types that trigger special styling
22
+     *
23
+     * @var array
24
+     */
25
+    protected $availableTypes = array();
26
+
27
+    /**
28
+     * The field states available
29
+     *
30
+     * @var array
31
+     */
32
+    protected $states = array();
33
+
34
+    /**
35
+     * The default label width (for horizontal forms)
36
+     *
37
+     * @var string
38
+     */
39
+    protected $labelWidth;
40
+
41
+    /**
42
+     * The default field width (for horizontal forms)
43
+     *
44
+     * @var string
45
+     */
46
+    protected $fieldWidth;
47
+
48
+    /**
49
+     * The default offset for fields (for horizontal form fields
50
+     * with no label, so usually equal to the default label width)
51
+     *
52
+     * @var string
53
+     */
54
+    protected $fieldOffset;
55
+
56
+    /**
57
+     * The default HTML tag used for icons
58
+     *
59
+     * @var string
60
+     */
61
+    protected $iconTag;
62
+
63
+    /**
64
+     * The default set for icon fonts
65
+     *
66
+     * @var string
67
+     */
68
+    protected $iconSet;
69
+
70
+    /**
71
+     * The default prefix icon names
72
+     *
73
+     * @var string
74
+     */
75
+    protected $iconPrefix;
76
+
77
+    ////////////////////////////////////////////////////////////////////
78
+    //////////////////////// CURRENT FRAMEWORK /////////////////////////
79
+    ////////////////////////////////////////////////////////////////////
80
+
81
+    /**
82
+     * Get the name of the current framework
83
+     *
84
+     * @return string
85
+     */
86
+    public function current()
87
+    {
88
+        return basename(str_replace('\\', '/', get_class($this)));
89
+    }
90
+
91
+    /**
92
+     * Check if the current framework matches something
93
+     *
94
+     * @param  string $framework
95
+     *
96
+     * @return boolean
97
+     */
98
+    public function is($framework)
99
+    {
100
+        return $framework == $this->current();
101
+    }
102
+
103
+    /**
104
+     * Check if the current framework doesn't match something
105
+     *
106
+     * @param  string $framework
107
+     *
108
+     * @return boolean
109
+     */
110
+    public function isnt($framework)
111
+    {
112
+        return $framework != $this->current();
113
+    }
114
+
115
+    ////////////////////////////////////////////////////////////////////
116
+    /////////////////////////// COMMON METHODS /////////////////////////
117
+    ////////////////////////////////////////////////////////////////////
118
+
119
+    /**
120
+     * List form types triggered special styling form current framework
121
+     *
122
+     * @return array
123
+     */
124
+    public function availableTypes()
125
+    {
126
+        return $this->availableTypes;
127
+    }
128
+
129
+    /**
130
+     * Filter a field state
131
+     *
132
+     * @param string $state
133
+     *
134
+     * @return string
135
+     */
136
+    public function filterState($state)
137
+    {
138
+        // Filter out wrong states
139
+        return in_array($state, $this->states) ? $state : null;
140
+    }
141
+
142
+    /**
143
+     * Framework error state
144
+     *
145
+     * @return string
146
+     */
147
+    public function errorState()
148
+    {
149
+        return 'error';
150
+    }
151
+
152
+    /**
153
+     * Returns corresponding inline class of a field
154
+     *
155
+     * @param Field $field
156
+     *
157
+     * @return string
158
+     */
159
+    public function getInlineLabelClass($field)
160
+    {
161
+        return 'inline';
162
+    }
163
+
164
+    /**
165
+     * Set framework defaults from its config file
166
+     */
167
+    protected function setFrameworkDefaults()
168
+    {
169
+        $this->setFieldWidths($this->getFrameworkOption('labelWidths'));
170
+        $this->setIconDefaults();
171
+    }
172
+
173
+    /**
174
+     * @param string $widths
175
+     */
176
+    protected function setFieldWidths($widths)
177
+    {
178
+    }
179
+
180
+    /**
181
+     * Override framework defaults for icons with config values where set
182
+     */
183
+    protected function setIconDefaults()
184
+    {
185
+        $this->iconTag    = $this->getFrameworkOption('icon.tag');
186
+        $this->iconSet    = $this->getFrameworkOption('icon.set');
187
+        $this->iconPrefix = $this->getFrameworkOption('icon.prefix');
188
+    }
189
+
190
+    /**
191
+     * Render an icon
192
+     *
193
+     * @param array $attributes Its general attributes
194
+     *
195
+     * @return string
196
+     */
197
+    public function createIcon($iconType, $attributes = array(), $iconSettings = array())
198
+    {
199
+        // Check for empty icons
200
+        if (!$iconType) {
201
+            return false;
202
+        }
203
+
204
+        // icon settings can be overridden for a specific icon
205
+        $tag    = Arr::get($iconSettings, 'tag', $this->iconTag);
206
+        $set    = Arr::get($iconSettings, 'set', $this->iconSet);
207
+        $prefix = Arr::get($iconSettings, 'prefix', $this->iconPrefix);
208
+
209
+        return Element::create($tag, null, $attributes)->addClass("$set $prefix-$iconType");
210
+    }
211
+
212
+    ////////////////////////////////////////////////////////////////////
213
+    ///////////////////////////// HELPERS //////////////////////////////
214
+    ////////////////////////////////////////////////////////////////////
215
+
216
+    /**
217
+     * Add classes to a field
218
+     *
219
+     * @param Field $field
220
+     * @param array $classes
221
+     *
222
+     * @return \Former\Traits\Field
223
+     */
224
+    protected function addClassesToField($field, $classes)
225
+    {
226
+        // If we found any class, add them
227
+        if ($classes) {
228
+            $field->addClass(implode(' ', $classes));
229
+        }
230
+
231
+        return $field;
232
+    }
233
+
234
+    /**
235
+     * Prepend an array of classes with a string
236
+     *
237
+     * @param array  $classes The classes to prepend
238
+     * @param string $with    The string to prepend them with
239
+     *
240
+     * @return array A prepended array
241
+     */
242
+    protected function prependWith($classes, $with)
243
+    {
244
+        return array_map(function ($class) use ($with) {
245
+            return $with.$class;
246
+        }, $classes);
247
+    }
248
+
249
+    /**
250
+     * Create a label for a field
251
+     *
252
+     * @param Field   $field
253
+     * @param Element $label The field label if non provided
254
+     *
255
+     * @return string A label
256
+     */
257
+    public function createLabelOf(Field $field, Element $label = null)
258
+    {
259
+        // Get the label and its informations
260
+        if (!$label) {
261
+            $label = $field->getLabel();
262
+        }
263
+
264
+        // Get label "for"
265
+        $for = $field->id ?: $field->getName();
266
+
267
+        // Get label text
268
+        $text = $label->getValue();
269
+        if (!$text) {
270
+            return false;
271
+        }
272
+
273
+        // Append required text
274
+        if ($field->isRequired()) {
275
+            $text .= $this->app['former']->getOption('required_text');
276
+        }
277
+
278
+        // Render plain label if checkable, else a classic one
279
+        $label->setValue($text);
280
+        if (!$field->isCheckable()) {
281
+            $label->for($for);
282
+        }
283
+
284
+        return $label;
285
+    }
286
+
287
+    /**
288
+     * Get an option for the current framework
289
+     *
290
+     * @param string $option
291
+     *
292
+     * @return string
293
+     */
294
+    protected function getFrameworkOption($option)
295
+    {
296
+        return $this->app['config']->get("former.{$this->current()}.$option");
297
+    }
298
+
299
+    ////////////////////////////////////////////////////////////////////
300
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
301
+    ////////////////////////////////////////////////////////////////////
302
+
303
+    /**
304
+     * Wraps all label contents with potential additional tags.
305
+     *
306
+     * @param  string $label
307
+     *
308
+     * @return string A wrapped label
309
+     */
310
+    public function wrapLabel($label)
311
+    {
312
+        return $label;
313
+    }
314
+
315
+    ////////////////////////////////////////////////////////////////////
316
+    //////////////////////////// RENDER BLOCKS /////////////////////////
317
+    ////////////////////////////////////////////////////////////////////
318
+
319
+    /**
320
+     * Render an validation error text
321
+     *
322
+     * @param string $text
323
+     * @param array  $attributes
324
+     *
325
+     * @return string
326
+     */
327
+    public function createValidationError($text, $attributes = array())
328
+    {
329
+        return $this->createHelp($text, $attributes);
330
+    }
331 331
 }
Please login to merge, or discard this patch.
src/Former/FormerServiceProvider.php 2 patches
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -90,15 +90,15 @@  discard block
 block discarded – undo
90 90
 		// Session and request
91 91
 		//////////////////////////////////////////////////////////////////
92 92
 
93
-		$app->bindIf('session.manager', function ($app) {
93
+		$app->bindIf('session.manager', function($app) {
94 94
 			return new SessionManager($app);
95 95
 		});
96 96
 
97
-		$app->bindIf('session', function ($app) {
97
+		$app->bindIf('session', function($app) {
98 98
 			return $app['session.manager']->driver('array');
99 99
 		}, true);
100 100
 
101
-		$app->bindIf('request', function ($app) {
101
+		$app->bindIf('request', function($app) {
102 102
 			$request = Request::createFromGlobals();
103 103
 			if (method_exists($request, 'setSessionStore')) {
104 104
 				$request->setSessionStore($app['session']);
@@ -112,11 +112,11 @@  discard block
 block discarded – undo
112 112
 		// Config
113 113
 		//////////////////////////////////////////////////////////////////
114 114
 
115
-		$app->bindIf('path.config', function ($app) {
116
-			return __DIR__ . '/../config/';
115
+		$app->bindIf('path.config', function($app) {
116
+			return __DIR__.'/../config/';
117 117
 		}, true);
118 118
 
119
-		$app->bindIf('config', function ($app) {
119
+		$app->bindIf('config', function($app) {
120 120
 			$config = new Repository;
121 121
 			$this->loadConfigurationFiles($app, $config);
122 122
 			return $config;
@@ -125,11 +125,11 @@  discard block
 block discarded – undo
125 125
 		// Localization
126 126
 		//////////////////////////////////////////////////////////////////
127 127
 
128
-		$app->bindIf('translation.loader', function ($app) {
128
+		$app->bindIf('translation.loader', function($app) {
129 129
 			return new FileLoader($app['files'], 'src/config');
130 130
 		});
131 131
 
132
-		$app->bindIf('translator', function ($app) {
132
+		$app->bindIf('translator', function($app) {
133 133
 			$loader = new FileLoader($app['files'], 'lang');
134 134
 
135 135
 			return new Translator($loader, 'fr');
@@ -181,25 +181,25 @@  discard block
 block discarded – undo
181 181
 	public function bindFormer(Container $app)
182 182
 	{
183 183
 		// Add config namespace
184
-		$configPath = __DIR__ . '/../config/former.php';
184
+		$configPath = __DIR__.'/../config/former.php';
185 185
 		$this->mergeConfigFrom($configPath, 'former');
186
-		$this->publishes([$configPath => $app['path.config'] . '/former.php']);
186
+		$this->publishes([$configPath => $app['path.config'].'/former.php']);
187 187
 		
188 188
 		$framework = $app['config']->get('former.framework');
189 189
 		
190
-		$app->bind('former.framework', function ($app) {
190
+		$app->bind('former.framework', function($app) {
191 191
 			return $app['former']->getFrameworkInstance($app['config']->get('former.framework'));
192 192
 		});
193 193
 
194
-		$app->singleton('former.populator', function ($app) {
194
+		$app->singleton('former.populator', function($app) {
195 195
 			return new Populator();
196 196
 		});
197 197
 
198
-		$app->singleton('former.dispatcher', function ($app) {
198
+		$app->singleton('former.dispatcher', function($app) {
199 199
 			return new MethodDispatcher($app, Former::FIELDSPACE);
200 200
 		});
201 201
 
202
-		$app->singleton('former', function ($app) {
202
+		$app->singleton('former', function($app) {
203 203
 			return new Former($app, $app->make('former.dispatcher'));
204 204
 		});
205 205
 		$app->alias('former', 'Former\Former');
Please login to merge, or discard this patch.
Indentation   +188 added lines, -188 removed lines patch added patch discarded remove patch
@@ -15,199 +15,199 @@
 block discarded – undo
15 15
  */
16 16
 class FormerServiceProvider extends ServiceProvider
17 17
 {
18
-	/**
19
-	 * Indicates if loading of the provider is deferred.
20
-	 *
21
-	 * @var bool
22
-	 */
23
-	protected $defer = true;
24
-
25
-	/**
26
-	 * Register Former's package with Laravel
27
-	 *
28
-	 * @return void
29
-	 */
30
-	public function register()
31
-	{
32
-		$this->app = static::make($this->app);
33
-	}
34
-
35
-	/**
36
-	 * Get the services provided by the provider.
37
-	 *
38
-	 * @return string[]
39
-	 */
40
-	public function provides()
41
-	{
42
-		return array('former', 'Former\Former');
43
-	}
44
-
45
-	////////////////////////////////////////////////////////////////////
46
-	/////////////////////////// CLASS BINDINGS /////////////////////////
47
-	////////////////////////////////////////////////////////////////////
48
-
49
-	/**
50
-	 * Create a Former container
51
-	 *
52
-	 * @param  Container $app
53
-	 *
54
-	 * @return Container
55
-	 */
56
-	public static function make($app = null)
57
-	{
58
-		if (!$app) {
59
-			$app = new Container();
60
-		}
61
-
62
-		// Bind classes to container
63
-		$provider = new static($app);
64
-		$app      = $provider->bindCoreClasses($app);
65
-		$app      = $provider->bindFormer($app);
66
-
67
-		return $app;
68
-	}
69
-
70
-	/**
71
-	 * Bind the core classes to the Container
72
-	 *
73
-	 * @param  Container $app
74
-	 *
75
-	 * @return Container
76
-	 */
77
-	public function bindCoreClasses(Container $app)
78
-	{
79
-		// Cancel if in the scope of a Laravel application
80
-		if ($app->bound('events')) {
81
-			return $app;
82
-		}
83
-
84
-		// Core classes
85
-		//////////////////////////////////////////////////////////////////
86
-
87
-		$app->bindIf('files', 'Illuminate\Filesystem\Filesystem');
88
-		$app->bindIf('url', 'Illuminate\Routing\UrlGenerator');
89
-
90
-		// Session and request
91
-		//////////////////////////////////////////////////////////////////
92
-
93
-		$app->bindIf('session.manager', function ($app) {
94
-			return new SessionManager($app);
95
-		});
96
-
97
-		$app->bindIf('session', function ($app) {
98
-			return $app['session.manager']->driver('array');
99
-		}, true);
100
-
101
-		$app->bindIf('request', function ($app) {
102
-			$request = Request::createFromGlobals();
103
-			if (method_exists($request, 'setSessionStore')) {
104
-				$request->setSessionStore($app['session']);
105
-			} else if (method_exists($request, 'setLaravelSession')) {
106
-				$request->setLaravelSession($app['session']);
107
-			} else {
108
-				$request->setSession($app['session']);
109
-			}
110
-
111
-			return $request;
112
-		}, true);
113
-
114
-		// Config
115
-		//////////////////////////////////////////////////////////////////
116
-
117
-		$app->bindIf('path.config', function ($app) {
118
-			return __DIR__ . '/../config/';
119
-		}, true);
120
-
121
-		$app->bindIf('config', function ($app) {
122
-			$config = new Repository;
123
-			$this->loadConfigurationFiles($app, $config);
124
-			return $config;
125
-		}, true);
126
-
127
-		// Localization
128
-		//////////////////////////////////////////////////////////////////
129
-
130
-		$app->bindIf('translation.loader', function ($app) {
131
-			return new FileLoader($app['files'], 'src/config');
132
-		});
133
-
134
-		$app->bindIf('translator', function ($app) {
135
-			$loader = new FileLoader($app['files'], 'lang');
136
-
137
-			return new Translator($loader, 'fr');
138
-		});
139
-
140
-		return $app;
141
-	}
142
-
143
-	/**
144
-	 * Load the configuration items from all of the files.
145
-	 *
146
-	 * @param  Container $app
147
-	 * @param  Repository  $config
148
-	 * @return void
149
-	 */
150
-	protected function loadConfigurationFiles($app, Repository $config)
151
-	{
152
-		foreach ($this->getConfigurationFiles($app) as $key => $path)
153
-		{
154
-			$config->set($key, require $path);
155
-		}
156
-	}
157
-
158
-	/**
159
-	 * Get all of the configuration files for the application.
160
-	 *
161
-	 * @param  $app
162
-	 * @return array
163
-	 */
164
-	protected function getConfigurationFiles($app)
165
-	{
166
-		$files = array();
167
-
168
-		foreach (Finder::create()->files()->name('*.php')->in($app['path.config']) as $file)
169
-		{
170
-			$files[basename($file->getRealPath(), '.php')] = $file->getRealPath();
171
-		}
172
-
173
-		return $files;
174
-	}
175
-
176
-	/**
177
-	 * Bind Former classes to the container
178
-	 *
179
-	 * @param  Container $app
180
-	 *
181
-	 * @return Container
182
-	 */
183
-	public function bindFormer(Container $app)
184
-	{
185
-		// Add config namespace
186
-		$configPath = __DIR__ . '/../config/former.php';
187
-		$this->mergeConfigFrom($configPath, 'former');
188
-		$this->publishes([$configPath => $app['path.config'] . '/former.php']);
18
+    /**
19
+     * Indicates if loading of the provider is deferred.
20
+     *
21
+     * @var bool
22
+     */
23
+    protected $defer = true;
24
+
25
+    /**
26
+     * Register Former's package with Laravel
27
+     *
28
+     * @return void
29
+     */
30
+    public function register()
31
+    {
32
+        $this->app = static::make($this->app);
33
+    }
34
+
35
+    /**
36
+     * Get the services provided by the provider.
37
+     *
38
+     * @return string[]
39
+     */
40
+    public function provides()
41
+    {
42
+        return array('former', 'Former\Former');
43
+    }
44
+
45
+    ////////////////////////////////////////////////////////////////////
46
+    /////////////////////////// CLASS BINDINGS /////////////////////////
47
+    ////////////////////////////////////////////////////////////////////
48
+
49
+    /**
50
+     * Create a Former container
51
+     *
52
+     * @param  Container $app
53
+     *
54
+     * @return Container
55
+     */
56
+    public static function make($app = null)
57
+    {
58
+        if (!$app) {
59
+            $app = new Container();
60
+        }
61
+
62
+        // Bind classes to container
63
+        $provider = new static($app);
64
+        $app      = $provider->bindCoreClasses($app);
65
+        $app      = $provider->bindFormer($app);
66
+
67
+        return $app;
68
+    }
69
+
70
+    /**
71
+     * Bind the core classes to the Container
72
+     *
73
+     * @param  Container $app
74
+     *
75
+     * @return Container
76
+     */
77
+    public function bindCoreClasses(Container $app)
78
+    {
79
+        // Cancel if in the scope of a Laravel application
80
+        if ($app->bound('events')) {
81
+            return $app;
82
+        }
83
+
84
+        // Core classes
85
+        //////////////////////////////////////////////////////////////////
86
+
87
+        $app->bindIf('files', 'Illuminate\Filesystem\Filesystem');
88
+        $app->bindIf('url', 'Illuminate\Routing\UrlGenerator');
89
+
90
+        // Session and request
91
+        //////////////////////////////////////////////////////////////////
92
+
93
+        $app->bindIf('session.manager', function ($app) {
94
+            return new SessionManager($app);
95
+        });
96
+
97
+        $app->bindIf('session', function ($app) {
98
+            return $app['session.manager']->driver('array');
99
+        }, true);
100
+
101
+        $app->bindIf('request', function ($app) {
102
+            $request = Request::createFromGlobals();
103
+            if (method_exists($request, 'setSessionStore')) {
104
+                $request->setSessionStore($app['session']);
105
+            } else if (method_exists($request, 'setLaravelSession')) {
106
+                $request->setLaravelSession($app['session']);
107
+            } else {
108
+                $request->setSession($app['session']);
109
+            }
110
+
111
+            return $request;
112
+        }, true);
113
+
114
+        // Config
115
+        //////////////////////////////////////////////////////////////////
116
+
117
+        $app->bindIf('path.config', function ($app) {
118
+            return __DIR__ . '/../config/';
119
+        }, true);
120
+
121
+        $app->bindIf('config', function ($app) {
122
+            $config = new Repository;
123
+            $this->loadConfigurationFiles($app, $config);
124
+            return $config;
125
+        }, true);
126
+
127
+        // Localization
128
+        //////////////////////////////////////////////////////////////////
129
+
130
+        $app->bindIf('translation.loader', function ($app) {
131
+            return new FileLoader($app['files'], 'src/config');
132
+        });
133
+
134
+        $app->bindIf('translator', function ($app) {
135
+            $loader = new FileLoader($app['files'], 'lang');
136
+
137
+            return new Translator($loader, 'fr');
138
+        });
139
+
140
+        return $app;
141
+    }
142
+
143
+    /**
144
+     * Load the configuration items from all of the files.
145
+     *
146
+     * @param  Container $app
147
+     * @param  Repository  $config
148
+     * @return void
149
+     */
150
+    protected function loadConfigurationFiles($app, Repository $config)
151
+    {
152
+        foreach ($this->getConfigurationFiles($app) as $key => $path)
153
+        {
154
+            $config->set($key, require $path);
155
+        }
156
+    }
157
+
158
+    /**
159
+     * Get all of the configuration files for the application.
160
+     *
161
+     * @param  $app
162
+     * @return array
163
+     */
164
+    protected function getConfigurationFiles($app)
165
+    {
166
+        $files = array();
167
+
168
+        foreach (Finder::create()->files()->name('*.php')->in($app['path.config']) as $file)
169
+        {
170
+            $files[basename($file->getRealPath(), '.php')] = $file->getRealPath();
171
+        }
172
+
173
+        return $files;
174
+    }
175
+
176
+    /**
177
+     * Bind Former classes to the container
178
+     *
179
+     * @param  Container $app
180
+     *
181
+     * @return Container
182
+     */
183
+    public function bindFormer(Container $app)
184
+    {
185
+        // Add config namespace
186
+        $configPath = __DIR__ . '/../config/former.php';
187
+        $this->mergeConfigFrom($configPath, 'former');
188
+        $this->publishes([$configPath => $app['path.config'] . '/former.php']);
189 189
 		
190
-		$framework = $app['config']->get('former.framework');
190
+        $framework = $app['config']->get('former.framework');
191 191
 		
192
-		$app->bind('former.framework', function ($app) {
193
-			return $app['former']->getFrameworkInstance($app['config']->get('former.framework'));
194
-		});
192
+        $app->bind('former.framework', function ($app) {
193
+            return $app['former']->getFrameworkInstance($app['config']->get('former.framework'));
194
+        });
195 195
 
196
-		$app->singleton('former.populator', function ($app) {
197
-			return new Populator();
198
-		});
196
+        $app->singleton('former.populator', function ($app) {
197
+            return new Populator();
198
+        });
199 199
 
200
-		$app->singleton('former.dispatcher', function ($app) {
201
-			return new MethodDispatcher($app, Former::FIELDSPACE);
202
-		});
200
+        $app->singleton('former.dispatcher', function ($app) {
201
+            return new MethodDispatcher($app, Former::FIELDSPACE);
202
+        });
203 203
 
204
-		$app->singleton('former', function ($app) {
205
-			return new Former($app, $app->make('former.dispatcher'));
206
-		});
207
-		$app->alias('former', 'Former\Former');
204
+        $app->singleton('former', function ($app) {
205
+            return new Former($app, $app->make('former.dispatcher'));
206
+        });
207
+        $app->alias('former', 'Former\Former');
208 208
 
209
-		Helpers::setApp($app);
209
+        Helpers::setApp($app);
210 210
 
211
-		return $app;
212
-	}
211
+        return $app;
212
+    }
213 213
 }
Please login to merge, or discard this patch.
src/Former/Framework/TwitterBootstrap3.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -139,7 +139,7 @@
 block discarded – undo
139 139
 		$classes = array_intersect($classes, $this->fields);
140 140
 
141 141
 		// Prepend field type
142
-		$classes = array_map(function ($class) {
142
+		$classes = array_map(function($class) {
143 143
 			return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
144 144
 		}, $classes);
145 145
 
Please login to merge, or discard this patch.
Indentation   +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 .= implode('', $prepend);
410
-		$return .= $field->render();
411
-		$return .= implode('', $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 .= implode('', $prepend);
410
+        $return .= $field->render();
411
+        $return .= implode('', $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/TwitterBootstrap.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -124,7 +124,7 @@
 block discarded – undo
124 124
 		$classes = array_intersect($classes, $this->fields);
125 125
 
126 126
 		// Prepend field type
127
-		$classes = array_map(function ($class) {
127
+		$classes = array_map(function($class) {
128 128
 			return Str::startsWith($class, 'span') ? $class : 'input-'.$class;
129 129
 		}, $classes);
130 130
 
Please login to merge, or discard this patch.
Indentation   +370 added lines, -370 removed lines patch added patch discarded remove patch
@@ -14,374 +14,374 @@
 block discarded – undo
14 14
  */
15 15
 class TwitterBootstrap extends Framework implements FrameworkInterface
16 16
 {
17
-	/**
18
-	 * Form types that trigger special styling for this Framework
19
-	 *
20
-	 * @var array
21
-	 */
22
-	protected $availableTypes = array('horizontal', 'vertical', 'inline', 'search');
23
-
24
-	/**
25
-	 * The button types available
26
-	 *
27
-	 * @var array
28
-	 */
29
-	private $buttons = array(
30
-		'large',
31
-		'small',
32
-		'mini',
33
-		'block',
34
-		'danger',
35
-		'info',
36
-		'inverse',
37
-		'link',
38
-		'primary',
39
-		'success',
40
-		'warning',
41
-	);
42
-
43
-	/**
44
-	 * The field sizes available
45
-	 *
46
-	 * @var array
47
-	 */
48
-	private $fields = array(
49
-		'mini',
50
-		'small',
51
-		'medium',
52
-		'large',
53
-		'xlarge',
54
-		'xxlarge',
55
-		'span1',
56
-		'span2',
57
-		'span3',
58
-		'span4',
59
-		'span5',
60
-		'span6',
61
-		'span7',
62
-		'span8',
63
-		'span9',
64
-		'span10',
65
-		'span11',
66
-		'span12',
67
-	);
68
-
69
-	/**
70
-	 * The field states available
71
-	 *
72
-	 * @var array
73
-	 */
74
-	protected $states = array(
75
-		'success',
76
-		'warning',
77
-		'error',
78
-		'info',
79
-	);
80
-
81
-	/**
82
-	 * Create a new TwitterBootstrap instance
83
-	 *
84
-	 * @param \Illuminate\Container\Container $app
85
-	 */
86
-	public function __construct(Container $app)
87
-	{
88
-		$this->app = $app;
89
-		$this->setFrameworkDefaults();
90
-	}
91
-
92
-	////////////////////////////////////////////////////////////////////
93
-	/////////////////////////// FILTER ARRAYS //////////////////////////
94
-	////////////////////////////////////////////////////////////////////
95
-
96
-	/**
97
-	 * Filter buttons classes
98
-	 *
99
-	 * @param  array $classes An array of classes
100
-	 *
101
-	 * @return string[] A filtered array
102
-	 */
103
-	public function filterButtonClasses($classes)
104
-	{
105
-		// Filter classes
106
-		// $classes = array_intersect($classes, $this->buttons);
107
-
108
-		// Prepend button type
109
-		$classes   = $this->prependWith($classes, 'btn-');
110
-		$classes[] = 'btn';
111
-
112
-		return $classes;
113
-	}
114
-
115
-	/**
116
-	 * Filter field classes
117
-	 *
118
-	 * @param  array $classes An array of classes
119
-	 *
120
-	 * @return array A filtered array
121
-	 */
122
-	public function filterFieldClasses($classes)
123
-	{
124
-		// Filter classes
125
-		$classes = array_intersect($classes, $this->fields);
126
-
127
-		// Prepend field type
128
-		$classes = array_map(function ($class) {
129
-			return Str::startsWith($class, 'span') ? $class : 'input-'.$class;
130
-		}, $classes);
131
-
132
-		return $classes;
133
-	}
134
-
135
-	////////////////////////////////////////////////////////////////////
136
-	///////////////////////////// ADD CLASSES //////////////////////////
137
-	////////////////////////////////////////////////////////////////////
138
-
139
-	/**
140
-	 * Add classes to a field
141
-	 *
142
-	 * @param Field $field
143
-	 * @param array $classes The possible classes to add
144
-	 *
145
-	 * @return Field
146
-	 */
147
-	public function getFieldClasses(Field $field, $classes)
148
-	{
149
-		// Add inline class for checkables
150
-		if ($field->isCheckable() and in_array('inline', $classes)) {
151
-			$field->inline();
152
-		}
153
-
154
-		// Filter classes according to field type
155
-		if ($field->isButton()) {
156
-			$classes = $this->filterButtonClasses($classes);
157
-		} else {
158
-			$classes = $this->filterFieldClasses($classes);
159
-		}
160
-
161
-		return $this->addClassesToField($field, $classes);
162
-	}
163
-
164
-	/**
165
-	 * Add group classes
166
-	 *
167
-	 * @return string A list of group classes
168
-	 */
169
-	public function getGroupClasses()
170
-	{
171
-		return 'control-group';
172
-	}
173
-
174
-	/**
175
-	 * Add label classes
176
-	 *
177
-	 * @return string An array of attributes with the label class
178
-	 */
179
-	public function getLabelClasses()
180
-	{
181
-		return 'control-label';
182
-	}
183
-
184
-	/**
185
-	 * Add uneditable field classes
186
-	 *
187
-	 * @return string An array of attributes with the uneditable class
188
-	 */
189
-	public function getUneditableClasses()
190
-	{
191
-		return 'uneditable-input';
192
-	}
193
-
194
-	public function getPlainTextClasses()
195
-	{
196
-		return null;
197
-	}
198
-
199
-	/**
200
-	 * Add form class
201
-	 *
202
-	 * @param  string $type The type of form to add
203
-	 *
204
-	 * @return string|null
205
-	 */
206
-	public function getFormClasses($type)
207
-	{
208
-		return $type ? 'form-'.$type : null;
209
-	}
210
-
211
-	/**
212
-	 * Add actions block class
213
-	 *
214
-	 * @return string
215
-	 */
216
-	public function getActionClasses()
217
-	{
218
-		return 'form-actions';
219
-	}
220
-
221
-	////////////////////////////////////////////////////////////////////
222
-	//////////////////////////// RENDER BLOCKS /////////////////////////
223
-	////////////////////////////////////////////////////////////////////
224
-
225
-	/**
226
-	 * Render an help text
227
-	 *
228
-	 * @param string $text
229
-	 * @param array  $attributes
230
-	 *
231
-	 * @return Element
232
-	 */
233
-	public function createHelp($text, $attributes = array())
234
-	{
235
-		return Element::create('span', $text, $attributes)->addClass('help-inline');
236
-	}
237
-
238
-	/**
239
-	 * Render a block help text
240
-	 *
241
-	 * @param string $text
242
-	 * @param array  $attributes
243
-	 *
244
-	 * @return Element
245
-	 */
246
-	public function createBlockHelp($text, $attributes = array())
247
-	{
248
-		return Element::create('p', $text, $attributes)->addClass('help-block');
249
-	}
250
-
251
-	/**
252
-	 * Render a disabled field
253
-	 *
254
-	 * @param Field $field
255
-	 *
256
-	 * @return Element
257
-	 */
258
-	public function createDisabledField(Field $field)
259
-	{
260
-		return Element::create('span', $field->getValue(), $field->getAttributes());
261
-	}
262
-
263
-	/**
264
-	 * Render a plain text field
265
-	 * Which fallback to a disabled field
266
-	 *
267
-	 * @param Field $field
268
-	 *
269
-	 * @return Element
270
-	 */
271
-	public function createPlainTextField(Field $field)
272
-	{
273
-		return $this->createDisabledField($field);
274
-	}
275
-
276
-	/**
277
-	 * Render an icon
278
-	 *
279
-	 * @param array $attributes Its general attributes
280
-	 *
281
-	 * @return string
282
-	 */
283
-	public function createIcon($iconType, $attributes = array(), $iconSettings = array())
284
-	{
285
-		// Check for empty icons
286
-		if (!$iconType) {
287
-			return false;
288
-		}
289
-
290
-		// Create tag
291
-		$tag  = Arr::get($iconSettings, 'tag', $this->iconTag);
292
-		$icon = Element::create($tag, null, $attributes);
293
-
294
-		// White icons ignore user overrides to use legacy Bootstrap styling
295
-		if (Str::contains($iconType, 'white')) {
296
-			$iconType = str_replace('white', '', $iconType);
297
-			$iconType = trim($iconType, '-');
298
-			$icon->addClass('icon-white');
299
-			$set    = null;
300
-			$prefix = 'icon';
301
-		} else {
302
-			$set    = Arr::get($iconSettings, 'set', $this->iconSet);
303
-			$prefix = Arr::get($iconSettings, 'prefix', $this->iconPrefix);
304
-		}
305
-		$icon->addClass("$set $prefix-$iconType");
306
-
307
-		return $icon;
308
-	}
309
-
310
-	////////////////////////////////////////////////////////////////////
311
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
312
-	////////////////////////////////////////////////////////////////////
313
-
314
-	/**
315
-	 * Wrap an item to be prepended or appended to the current field
316
-	 *
317
-	 * @param  string $item
318
-	 *
319
-	 * @return string A wrapped item
320
-	 */
321
-	public function placeAround($item)
322
-	{
323
-		// Render object
324
-		if (is_object($item) and method_exists($item, '__toString')) {
325
-			$item = $item->__toString();
326
-		}
327
-
328
-		// Return unwrapped if button
329
-		if (strpos($item, '<button') !== false) {
330
-			return $item;
331
-		}
332
-
333
-		return Element::create('span', $item)->addClass('add-on');
334
-	}
335
-
336
-	/**
337
-	 * Wrap a field with prepended and appended items
338
-	 *
339
-	 * @param  Field $field
340
-	 * @param  array $prepend
341
-	 * @param  array $append
342
-	 *
343
-	 * @return string A field concatented with prepended and/or appended items
344
-	 */
345
-	public function prependAppend($field, $prepend, $append)
346
-	{
347
-		$class = array();
348
-		if ($prepend) {
349
-			$class[] = 'input-prepend';
350
-		}
351
-		if ($append) {
352
-			$class[] = 'input-append';
353
-		}
354
-
355
-		$return = '<div class="'.join(' ', $class).'">';
356
-		$return .= implode('', $prepend);
357
-		$return .= $field->render();
358
-		$return .= implode('', $append);
359
-		$return .= '</div>';
360
-
361
-		return $return;
362
-	}
363
-
364
-	/**
365
-	 * Wrap a field with potential additional tags
366
-	 *
367
-	 * @param  Field $field
368
-	 *
369
-	 * @return Element A wrapped field
370
-	 */
371
-	public function wrapField($field)
372
-	{
373
-		return Element::create('div', $field)->addClass('controls');
374
-	}
375
-
376
-	/**
377
-	 * Wrap actions block with potential additional tags
378
-	 *
379
-	 * @param  Actions $actions
380
-	 *
381
-	 * @return string A wrapped actions block
382
-	 */
383
-	public function wrapActions($actions)
384
-	{
385
-		return $actions;
386
-	}
17
+    /**
18
+     * Form types that trigger special styling for this Framework
19
+     *
20
+     * @var array
21
+     */
22
+    protected $availableTypes = array('horizontal', 'vertical', 'inline', 'search');
23
+
24
+    /**
25
+     * The button types available
26
+     *
27
+     * @var array
28
+     */
29
+    private $buttons = array(
30
+        'large',
31
+        'small',
32
+        'mini',
33
+        'block',
34
+        'danger',
35
+        'info',
36
+        'inverse',
37
+        'link',
38
+        'primary',
39
+        'success',
40
+        'warning',
41
+    );
42
+
43
+    /**
44
+     * The field sizes available
45
+     *
46
+     * @var array
47
+     */
48
+    private $fields = array(
49
+        'mini',
50
+        'small',
51
+        'medium',
52
+        'large',
53
+        'xlarge',
54
+        'xxlarge',
55
+        'span1',
56
+        'span2',
57
+        'span3',
58
+        'span4',
59
+        'span5',
60
+        'span6',
61
+        'span7',
62
+        'span8',
63
+        'span9',
64
+        'span10',
65
+        'span11',
66
+        'span12',
67
+    );
68
+
69
+    /**
70
+     * The field states available
71
+     *
72
+     * @var array
73
+     */
74
+    protected $states = array(
75
+        'success',
76
+        'warning',
77
+        'error',
78
+        'info',
79
+    );
80
+
81
+    /**
82
+     * Create a new TwitterBootstrap instance
83
+     *
84
+     * @param \Illuminate\Container\Container $app
85
+     */
86
+    public function __construct(Container $app)
87
+    {
88
+        $this->app = $app;
89
+        $this->setFrameworkDefaults();
90
+    }
91
+
92
+    ////////////////////////////////////////////////////////////////////
93
+    /////////////////////////// FILTER ARRAYS //////////////////////////
94
+    ////////////////////////////////////////////////////////////////////
95
+
96
+    /**
97
+     * Filter buttons classes
98
+     *
99
+     * @param  array $classes An array of classes
100
+     *
101
+     * @return string[] A filtered array
102
+     */
103
+    public function filterButtonClasses($classes)
104
+    {
105
+        // Filter classes
106
+        // $classes = array_intersect($classes, $this->buttons);
107
+
108
+        // Prepend button type
109
+        $classes   = $this->prependWith($classes, 'btn-');
110
+        $classes[] = 'btn';
111
+
112
+        return $classes;
113
+    }
114
+
115
+    /**
116
+     * Filter field classes
117
+     *
118
+     * @param  array $classes An array of classes
119
+     *
120
+     * @return array A filtered array
121
+     */
122
+    public function filterFieldClasses($classes)
123
+    {
124
+        // Filter classes
125
+        $classes = array_intersect($classes, $this->fields);
126
+
127
+        // Prepend field type
128
+        $classes = array_map(function ($class) {
129
+            return Str::startsWith($class, 'span') ? $class : 'input-'.$class;
130
+        }, $classes);
131
+
132
+        return $classes;
133
+    }
134
+
135
+    ////////////////////////////////////////////////////////////////////
136
+    ///////////////////////////// ADD CLASSES //////////////////////////
137
+    ////////////////////////////////////////////////////////////////////
138
+
139
+    /**
140
+     * Add classes to a field
141
+     *
142
+     * @param Field $field
143
+     * @param array $classes The possible classes to add
144
+     *
145
+     * @return Field
146
+     */
147
+    public function getFieldClasses(Field $field, $classes)
148
+    {
149
+        // Add inline class for checkables
150
+        if ($field->isCheckable() and in_array('inline', $classes)) {
151
+            $field->inline();
152
+        }
153
+
154
+        // Filter classes according to field type
155
+        if ($field->isButton()) {
156
+            $classes = $this->filterButtonClasses($classes);
157
+        } else {
158
+            $classes = $this->filterFieldClasses($classes);
159
+        }
160
+
161
+        return $this->addClassesToField($field, $classes);
162
+    }
163
+
164
+    /**
165
+     * Add group classes
166
+     *
167
+     * @return string A list of group classes
168
+     */
169
+    public function getGroupClasses()
170
+    {
171
+        return 'control-group';
172
+    }
173
+
174
+    /**
175
+     * Add label classes
176
+     *
177
+     * @return string An array of attributes with the label class
178
+     */
179
+    public function getLabelClasses()
180
+    {
181
+        return 'control-label';
182
+    }
183
+
184
+    /**
185
+     * Add uneditable field classes
186
+     *
187
+     * @return string An array of attributes with the uneditable class
188
+     */
189
+    public function getUneditableClasses()
190
+    {
191
+        return 'uneditable-input';
192
+    }
193
+
194
+    public function getPlainTextClasses()
195
+    {
196
+        return null;
197
+    }
198
+
199
+    /**
200
+     * Add form class
201
+     *
202
+     * @param  string $type The type of form to add
203
+     *
204
+     * @return string|null
205
+     */
206
+    public function getFormClasses($type)
207
+    {
208
+        return $type ? 'form-'.$type : null;
209
+    }
210
+
211
+    /**
212
+     * Add actions block class
213
+     *
214
+     * @return string
215
+     */
216
+    public function getActionClasses()
217
+    {
218
+        return 'form-actions';
219
+    }
220
+
221
+    ////////////////////////////////////////////////////////////////////
222
+    //////////////////////////// RENDER BLOCKS /////////////////////////
223
+    ////////////////////////////////////////////////////////////////////
224
+
225
+    /**
226
+     * Render an help text
227
+     *
228
+     * @param string $text
229
+     * @param array  $attributes
230
+     *
231
+     * @return Element
232
+     */
233
+    public function createHelp($text, $attributes = array())
234
+    {
235
+        return Element::create('span', $text, $attributes)->addClass('help-inline');
236
+    }
237
+
238
+    /**
239
+     * Render a block help text
240
+     *
241
+     * @param string $text
242
+     * @param array  $attributes
243
+     *
244
+     * @return Element
245
+     */
246
+    public function createBlockHelp($text, $attributes = array())
247
+    {
248
+        return Element::create('p', $text, $attributes)->addClass('help-block');
249
+    }
250
+
251
+    /**
252
+     * Render a disabled field
253
+     *
254
+     * @param Field $field
255
+     *
256
+     * @return Element
257
+     */
258
+    public function createDisabledField(Field $field)
259
+    {
260
+        return Element::create('span', $field->getValue(), $field->getAttributes());
261
+    }
262
+
263
+    /**
264
+     * Render a plain text field
265
+     * Which fallback to a disabled field
266
+     *
267
+     * @param Field $field
268
+     *
269
+     * @return Element
270
+     */
271
+    public function createPlainTextField(Field $field)
272
+    {
273
+        return $this->createDisabledField($field);
274
+    }
275
+
276
+    /**
277
+     * Render an icon
278
+     *
279
+     * @param array $attributes Its general attributes
280
+     *
281
+     * @return string
282
+     */
283
+    public function createIcon($iconType, $attributes = array(), $iconSettings = array())
284
+    {
285
+        // Check for empty icons
286
+        if (!$iconType) {
287
+            return false;
288
+        }
289
+
290
+        // Create tag
291
+        $tag  = Arr::get($iconSettings, 'tag', $this->iconTag);
292
+        $icon = Element::create($tag, null, $attributes);
293
+
294
+        // White icons ignore user overrides to use legacy Bootstrap styling
295
+        if (Str::contains($iconType, 'white')) {
296
+            $iconType = str_replace('white', '', $iconType);
297
+            $iconType = trim($iconType, '-');
298
+            $icon->addClass('icon-white');
299
+            $set    = null;
300
+            $prefix = 'icon';
301
+        } else {
302
+            $set    = Arr::get($iconSettings, 'set', $this->iconSet);
303
+            $prefix = Arr::get($iconSettings, 'prefix', $this->iconPrefix);
304
+        }
305
+        $icon->addClass("$set $prefix-$iconType");
306
+
307
+        return $icon;
308
+    }
309
+
310
+    ////////////////////////////////////////////////////////////////////
311
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
312
+    ////////////////////////////////////////////////////////////////////
313
+
314
+    /**
315
+     * Wrap an item to be prepended or appended to the current field
316
+     *
317
+     * @param  string $item
318
+     *
319
+     * @return string A wrapped item
320
+     */
321
+    public function placeAround($item)
322
+    {
323
+        // Render object
324
+        if (is_object($item) and method_exists($item, '__toString')) {
325
+            $item = $item->__toString();
326
+        }
327
+
328
+        // Return unwrapped if button
329
+        if (strpos($item, '<button') !== false) {
330
+            return $item;
331
+        }
332
+
333
+        return Element::create('span', $item)->addClass('add-on');
334
+    }
335
+
336
+    /**
337
+     * Wrap a field with prepended and appended items
338
+     *
339
+     * @param  Field $field
340
+     * @param  array $prepend
341
+     * @param  array $append
342
+     *
343
+     * @return string A field concatented with prepended and/or appended items
344
+     */
345
+    public function prependAppend($field, $prepend, $append)
346
+    {
347
+        $class = array();
348
+        if ($prepend) {
349
+            $class[] = 'input-prepend';
350
+        }
351
+        if ($append) {
352
+            $class[] = 'input-append';
353
+        }
354
+
355
+        $return = '<div class="'.join(' ', $class).'">';
356
+        $return .= implode('', $prepend);
357
+        $return .= $field->render();
358
+        $return .= implode('', $append);
359
+        $return .= '</div>';
360
+
361
+        return $return;
362
+    }
363
+
364
+    /**
365
+     * Wrap a field with potential additional tags
366
+     *
367
+     * @param  Field $field
368
+     *
369
+     * @return Element A wrapped field
370
+     */
371
+    public function wrapField($field)
372
+    {
373
+        return Element::create('div', $field)->addClass('controls');
374
+    }
375
+
376
+    /**
377
+     * Wrap actions block with potential additional tags
378
+     *
379
+     * @param  Actions $actions
380
+     *
381
+     * @return string A wrapped actions block
382
+     */
383
+    public function wrapActions($actions)
384
+    {
385
+        return $actions;
386
+    }
387 387
 }
Please login to merge, or discard this patch.
src/Former/LiveValidation.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@
 block discarded – undo
21 21
 	 *
22 22
 	 * @param Field $field The field
23 23
 	 */
24
-	public function __construct(Field &$field)
24
+	public function __construct(Field&$field)
25 25
 	{
26 26
 		$this->field = $field;
27 27
 	}
Please login to merge, or discard this patch.
Indentation   +334 added lines, -334 removed lines patch added patch discarded remove patch
@@ -9,338 +9,338 @@
 block discarded – undo
9 9
  */
10 10
 class LiveValidation
11 11
 {
12
-	/**
13
-	 * The field being worked on
14
-	 *
15
-	 * @var Field
16
-	 */
17
-	public $field;
18
-
19
-	/**
20
-	 * Load a Field instance to apply rules to it
21
-	 *
22
-	 * @param Field $field The field
23
-	 */
24
-	public function __construct(Field &$field)
25
-	{
26
-		$this->field = $field;
27
-	}
28
-
29
-	/**
30
-	 * Apply live validation rules to a field
31
-	 *
32
-	 * @param array $rules The rules to apply
33
-	 */
34
-	public function apply($rules)
35
-	{
36
-		// If no rules to apply, cancel
37
-		if (!$rules) {
38
-			return false;
39
-		}
40
-
41
-		foreach ($rules as $rule => $parameters) {
42
-
43
-			// If the rule is unsupported yet, skip it
44
-			if (!method_exists($this, $rule)) {
45
-				continue;
46
-			}
47
-
48
-			$this->$rule($parameters);
49
-		}
50
-	}
51
-
52
-	////////////////////////////////////////////////////////////////////
53
-	//////////////////////////////// RULES /////////////////////////////
54
-	////////////////////////////////////////////////////////////////////
55
-
56
-	// Field types
57
-	////////////////////////////////////////////////////////////////////
58
-
59
-	/**
60
-	 * Email field
61
-	 */
62
-	public function email()
63
-	{
64
-		$this->field->setType('email');
65
-	}
66
-
67
-	/**
68
-	 * URL field
69
-	 */
70
-	public function url()
71
-	{
72
-		$this->field->setType('url');
73
-	}
74
-
75
-	/**
76
-	 * Required field
77
-	 */
78
-	public function required()
79
-	{
80
-		$this->field->required();
81
-	}
82
-
83
-	// Patterns
84
-	////////////////////////////////////////////////////////////////////
85
-
86
-	/**
87
-	 * Integer field
88
-	 */
89
-	public function integer()
90
-	{
91
-		$this->field->pattern('\d+');
92
-	}
93
-
94
-	/**
95
-	 * Numeric field
96
-	 */
97
-	public function numeric()
98
-	{
99
-		if ($this->field->isOfType('number')) {
100
-			$this->field->step('any');
101
-		} else {
102
-			$this->field->pattern('[+-]?\d*\.?\d+');
103
-		}
104
-	}
105
-
106
-	/**
107
-	 * Not numeric field
108
-	 */
109
-	public function not_numeric()
110
-	{
111
-		$this->field->pattern('\D+');
112
-	}
113
-
114
-	/**
115
-	 * Only alphanumerical
116
-	 */
117
-	public function alpha()
118
-	{
119
-		$this->field->pattern('[a-zA-Z]+');
120
-	}
121
-
122
-	/**
123
-	 * Only alphanumerical and numbers
124
-	 */
125
-	public function alpha_num()
126
-	{
127
-		$this->field->pattern('[a-zA-Z0-9]+');
128
-	}
129
-
130
-	/**
131
-	 * Alphanumerical, numbers and dashes
132
-	 */
133
-	public function alpha_dash()
134
-	{
135
-		$this->field->pattern('[a-zA-Z0-9_\-]+');
136
-	}
137
-
138
-	/**
139
-	 * In []
140
-	 */
141
-	public function in($possible)
142
-	{
143
-		// Create the corresponding regex
144
-		$possible = (sizeof($possible) == 1) ? $possible[0] : '('.join('|', $possible).')';
145
-
146
-		$this->field->pattern('^'.$possible.'$');
147
-	}
148
-
149
-	/**
150
-	 * Not in []
151
-	 */
152
-	public function not_in($impossible)
153
-	{
154
-		$this->field->pattern('(?:(?!^'.join('$|^', $impossible).'$).)*');
155
-	}
156
-
157
-	/**
158
-	 * Matches a pattern
159
-	 */
160
-	public function match($pattern)
161
-	{
162
-		// Remove delimiters from existing regex
163
-		$pattern = substr($pattern[0], 1, -1);
164
-
165
-		$this->field->pattern($pattern);
166
-	}
167
-
168
-	/**
169
-	 * Alias for match
170
-	 */
171
-	public function regex($pattern)
172
-	{
173
-		return $this->match($pattern);
174
-	}
175
-
176
-	// Boundaries
177
-	////////////////////////////////////////////////////////////////////
178
-
179
-	/**
180
-	 * Max value
181
-	 */
182
-	public function max($max)
183
-	{
184
-		if ($this->field->isOfType('file')) {
185
-			$this->size($max);
186
-		} else {
187
-			$this->setMax($max[0]);
188
-		}
189
-	}
190
-
191
-	/**
192
-	 * Max size
193
-	 */
194
-	public function size($size)
195
-	{
196
-		$this->field->max($size[0]);
197
-	}
198
-
199
-	/**
200
-	 * Min value
201
-	 */
202
-	public function min($min)
203
-	{
204
-		$this->setMin($min[0]);
205
-	}
206
-
207
-	/**
208
-	 * Set boundaries
209
-	 */
210
-	public function between($between)
211
-	{
212
-		list($min, $max) = $between;
213
-
214
-		$this->setBetween($min, $max);
215
-	}
216
-
217
-	/**
218
-	 * Set accepted mime types
219
-	 *
220
-	 * @param string[] $mimes
221
-	 */
222
-	public function mimes($mimes)
223
-	{
224
-		// Only useful on file fields
225
-		if (!$this->field->isOfType('file')) {
226
-			return false;
227
-		}
228
-
229
-		$this->field->accept($this->setAccepted($mimes));
230
-	}
231
-
232
-	/**
233
-	 * Set accept only images
234
-	 */
235
-	public function image()
236
-	{
237
-		$this->mimes(array('jpg', 'png', 'gif', 'bmp'));
238
-	}
239
-
240
-	// Dates
241
-	////////////////////////////////////////////////////////////////////
242
-
243
-	/**
244
-	 * Before a date
245
-	 */
246
-	public function before($date)
247
-	{
248
-		list($format, $date) = $this->formatDate($date[0]);
249
-
250
-		$this->field->max(date($format, $date));
251
-	}
252
-
253
-	/**
254
-	 * After a date
255
-	 */
256
-	public function after($date)
257
-	{
258
-		list($format, $date) = $this->formatDate($date[0]);
259
-
260
-		$this->field->min(date($format, $date));
261
-	}
262
-
263
-	////////////////////////////////////////////////////////////////////
264
-	////////////////////////////// HELPERS /////////////////////////////
265
-	////////////////////////////////////////////////////////////////////
266
-
267
-	/**
268
-	 * Transform extensions and mime groups into a list of mime types
269
-	 *
270
-	 * @param  array $mimes An array of mimes
271
-	 *
272
-	 * @return string A concatenated list of mimes
273
-	 */
274
-	private function setAccepted($mimes)
275
-	{
276
-		// Transform extensions or mime groups into mime types
277
-		$mimes = array_map(array('\Laravel\File', 'mime'), $mimes);
278
-
279
-		return implode(',', $mimes);
280
-	}
281
-
282
-	/**
283
-	 * Format a date to a pattern
284
-	 *
285
-	 * @param  string $date The date
286
-	 *
287
-	 * @return string The pattern
288
-	 */
289
-	private function formatDate($date)
290
-	{
291
-		$format = 'Y-m-d';
292
-
293
-		// Add hour for datetime fields
294
-		if ($this->field->isOfType('datetime', 'datetime-local')) {
295
-			$format .= '\TH:i:s';
296
-		}
297
-
298
-		return array($format, strtotime($date));
299
-	}
300
-
301
-	/**
302
-	 * Set a maximum value to a field
303
-	 *
304
-	 * @param integer $max
305
-	 */
306
-	private function setMax($max)
307
-	{
308
-		$attribute = $this->field->isOfType('number') ? 'max' : 'maxlength';
309
-
310
-		$this->field->$attribute($max);
311
-	}
312
-
313
-	/**
314
-	 * Set a minimum value to a field
315
-	 *
316
-	 * @param integer $min
317
-	 */
318
-	private function setMin($min)
319
-	{
320
-		if ($this->field->isOfType('number') == 'min') {
321
-			$this->field->min($min);
322
-		} else {
323
-			$this->field->pattern(".{".$min.",}");
324
-		}
325
-	}
326
-
327
-	/**
328
-	 * Set a minimum and maximum value to a field
329
-	 *
330
-	 * @param $min
331
-	 * @param $max
332
-	 */
333
-	public function setBetween($min, $max)
334
-	{
335
-		if ($this->field->isOfType('number') == 'min') {
336
-			// min, max values for generation of the pattern
337
-			$this->field->min($min);
338
-			$this->field->max($max);
339
-		} else {
340
-			$this->field->pattern('.{'.$min.','.$max.'}');
341
-
342
-			// still let the browser limit text input after reaching the max
343
-			$this->field->maxlength($max);
344
-		}
345
-	}
12
+    /**
13
+     * The field being worked on
14
+     *
15
+     * @var Field
16
+     */
17
+    public $field;
18
+
19
+    /**
20
+     * Load a Field instance to apply rules to it
21
+     *
22
+     * @param Field $field The field
23
+     */
24
+    public function __construct(Field &$field)
25
+    {
26
+        $this->field = $field;
27
+    }
28
+
29
+    /**
30
+     * Apply live validation rules to a field
31
+     *
32
+     * @param array $rules The rules to apply
33
+     */
34
+    public function apply($rules)
35
+    {
36
+        // If no rules to apply, cancel
37
+        if (!$rules) {
38
+            return false;
39
+        }
40
+
41
+        foreach ($rules as $rule => $parameters) {
42
+
43
+            // If the rule is unsupported yet, skip it
44
+            if (!method_exists($this, $rule)) {
45
+                continue;
46
+            }
47
+
48
+            $this->$rule($parameters);
49
+        }
50
+    }
51
+
52
+    ////////////////////////////////////////////////////////////////////
53
+    //////////////////////////////// RULES /////////////////////////////
54
+    ////////////////////////////////////////////////////////////////////
55
+
56
+    // Field types
57
+    ////////////////////////////////////////////////////////////////////
58
+
59
+    /**
60
+     * Email field
61
+     */
62
+    public function email()
63
+    {
64
+        $this->field->setType('email');
65
+    }
66
+
67
+    /**
68
+     * URL field
69
+     */
70
+    public function url()
71
+    {
72
+        $this->field->setType('url');
73
+    }
74
+
75
+    /**
76
+     * Required field
77
+     */
78
+    public function required()
79
+    {
80
+        $this->field->required();
81
+    }
82
+
83
+    // Patterns
84
+    ////////////////////////////////////////////////////////////////////
85
+
86
+    /**
87
+     * Integer field
88
+     */
89
+    public function integer()
90
+    {
91
+        $this->field->pattern('\d+');
92
+    }
93
+
94
+    /**
95
+     * Numeric field
96
+     */
97
+    public function numeric()
98
+    {
99
+        if ($this->field->isOfType('number')) {
100
+            $this->field->step('any');
101
+        } else {
102
+            $this->field->pattern('[+-]?\d*\.?\d+');
103
+        }
104
+    }
105
+
106
+    /**
107
+     * Not numeric field
108
+     */
109
+    public function not_numeric()
110
+    {
111
+        $this->field->pattern('\D+');
112
+    }
113
+
114
+    /**
115
+     * Only alphanumerical
116
+     */
117
+    public function alpha()
118
+    {
119
+        $this->field->pattern('[a-zA-Z]+');
120
+    }
121
+
122
+    /**
123
+     * Only alphanumerical and numbers
124
+     */
125
+    public function alpha_num()
126
+    {
127
+        $this->field->pattern('[a-zA-Z0-9]+');
128
+    }
129
+
130
+    /**
131
+     * Alphanumerical, numbers and dashes
132
+     */
133
+    public function alpha_dash()
134
+    {
135
+        $this->field->pattern('[a-zA-Z0-9_\-]+');
136
+    }
137
+
138
+    /**
139
+     * In []
140
+     */
141
+    public function in($possible)
142
+    {
143
+        // Create the corresponding regex
144
+        $possible = (sizeof($possible) == 1) ? $possible[0] : '('.join('|', $possible).')';
145
+
146
+        $this->field->pattern('^'.$possible.'$');
147
+    }
148
+
149
+    /**
150
+     * Not in []
151
+     */
152
+    public function not_in($impossible)
153
+    {
154
+        $this->field->pattern('(?:(?!^'.join('$|^', $impossible).'$).)*');
155
+    }
156
+
157
+    /**
158
+     * Matches a pattern
159
+     */
160
+    public function match($pattern)
161
+    {
162
+        // Remove delimiters from existing regex
163
+        $pattern = substr($pattern[0], 1, -1);
164
+
165
+        $this->field->pattern($pattern);
166
+    }
167
+
168
+    /**
169
+     * Alias for match
170
+     */
171
+    public function regex($pattern)
172
+    {
173
+        return $this->match($pattern);
174
+    }
175
+
176
+    // Boundaries
177
+    ////////////////////////////////////////////////////////////////////
178
+
179
+    /**
180
+     * Max value
181
+     */
182
+    public function max($max)
183
+    {
184
+        if ($this->field->isOfType('file')) {
185
+            $this->size($max);
186
+        } else {
187
+            $this->setMax($max[0]);
188
+        }
189
+    }
190
+
191
+    /**
192
+     * Max size
193
+     */
194
+    public function size($size)
195
+    {
196
+        $this->field->max($size[0]);
197
+    }
198
+
199
+    /**
200
+     * Min value
201
+     */
202
+    public function min($min)
203
+    {
204
+        $this->setMin($min[0]);
205
+    }
206
+
207
+    /**
208
+     * Set boundaries
209
+     */
210
+    public function between($between)
211
+    {
212
+        list($min, $max) = $between;
213
+
214
+        $this->setBetween($min, $max);
215
+    }
216
+
217
+    /**
218
+     * Set accepted mime types
219
+     *
220
+     * @param string[] $mimes
221
+     */
222
+    public function mimes($mimes)
223
+    {
224
+        // Only useful on file fields
225
+        if (!$this->field->isOfType('file')) {
226
+            return false;
227
+        }
228
+
229
+        $this->field->accept($this->setAccepted($mimes));
230
+    }
231
+
232
+    /**
233
+     * Set accept only images
234
+     */
235
+    public function image()
236
+    {
237
+        $this->mimes(array('jpg', 'png', 'gif', 'bmp'));
238
+    }
239
+
240
+    // Dates
241
+    ////////////////////////////////////////////////////////////////////
242
+
243
+    /**
244
+     * Before a date
245
+     */
246
+    public function before($date)
247
+    {
248
+        list($format, $date) = $this->formatDate($date[0]);
249
+
250
+        $this->field->max(date($format, $date));
251
+    }
252
+
253
+    /**
254
+     * After a date
255
+     */
256
+    public function after($date)
257
+    {
258
+        list($format, $date) = $this->formatDate($date[0]);
259
+
260
+        $this->field->min(date($format, $date));
261
+    }
262
+
263
+    ////////////////////////////////////////////////////////////////////
264
+    ////////////////////////////// HELPERS /////////////////////////////
265
+    ////////////////////////////////////////////////////////////////////
266
+
267
+    /**
268
+     * Transform extensions and mime groups into a list of mime types
269
+     *
270
+     * @param  array $mimes An array of mimes
271
+     *
272
+     * @return string A concatenated list of mimes
273
+     */
274
+    private function setAccepted($mimes)
275
+    {
276
+        // Transform extensions or mime groups into mime types
277
+        $mimes = array_map(array('\Laravel\File', 'mime'), $mimes);
278
+
279
+        return implode(',', $mimes);
280
+    }
281
+
282
+    /**
283
+     * Format a date to a pattern
284
+     *
285
+     * @param  string $date The date
286
+     *
287
+     * @return string The pattern
288
+     */
289
+    private function formatDate($date)
290
+    {
291
+        $format = 'Y-m-d';
292
+
293
+        // Add hour for datetime fields
294
+        if ($this->field->isOfType('datetime', 'datetime-local')) {
295
+            $format .= '\TH:i:s';
296
+        }
297
+
298
+        return array($format, strtotime($date));
299
+    }
300
+
301
+    /**
302
+     * Set a maximum value to a field
303
+     *
304
+     * @param integer $max
305
+     */
306
+    private function setMax($max)
307
+    {
308
+        $attribute = $this->field->isOfType('number') ? 'max' : 'maxlength';
309
+
310
+        $this->field->$attribute($max);
311
+    }
312
+
313
+    /**
314
+     * Set a minimum value to a field
315
+     *
316
+     * @param integer $min
317
+     */
318
+    private function setMin($min)
319
+    {
320
+        if ($this->field->isOfType('number') == 'min') {
321
+            $this->field->min($min);
322
+        } else {
323
+            $this->field->pattern(".{".$min.",}");
324
+        }
325
+    }
326
+
327
+    /**
328
+     * Set a minimum and maximum value to a field
329
+     *
330
+     * @param $min
331
+     * @param $max
332
+     */
333
+    public function setBetween($min, $max)
334
+    {
335
+        if ($this->field->isOfType('number') == 'min') {
336
+            // min, max values for generation of the pattern
337
+            $this->field->min($min);
338
+            $this->field->max($max);
339
+        } else {
340
+            $this->field->pattern('.{'.$min.','.$max.'}');
341
+
342
+            // still let the browser limit text input after reaching the max
343
+            $this->field->maxlength($max);
344
+        }
345
+    }
346 346
 }
Please login to merge, or discard this patch.