Completed
Pull Request — master (#595)
by Alex
06:54
created
src/Former/Form/Fields/Select.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -170,7 +170,7 @@
 block discarded – undo
170 170
 	 * Set the select options
171 171
 	 *
172 172
 	 * @param  array   $_options     The options as an array
173
-	 * @param  mixed   $selected     Facultative selected entry
173
+	 * @param  null|string   $selected     Facultative selected entry
174 174
 	 * @param  boolean $valuesAsKeys Whether the array's values should be used as
175 175
 	 *                               the option's values instead of the array's keys
176 176
 	 */
Please login to merge, or discard this patch.
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 3 patches
Doc Comments   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -351,9 +351,9 @@
 block discarded – undo
351 351
 	}
352 352
 
353 353
 	/**
354
-	 * @param $name
354
+	 * @param string $name
355 355
 	 * @param $params
356
-	 * @param $type
356
+	 * @param string $type
357 357
 	 *
358 358
 	 * @return $this
359 359
 	 */
Please login to merge, or discard this patch.
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/Form/Group.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -442,7 +442,7 @@
 block discarded – undo
442 442
 	/**
443 443
 	 * Format the field with prepended/appended elements
444 444
 	 *
445
-	 * @param  Field $field The field to format
445
+	 * @param  \Former\Traits\Field $field The field to format
446 446
 	 *
447 447
 	 * @return string        Field plus supplementary elements
448 448
 	 */
Please login to merge, or discard this patch.
Indentation   +460 added lines, -460 removed lines patch added patch discarded remove patch
@@ -13,464 +13,464 @@
 block discarded – undo
13 13
  */
14 14
 class Group extends Tag
15 15
 {
16
-	/**
17
-	 * The Container
18
-	 *
19
-	 * @var Container
20
-	 */
21
-	protected $app;
22
-
23
-	/**
24
-	 * The current state of the group
25
-	 *
26
-	 * @var string
27
-	 */
28
-	protected $state = null;
29
-
30
-	/**
31
-	 * Whether the field should be displayed raw or not
32
-	 *
33
-	 * @var boolean
34
-	 */
35
-	protected $raw = false;
36
-
37
-	/**
38
-	 * The group label
39
-	 *
40
-	 * @var Element
41
-	 */
42
-	protected $label;
43
-
44
-	/**
45
-	 * The group help
46
-	 *
47
-	 * @var array
48
-	 */
49
-	protected $help = array();
50
-
51
-	/**
52
-	 * An array of elements to preprend the field
53
-	 *
54
-	 * @var array
55
-	 */
56
-	protected $prepend = array();
57
-
58
-	/**
59
-	 * An array of elements to append the field
60
-	 *
61
-	 * @var array
62
-	 */
63
-	protected $append = array();
64
-
65
-	/**
66
-	 * The field validations to be checked for errors
67
-	 *
68
-	 * @var array
69
-	 */
70
-	protected $validations = array();
71
-
72
-	/**
73
-	 * The group's element
74
-	 *
75
-	 * @var string
76
-	 */
77
-	protected $element = 'div';
78
-
79
-	/**
80
-	 * Whether a custom group is opened or not
81
-	 *
82
-	 * @var boolean
83
-	 */
84
-	public static $opened = false;
85
-
86
-	/**
87
-	 * The custom group that is open
88
-	 *
89
-	 * @var Former\Form\Group
90
-	 */
91
-	public static $openGroup = null;
92
-
93
-	////////////////////////////////////////////////////////////////////
94
-	/////////////////////////// CORE METHODS ///////////////////////////
95
-	////////////////////////////////////////////////////////////////////
96
-
97
-	/**
98
-	 * Creates a group
99
-	 *
100
-	 * @param string $label Its label
101
-	 */
102
-	public function __construct(Container $app, $label, $validations = null)
103
-	{
104
-		// Get special classes
105
-		$this->app = $app;
106
-		$this->addClass($this->app['former.framework']->getGroupClasses());
107
-
108
-		// Invisible if Nude
109
-		if ($this->app['former.framework']->is('Nude')) {
110
-			$this->element = '';
111
-		}
112
-
113
-		// Set group label
114
-		if ($label) {
115
-			$this->setLabel($label);
116
-		}
117
-
118
-		// Set validations used to override groups own conclusions
119
-		$this->validations = (array) $validations;
120
-	}
121
-
122
-	/**
123
-	 * Prints out the opening of the Control Group
124
-	 *
125
-	 * @return string A control group opening tag
126
-	 */
127
-	public function __toString()
128
-	{
129
-		return $this->open().$this->getFormattedLabel();
130
-	}
131
-
132
-	/**
133
-	 * Opens a group
134
-	 *
135
-	 * @return string Opening tag
136
-	 */
137
-	public function open()
138
-	{
139
-		if ($this->getErrors()) {
140
-			$this->state($this->app['former.framework']->errorState());
141
-		}
142
-
143
-		// Retrieve state and append it to classes
144
-		if ($this->state) {
145
-			$this->addClass($this->state);
146
-		}
147
-
148
-		// Required state
149
-		if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) {
150
-			$this->addClass($this->app['former']->getOption('required_class'));
151
-		}
152
-
153
-		return parent::open();
154
-	}
155
-
156
-	/**
157
-	 * Set the contents of the current group
158
-	 *
159
-	 * @param string $contents The group contents
160
-	 *
161
-	 * @return string A group
162
-	 */
163
-	public function contents($contents)
164
-	{
165
-		return $this->wrap($contents, $this->getFormattedLabel());
166
-	}
167
-
168
-	/**
169
-	 * Wrap a Field with the current group
170
-	 *
171
-	 * @param  \Former\Traits\Field $field A Field instance
172
-	 *
173
-	 * @return string        A group
174
-	 */
175
-	public function wrapField($field)
176
-	{
177
-		$label = $this->getLabel($field);
178
-		$field = $this->prependAppend($field);
179
-		$field .= $this->getHelp();
180
-
181
-		return $this->wrap($field, $label);
182
-	}
183
-
184
-	////////////////////////////////////////////////////////////////////
185
-	//////////////////////////// FIELD METHODS /////////////////////////
186
-	////////////////////////////////////////////////////////////////////
187
-
188
-	/**
189
-	 * Set the state of the group
190
-	 *
191
-	 * @param  string $state A Bootstrap state class
192
-	 */
193
-	public function state($state)
194
-	{
195
-		// Filter state
196
-		$state = $this->app['former.framework']->filterState($state);
197
-
198
-		$this->state = $state;
199
-	}
200
-
201
-	/**
202
-	 * Set a class on the Group
203
-	 *
204
-	 * @param string $class The class to add
205
-	 */
206
-	public function addGroupClass($class)
207
-	{
208
-		$this->addClass($class);
209
-	}
210
-
211
-	/**
212
-	 * Adds a label to the group
213
-	 *
214
-	 * @param  string $label A label
215
-	 */
216
-	public function setLabel($label)
217
-	{
218
-		if (!$label instanceof Element) {
219
-			$label = Helpers::translate($label);
220
-			$label = Element::create('label', $label)->for($label);
221
-		}
222
-
223
-		$this->label = $label;
224
-	}
225
-
226
-	/**
227
-	 * Get the formatted group label
228
-	 *
229
-	 * @return string|null
230
-	 */
231
-	public function getFormattedLabel()
232
-	{
233
-		if (!$this->label) {
234
-			return false;
235
-		}
236
-
237
-		return $this->label->addClass($this->app['former.framework']->getLabelClasses());
238
-	}
239
-
240
-	/**
241
-	 * Disables the control group for the current field
242
-	 */
243
-	public function raw()
244
-	{
245
-		$this->raw = true;
246
-	}
247
-
248
-	/**
249
-	 * Check if the current group is to be displayed or not
250
-	 *
251
-	 * @return boolean
252
-	 */
253
-	public function isRaw()
254
-	{
255
-		return (bool) $this->raw;
256
-	}
257
-
258
-	////////////////////////////////////////////////////////////////////
259
-	///////////////////////////// HELP BLOCKS //////////////////////////
260
-	////////////////////////////////////////////////////////////////////
261
-
262
-	/**
263
-	 * Alias for inlineHelp
264
-	 *
265
-	 * @param  string $help       The help text
266
-	 * @param  array  $attributes Facultative attributes
267
-	 */
268
-	public function help($help, $attributes = array())
269
-	{
270
-		return $this->inlineHelp($help, $attributes);
271
-	}
272
-
273
-	/**
274
-	 * Add an inline help
275
-	 *
276
-	 * @param  string $help       The help text
277
-	 * @param  array  $attributes Facultative attributes
278
-	 */
279
-	public function inlineHelp($help, $attributes = array())
280
-	{
281
-		// If no help text, do nothing
282
-		if (!$help) {
283
-			return false;
284
-		}
285
-
286
-		$this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes);
287
-	}
288
-
289
-	/**
290
-	 * Add an block help
291
-	 *
292
-	 * @param  string $help       The help text
293
-	 * @param  array  $attributes Facultative attributes
294
-	 */
295
-	public function blockHelp($help, $attributes = array())
296
-	{
297
-		// Reserved method
298
-		if ($this->app['former.framework']->isnt('TwitterBootstrap') &&
299
-		    $this->app['former.framework']->isnt('TwitterBootstrap3') &&
300
-		    $this->app['former.framework']->isnt('TwitterBootstrap4')
301
-		) {
302
-			throw new BadMethodCallException('This method is only available on the Bootstrap framework');
303
-		}
304
-
305
-		// If no help text, do nothing
306
-		if (!$help) {
307
-			return false;
308
-		}
309
-
310
-		$this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes);
311
-	}
312
-
313
-	////////////////////////////////////////////////////////////////////
314
-	///////////////////////// PREPEND/APPEND METHODS ///////////////////
315
-	////////////////////////////////////////////////////////////////////
316
-
317
-	/**
318
-	 * Prepend elements to the field
319
-	 */
320
-	public function prepend()
321
-	{
322
-		$this->placeAround(func_get_args(), 'prepend');
323
-	}
324
-
325
-	/**
326
-	 * Append elements to the field
327
-	 */
328
-	public function append()
329
-	{
330
-		$this->placeAround(func_get_args(), 'append');
331
-	}
332
-
333
-	/**
334
-	 * Prepends an icon to a field
335
-	 *
336
-	 * @param string $icon       The icon to prepend
337
-	 * @param array  $attributes Its attributes
338
-	 */
339
-	public function prependIcon($icon, $attributes = array(), $iconSettings = array())
340
-	{
341
-		$icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
342
-
343
-		$this->prepend($icon);
344
-	}
345
-
346
-	/**
347
-	 * Append an icon to a field
348
-	 *
349
-	 * @param string $icon       The icon to prepend
350
-	 * @param array  $attributes Its attributes
351
-	 */
352
-	public function appendIcon($icon, $attributes = array(), $iconSettings = array())
353
-	{
354
-		$icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
355
-
356
-		$this->append($icon);
357
-	}
358
-
359
-	////////////////////////////////////////////////////////////////////
360
-	//////////////////////////////// HELPERS ///////////////////////////
361
-	////////////////////////////////////////////////////////////////////
362
-
363
-	/**
364
-	 * Get the errors for the group
365
-	 *
366
-	 * @return string
367
-	 */
368
-	public function getErrors()
369
-	{
370
-		$errors = '';
371
-
372
-		if (!self::$opened) {
373
-
374
-			// for non-custom groups, normal error handling applies
375
-			$errors = $this->app['former']->getErrors();
376
-		} elseif (!empty($this->validations)) {
377
-
378
-			// error handling only when validations specified for custom groups
379
-			foreach ($this->validations as $validation) {
380
-				$errors .= $this->app['former']->getErrors($validation);
381
-			}
382
-		}
383
-
384
-		return $errors;
385
-	}
386
-
387
-	/**
388
-	 * Wraps content in a group
389
-	 *
390
-	 * @param string $contents The content
391
-	 * @param string $label    The label to add
392
-	 *
393
-	 * @return string A group
394
-	 */
395
-	public function wrap($contents, $label = null)
396
-	{
397
-		$group = $this->open();
398
-		$group .= $label;
399
-		$group .= $this->app['former.framework']->wrapField($contents);
400
-		$group .= $this->close();
401
-
402
-		return $group;
403
-	}
404
-
405
-	/**
406
-	 * Prints out the current label
407
-	 *
408
-	 * @param  string $field The field to create a label for
409
-	 *
410
-	 * @return string        A <label> tag
411
-	 */
412
-	protected function getLabel($field = null)
413
-	{
414
-		// Don't create a label if none exist
415
-		if (!$field or !$this->label) {
416
-			return null;
417
-		}
418
-
419
-		// Wrap label in framework classes
420
-		$this->label->addClass($this->app['former.framework']->getLabelClasses());
421
-		$this->label = $this->app['former.framework']->createLabelOf($field, $this->label);
422
-		$this->label = $this->app['former.framework']->wrapLabel($this->label);
423
-
424
-		return $this->label;
425
-	}
426
-
427
-	/**
428
-	 * Prints out the current help
429
-	 *
430
-	 * @return string A .help-block or .help-inline
431
-	 */
432
-	protected function getHelp()
433
-	{
434
-		$inline = Arr::get($this->help, 'inline');
435
-		$block  = Arr::get($this->help, 'block');
436
-
437
-		// Replace help text with error if any found
438
-		$errors = $this->app['former']->getErrors();
439
-		if ($errors and $this->app['former']->getOption('error_messages')) {
440
-			$inline = $this->app['former.framework']->createValidationError($errors);
441
-		}
442
-
443
-		return join(null, array($inline, $block));
444
-	}
445
-
446
-	/**
447
-	 * Format the field with prepended/appended elements
448
-	 *
449
-	 * @param  Field $field The field to format
450
-	 *
451
-	 * @return string        Field plus supplementary elements
452
-	 */
453
-	protected function prependAppend($field)
454
-	{
455
-		if (!$this->prepend and !$this->append) {
456
-			return $field->render();
457
-		}
458
-
459
-		return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append);
460
-	}
461
-
462
-	/**
463
-	 * Place elements around the field
464
-	 *
465
-	 * @param  array  $items An array of items to place
466
-	 * @param  string $place Where they should end up (prepend|append)
467
-	 */
468
-	protected function placeAround($items, $place)
469
-	{
470
-		// Iterate over the items and place them where they should
471
-		foreach ((array) $items as $item) {
472
-			$item             = $this->app['former.framework']->placeAround($item);
473
-			$this->{$place}[] = $item;
474
-		}
475
-	}
16
+    /**
17
+     * The Container
18
+     *
19
+     * @var Container
20
+     */
21
+    protected $app;
22
+
23
+    /**
24
+     * The current state of the group
25
+     *
26
+     * @var string
27
+     */
28
+    protected $state = null;
29
+
30
+    /**
31
+     * Whether the field should be displayed raw or not
32
+     *
33
+     * @var boolean
34
+     */
35
+    protected $raw = false;
36
+
37
+    /**
38
+     * The group label
39
+     *
40
+     * @var Element
41
+     */
42
+    protected $label;
43
+
44
+    /**
45
+     * The group help
46
+     *
47
+     * @var array
48
+     */
49
+    protected $help = array();
50
+
51
+    /**
52
+     * An array of elements to preprend the field
53
+     *
54
+     * @var array
55
+     */
56
+    protected $prepend = array();
57
+
58
+    /**
59
+     * An array of elements to append the field
60
+     *
61
+     * @var array
62
+     */
63
+    protected $append = array();
64
+
65
+    /**
66
+     * The field validations to be checked for errors
67
+     *
68
+     * @var array
69
+     */
70
+    protected $validations = array();
71
+
72
+    /**
73
+     * The group's element
74
+     *
75
+     * @var string
76
+     */
77
+    protected $element = 'div';
78
+
79
+    /**
80
+     * Whether a custom group is opened or not
81
+     *
82
+     * @var boolean
83
+     */
84
+    public static $opened = false;
85
+
86
+    /**
87
+     * The custom group that is open
88
+     *
89
+     * @var Former\Form\Group
90
+     */
91
+    public static $openGroup = null;
92
+
93
+    ////////////////////////////////////////////////////////////////////
94
+    /////////////////////////// CORE METHODS ///////////////////////////
95
+    ////////////////////////////////////////////////////////////////////
96
+
97
+    /**
98
+     * Creates a group
99
+     *
100
+     * @param string $label Its label
101
+     */
102
+    public function __construct(Container $app, $label, $validations = null)
103
+    {
104
+        // Get special classes
105
+        $this->app = $app;
106
+        $this->addClass($this->app['former.framework']->getGroupClasses());
107
+
108
+        // Invisible if Nude
109
+        if ($this->app['former.framework']->is('Nude')) {
110
+            $this->element = '';
111
+        }
112
+
113
+        // Set group label
114
+        if ($label) {
115
+            $this->setLabel($label);
116
+        }
117
+
118
+        // Set validations used to override groups own conclusions
119
+        $this->validations = (array) $validations;
120
+    }
121
+
122
+    /**
123
+     * Prints out the opening of the Control Group
124
+     *
125
+     * @return string A control group opening tag
126
+     */
127
+    public function __toString()
128
+    {
129
+        return $this->open().$this->getFormattedLabel();
130
+    }
131
+
132
+    /**
133
+     * Opens a group
134
+     *
135
+     * @return string Opening tag
136
+     */
137
+    public function open()
138
+    {
139
+        if ($this->getErrors()) {
140
+            $this->state($this->app['former.framework']->errorState());
141
+        }
142
+
143
+        // Retrieve state and append it to classes
144
+        if ($this->state) {
145
+            $this->addClass($this->state);
146
+        }
147
+
148
+        // Required state
149
+        if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) {
150
+            $this->addClass($this->app['former']->getOption('required_class'));
151
+        }
152
+
153
+        return parent::open();
154
+    }
155
+
156
+    /**
157
+     * Set the contents of the current group
158
+     *
159
+     * @param string $contents The group contents
160
+     *
161
+     * @return string A group
162
+     */
163
+    public function contents($contents)
164
+    {
165
+        return $this->wrap($contents, $this->getFormattedLabel());
166
+    }
167
+
168
+    /**
169
+     * Wrap a Field with the current group
170
+     *
171
+     * @param  \Former\Traits\Field $field A Field instance
172
+     *
173
+     * @return string        A group
174
+     */
175
+    public function wrapField($field)
176
+    {
177
+        $label = $this->getLabel($field);
178
+        $field = $this->prependAppend($field);
179
+        $field .= $this->getHelp();
180
+
181
+        return $this->wrap($field, $label);
182
+    }
183
+
184
+    ////////////////////////////////////////////////////////////////////
185
+    //////////////////////////// FIELD METHODS /////////////////////////
186
+    ////////////////////////////////////////////////////////////////////
187
+
188
+    /**
189
+     * Set the state of the group
190
+     *
191
+     * @param  string $state A Bootstrap state class
192
+     */
193
+    public function state($state)
194
+    {
195
+        // Filter state
196
+        $state = $this->app['former.framework']->filterState($state);
197
+
198
+        $this->state = $state;
199
+    }
200
+
201
+    /**
202
+     * Set a class on the Group
203
+     *
204
+     * @param string $class The class to add
205
+     */
206
+    public function addGroupClass($class)
207
+    {
208
+        $this->addClass($class);
209
+    }
210
+
211
+    /**
212
+     * Adds a label to the group
213
+     *
214
+     * @param  string $label A label
215
+     */
216
+    public function setLabel($label)
217
+    {
218
+        if (!$label instanceof Element) {
219
+            $label = Helpers::translate($label);
220
+            $label = Element::create('label', $label)->for($label);
221
+        }
222
+
223
+        $this->label = $label;
224
+    }
225
+
226
+    /**
227
+     * Get the formatted group label
228
+     *
229
+     * @return string|null
230
+     */
231
+    public function getFormattedLabel()
232
+    {
233
+        if (!$this->label) {
234
+            return false;
235
+        }
236
+
237
+        return $this->label->addClass($this->app['former.framework']->getLabelClasses());
238
+    }
239
+
240
+    /**
241
+     * Disables the control group for the current field
242
+     */
243
+    public function raw()
244
+    {
245
+        $this->raw = true;
246
+    }
247
+
248
+    /**
249
+     * Check if the current group is to be displayed or not
250
+     *
251
+     * @return boolean
252
+     */
253
+    public function isRaw()
254
+    {
255
+        return (bool) $this->raw;
256
+    }
257
+
258
+    ////////////////////////////////////////////////////////////////////
259
+    ///////////////////////////// HELP BLOCKS //////////////////////////
260
+    ////////////////////////////////////////////////////////////////////
261
+
262
+    /**
263
+     * Alias for inlineHelp
264
+     *
265
+     * @param  string $help       The help text
266
+     * @param  array  $attributes Facultative attributes
267
+     */
268
+    public function help($help, $attributes = array())
269
+    {
270
+        return $this->inlineHelp($help, $attributes);
271
+    }
272
+
273
+    /**
274
+     * Add an inline help
275
+     *
276
+     * @param  string $help       The help text
277
+     * @param  array  $attributes Facultative attributes
278
+     */
279
+    public function inlineHelp($help, $attributes = array())
280
+    {
281
+        // If no help text, do nothing
282
+        if (!$help) {
283
+            return false;
284
+        }
285
+
286
+        $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes);
287
+    }
288
+
289
+    /**
290
+     * Add an block help
291
+     *
292
+     * @param  string $help       The help text
293
+     * @param  array  $attributes Facultative attributes
294
+     */
295
+    public function blockHelp($help, $attributes = array())
296
+    {
297
+        // Reserved method
298
+        if ($this->app['former.framework']->isnt('TwitterBootstrap') &&
299
+            $this->app['former.framework']->isnt('TwitterBootstrap3') &&
300
+            $this->app['former.framework']->isnt('TwitterBootstrap4')
301
+        ) {
302
+            throw new BadMethodCallException('This method is only available on the Bootstrap framework');
303
+        }
304
+
305
+        // If no help text, do nothing
306
+        if (!$help) {
307
+            return false;
308
+        }
309
+
310
+        $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes);
311
+    }
312
+
313
+    ////////////////////////////////////////////////////////////////////
314
+    ///////////////////////// PREPEND/APPEND METHODS ///////////////////
315
+    ////////////////////////////////////////////////////////////////////
316
+
317
+    /**
318
+     * Prepend elements to the field
319
+     */
320
+    public function prepend()
321
+    {
322
+        $this->placeAround(func_get_args(), 'prepend');
323
+    }
324
+
325
+    /**
326
+     * Append elements to the field
327
+     */
328
+    public function append()
329
+    {
330
+        $this->placeAround(func_get_args(), 'append');
331
+    }
332
+
333
+    /**
334
+     * Prepends an icon to a field
335
+     *
336
+     * @param string $icon       The icon to prepend
337
+     * @param array  $attributes Its attributes
338
+     */
339
+    public function prependIcon($icon, $attributes = array(), $iconSettings = array())
340
+    {
341
+        $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
342
+
343
+        $this->prepend($icon);
344
+    }
345
+
346
+    /**
347
+     * Append an icon to a field
348
+     *
349
+     * @param string $icon       The icon to prepend
350
+     * @param array  $attributes Its attributes
351
+     */
352
+    public function appendIcon($icon, $attributes = array(), $iconSettings = array())
353
+    {
354
+        $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
355
+
356
+        $this->append($icon);
357
+    }
358
+
359
+    ////////////////////////////////////////////////////////////////////
360
+    //////////////////////////////// HELPERS ///////////////////////////
361
+    ////////////////////////////////////////////////////////////////////
362
+
363
+    /**
364
+     * Get the errors for the group
365
+     *
366
+     * @return string
367
+     */
368
+    public function getErrors()
369
+    {
370
+        $errors = '';
371
+
372
+        if (!self::$opened) {
373
+
374
+            // for non-custom groups, normal error handling applies
375
+            $errors = $this->app['former']->getErrors();
376
+        } elseif (!empty($this->validations)) {
377
+
378
+            // error handling only when validations specified for custom groups
379
+            foreach ($this->validations as $validation) {
380
+                $errors .= $this->app['former']->getErrors($validation);
381
+            }
382
+        }
383
+
384
+        return $errors;
385
+    }
386
+
387
+    /**
388
+     * Wraps content in a group
389
+     *
390
+     * @param string $contents The content
391
+     * @param string $label    The label to add
392
+     *
393
+     * @return string A group
394
+     */
395
+    public function wrap($contents, $label = null)
396
+    {
397
+        $group = $this->open();
398
+        $group .= $label;
399
+        $group .= $this->app['former.framework']->wrapField($contents);
400
+        $group .= $this->close();
401
+
402
+        return $group;
403
+    }
404
+
405
+    /**
406
+     * Prints out the current label
407
+     *
408
+     * @param  string $field The field to create a label for
409
+     *
410
+     * @return string        A <label> tag
411
+     */
412
+    protected function getLabel($field = null)
413
+    {
414
+        // Don't create a label if none exist
415
+        if (!$field or !$this->label) {
416
+            return null;
417
+        }
418
+
419
+        // Wrap label in framework classes
420
+        $this->label->addClass($this->app['former.framework']->getLabelClasses());
421
+        $this->label = $this->app['former.framework']->createLabelOf($field, $this->label);
422
+        $this->label = $this->app['former.framework']->wrapLabel($this->label);
423
+
424
+        return $this->label;
425
+    }
426
+
427
+    /**
428
+     * Prints out the current help
429
+     *
430
+     * @return string A .help-block or .help-inline
431
+     */
432
+    protected function getHelp()
433
+    {
434
+        $inline = Arr::get($this->help, 'inline');
435
+        $block  = Arr::get($this->help, 'block');
436
+
437
+        // Replace help text with error if any found
438
+        $errors = $this->app['former']->getErrors();
439
+        if ($errors and $this->app['former']->getOption('error_messages')) {
440
+            $inline = $this->app['former.framework']->createValidationError($errors);
441
+        }
442
+
443
+        return join(null, array($inline, $block));
444
+    }
445
+
446
+    /**
447
+     * Format the field with prepended/appended elements
448
+     *
449
+     * @param  Field $field The field to format
450
+     *
451
+     * @return string        Field plus supplementary elements
452
+     */
453
+    protected function prependAppend($field)
454
+    {
455
+        if (!$this->prepend and !$this->append) {
456
+            return $field->render();
457
+        }
458
+
459
+        return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append);
460
+    }
461
+
462
+    /**
463
+     * Place elements around the field
464
+     *
465
+     * @param  array  $items An array of items to place
466
+     * @param  string $place Where they should end up (prepend|append)
467
+     */
468
+    protected function placeAround($items, $place)
469
+    {
470
+        // Iterate over the items and place them where they should
471
+        foreach ((array) $items as $item) {
472
+            $item             = $this->app['former.framework']->placeAround($item);
473
+            $this->{$place}[] = $item;
474
+        }
475
+    }
476 476
 }
Please login to merge, or discard this patch.
src/Former/FormerServiceProvider.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -156,7 +156,7 @@
 block discarded – undo
156 156
 	/**
157 157
 	 * Get all of the configuration files for the application.
158 158
 	 *
159
-	 * @param  $app
159
+	 * @param  Container $app
160 160
 	 * @return array
161 161
 	 */
162 162
 	protected function getConfigurationFiles($app)
Please login to merge, or discard this patch.
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/Nude.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -126,7 +126,7 @@
 block discarded – undo
126 126
 	 *
127 127
 	 * @param Field $field
128 128
 	 *
129
-	 * @return Element
129
+	 * @return Input
130 130
 	 */
131 131
 	public function createPlainTextField(Field $field)
132 132
 	{
Please login to merge, or discard this patch.
Indentation   +178 added lines, -178 removed lines patch added patch discarded remove patch
@@ -14,182 +14,182 @@
 block discarded – undo
14 14
 class Nude extends Framework implements FrameworkInterface
15 15
 {
16 16
 
17
-	/**
18
-	 * The field states available
19
-	 *
20
-	 * @var array
21
-	 */
22
-	protected $states = array(
23
-		'error',
24
-	);
25
-
26
-	/**
27
-	 * Create a new Nude instance
28
-	 *
29
-	 * @param Container $app
30
-	 */
31
-	public function __construct(Container $app)
32
-	{
33
-		$this->app = $app;
34
-		$this->setFrameworkDefaults();
35
-	}
36
-
37
-	////////////////////////////////////////////////////////////////////
38
-	/////////////////////////// FILTER ARRAYS //////////////////////////
39
-	////////////////////////////////////////////////////////////////////
40
-
41
-	public function filterButtonClasses($classes)
42
-	{
43
-		return $classes;
44
-	}
45
-
46
-	public function filterFieldClasses($classes)
47
-	{
48
-		return $classes;
49
-	}
50
-
51
-	////////////////////////////////////////////////////////////////////
52
-	///////////////////////////// ADD CLASSES //////////////////////////
53
-	////////////////////////////////////////////////////////////////////
54
-
55
-	public function getFieldClasses(Field $field, $classes = array())
56
-	{
57
-		$classes = $this->filterFieldClasses($classes);
58
-
59
-		// If we found any class, add them
60
-		if ($classes) {
61
-			$field->class(implode(' ', $classes));
62
-		}
63
-
64
-		return $field;
65
-	}
66
-
67
-	public function getGroupClasses()
68
-	{
69
-		return null;
70
-	}
71
-
72
-	public function getLabelClasses()
73
-	{
74
-		return null;
75
-	}
76
-
77
-	public function getUneditableClasses()
78
-	{
79
-		return null;
80
-	}
81
-
82
-	public function getPlainTextClasses()
83
-	{
84
-		return null;
85
-	}
86
-
87
-	public function getFormClasses($type)
88
-	{
89
-		return null;
90
-	}
91
-
92
-	public function getActionClasses()
93
-	{
94
-		return null;
95
-	}
96
-
97
-	////////////////////////////////////////////////////////////////////
98
-	//////////////////////////// RENDER BLOCKS /////////////////////////
99
-	////////////////////////////////////////////////////////////////////
100
-
101
-	/**
102
-	 * Create an help text
103
-	 */
104
-	public function createHelp($text, $attributes = array())
105
-	{
106
-		return Element::create('span', $text, $attributes)->addClass('help');
107
-	}
108
-
109
-	/**
110
-	 * Render a disabled field
111
-	 *
112
-	 * @param Field $field
113
-	 *
114
-	 * @return Input
115
-	 */
116
-	public function createDisabledField(Field $field)
117
-	{
118
-		$field->disabled();
119
-
120
-		return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
121
-	}
122
-
123
-	/**
124
-	 * Render a plain text field
125
-	 * Which fallback to a disabled field
126
-	 *
127
-	 * @param Field $field
128
-	 *
129
-	 * @return Element
130
-	 */
131
-	public function createPlainTextField(Field $field)
132
-	{
133
-		return $this->createDisabledField($field);
134
-	}
135
-
136
-	////////////////////////////////////////////////////////////////////
137
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
138
-	////////////////////////////////////////////////////////////////////
139
-
140
-	/**
141
-	 * Wrap an item to be prepended or appended to the current field
142
-	 *
143
-	 * @param  string $item
144
-	 *
145
-	 * @return Element A wrapped item
146
-	 */
147
-	public function placeAround($item)
148
-	{
149
-		return Element::create('span', $item);
150
-	}
151
-
152
-	/**
153
-	 * Wrap a field with prepended and appended items
154
-	 *
155
-	 * @param  Field $field
156
-	 * @param  array $prepend
157
-	 * @param  array $append
158
-	 *
159
-	 * @return string A field concatented with prepended and/or appended items
160
-	 */
161
-	public function prependAppend($field, $prepend, $append)
162
-	{
163
-		$return = '<div>';
164
-		$return .= join(null, $prepend);
165
-		$return .= $field->render();
166
-		$return .= join(null, $append);
167
-		$return .= '</div>';
168
-
169
-		return $return;
170
-	}
171
-
172
-	/**
173
-	 * Wraps all field contents with potential additional tags.
174
-	 *
175
-	 * @param  Field $field
176
-	 *
177
-	 * @return Field A wrapped field
178
-	 */
179
-	public function wrapField($field)
180
-	{
181
-		return $field;
182
-	}
183
-
184
-	/**
185
-	 * Wrap actions block with potential additional tags
186
-	 *
187
-	 * @param  Actions $actions
188
-	 *
189
-	 * @return string A wrapped actions block
190
-	 */
191
-	public function wrapActions($actions)
192
-	{
193
-		return $actions;
194
-	}
17
+    /**
18
+     * The field states available
19
+     *
20
+     * @var array
21
+     */
22
+    protected $states = array(
23
+        'error',
24
+    );
25
+
26
+    /**
27
+     * Create a new Nude instance
28
+     *
29
+     * @param Container $app
30
+     */
31
+    public function __construct(Container $app)
32
+    {
33
+        $this->app = $app;
34
+        $this->setFrameworkDefaults();
35
+    }
36
+
37
+    ////////////////////////////////////////////////////////////////////
38
+    /////////////////////////// FILTER ARRAYS //////////////////////////
39
+    ////////////////////////////////////////////////////////////////////
40
+
41
+    public function filterButtonClasses($classes)
42
+    {
43
+        return $classes;
44
+    }
45
+
46
+    public function filterFieldClasses($classes)
47
+    {
48
+        return $classes;
49
+    }
50
+
51
+    ////////////////////////////////////////////////////////////////////
52
+    ///////////////////////////// ADD CLASSES //////////////////////////
53
+    ////////////////////////////////////////////////////////////////////
54
+
55
+    public function getFieldClasses(Field $field, $classes = array())
56
+    {
57
+        $classes = $this->filterFieldClasses($classes);
58
+
59
+        // If we found any class, add them
60
+        if ($classes) {
61
+            $field->class(implode(' ', $classes));
62
+        }
63
+
64
+        return $field;
65
+    }
66
+
67
+    public function getGroupClasses()
68
+    {
69
+        return null;
70
+    }
71
+
72
+    public function getLabelClasses()
73
+    {
74
+        return null;
75
+    }
76
+
77
+    public function getUneditableClasses()
78
+    {
79
+        return null;
80
+    }
81
+
82
+    public function getPlainTextClasses()
83
+    {
84
+        return null;
85
+    }
86
+
87
+    public function getFormClasses($type)
88
+    {
89
+        return null;
90
+    }
91
+
92
+    public function getActionClasses()
93
+    {
94
+        return null;
95
+    }
96
+
97
+    ////////////////////////////////////////////////////////////////////
98
+    //////////////////////////// RENDER BLOCKS /////////////////////////
99
+    ////////////////////////////////////////////////////////////////////
100
+
101
+    /**
102
+     * Create an help text
103
+     */
104
+    public function createHelp($text, $attributes = array())
105
+    {
106
+        return Element::create('span', $text, $attributes)->addClass('help');
107
+    }
108
+
109
+    /**
110
+     * Render a disabled field
111
+     *
112
+     * @param Field $field
113
+     *
114
+     * @return Input
115
+     */
116
+    public function createDisabledField(Field $field)
117
+    {
118
+        $field->disabled();
119
+
120
+        return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes());
121
+    }
122
+
123
+    /**
124
+     * Render a plain text field
125
+     * Which fallback to a disabled field
126
+     *
127
+     * @param Field $field
128
+     *
129
+     * @return Element
130
+     */
131
+    public function createPlainTextField(Field $field)
132
+    {
133
+        return $this->createDisabledField($field);
134
+    }
135
+
136
+    ////////////////////////////////////////////////////////////////////
137
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
138
+    ////////////////////////////////////////////////////////////////////
139
+
140
+    /**
141
+     * Wrap an item to be prepended or appended to the current field
142
+     *
143
+     * @param  string $item
144
+     *
145
+     * @return Element A wrapped item
146
+     */
147
+    public function placeAround($item)
148
+    {
149
+        return Element::create('span', $item);
150
+    }
151
+
152
+    /**
153
+     * Wrap a field with prepended and appended items
154
+     *
155
+     * @param  Field $field
156
+     * @param  array $prepend
157
+     * @param  array $append
158
+     *
159
+     * @return string A field concatented with prepended and/or appended items
160
+     */
161
+    public function prependAppend($field, $prepend, $append)
162
+    {
163
+        $return = '<div>';
164
+        $return .= join(null, $prepend);
165
+        $return .= $field->render();
166
+        $return .= join(null, $append);
167
+        $return .= '</div>';
168
+
169
+        return $return;
170
+    }
171
+
172
+    /**
173
+     * Wraps all field contents with potential additional tags.
174
+     *
175
+     * @param  Field $field
176
+     *
177
+     * @return Field A wrapped field
178
+     */
179
+    public function wrapField($field)
180
+    {
181
+        return $field;
182
+    }
183
+
184
+    /**
185
+     * Wrap actions block with potential additional tags
186
+     *
187
+     * @param  Actions $actions
188
+     *
189
+     * @return string A wrapped actions block
190
+     */
191
+    public function wrapActions($actions)
192
+    {
193
+        return $actions;
194
+    }
195 195
 }
Please login to merge, or discard this patch.
src/Former/LiveValidation.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -267,7 +267,7 @@
 block discarded – undo
267 267
 	/**
268 268
 	 * Transform extensions and mime groups into a list of mime types
269 269
 	 *
270
-	 * @param  array $mimes An array of mimes
270
+	 * @param  string[] $mimes An array of mimes
271 271
 	 *
272 272
 	 * @return string A concatenated list of mimes
273 273
 	 */
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.
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/Helpers.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
 		//Convert parametrs of old format to new format
128 128
 		if (!is_callable($text)) {
129 129
 			$optionTextValue = $text;
130
-			$text = function ($model) use($optionTextValue) {
130
+			$text = function($model) use($optionTextValue) {
131 131
 				if ($optionTextValue and isset($model->$optionTextValue)) {
132 132
 					return $model->$optionTextValue;
133 133
 				} elseif (method_exists($model, '__toString')) {
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
 					$optionAttributeValue = $modelAttributeName($model);
173 173
 				} elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
174 174
 					$optionAttributeValue = $model->$modelAttributeName;
175
-				} elseif($optionAttributeName === 'value') {
175
+				} elseif ($optionAttributeName === 'value') {
176 176
 					//For backward compatibility
177 177
 					if (method_exists($model, 'getKey')) {
178 178
 						$optionAttributeValue = $model->getKey();
Please login to merge, or discard this patch.
Indentation   +187 added lines, -187 removed lines patch added patch discarded remove patch
@@ -9,191 +9,191 @@
 block discarded – undo
9 9
  */
10 10
 class Helpers
11 11
 {
12
-	/**
13
-	 * The IoC Container
14
-	 *
15
-	 * @var Container
16
-	 */
17
-	protected static $app;
18
-
19
-	/**
20
-	 * Bind a Container to the Helpers class
21
-	 *
22
-	 * @param Container $app
23
-	 */
24
-	public static function setApp(Container $app)
25
-	{
26
-		static::$app = $app;
27
-	}
28
-
29
-	/**
30
-	 * Encodes HTML
31
-	 *
32
-	 * @param string $value The string to encode
33
-	 *
34
-	 * @return string
35
-	 */
36
-	public static function encode($value)
37
-	{
38
-		return htmlentities($value, ENT_QUOTES, 'UTF-8', true);
39
-	}
40
-
41
-	////////////////////////////////////////////////////////////////////
42
-	///////////////////////// LOCALIZATION HELPERS /////////////////////
43
-	////////////////////////////////////////////////////////////////////
44
-
45
-	/**
46
-	 * Translates a string by trying several fallbacks
47
-	 *
48
-	 * @param  string $key      The key to translate
49
-	 * @param  string $fallback The ultimate fallback
50
-	 *
51
-	 * @return string           A translated string
52
-	 */
53
-	public static function translate($key, $fallback = null)
54
-	{
55
-		// If nothing was given, return nothing, bitch
56
-		if (!$key) {
57
-			return null;
58
-		}
59
-
60
-		// If no fallback, use the key
61
-		if (!$fallback) {
62
-			$fallback = $key;
63
-		}
64
-
65
-		// Assure we don't already have a Lang object
66
-		if (is_object($key) and method_exists($key, 'get')) {
67
-			return $key->get();
68
-		}
69
-
70
-		$translation   = null;
71
-		$translateFrom = static::$app['former']->getOption('translate_from');
72
-		if (substr($translateFrom, -1) !== '/') {
73
-			$translateFrom .= '.';
74
-		}
75
-		$translateFrom .= $key;
76
-
77
-		// Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']]
78
-		if (strpos($translateFrom, ']') !== false) {
79
-			$translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom);
80
-		}
81
-
82
-		// Search for the key itself, if it is valid
83
-		$validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1;
84
-		if ($validKey && static::$app['translator']->has($key)) {
85
-			$translation = static::$app['translator']->get($key);
86
-		} elseif (static::$app['translator']->has($translateFrom)) {
87
-			$translation = static::$app['translator']->get($translateFrom);
88
-		}
89
-
90
-		// Replace by fallback if invalid
91
-		if (!$translation or is_array($translation)) {
92
-			$translation = $fallback;
93
-		}
94
-
95
-		// Capitalize
96
-		$capitalize = static::$app['former']->getOption('capitalize_translations');
97
-
98
-		return $capitalize ? ucfirst($translation) : $translation;
99
-	}
100
-
101
-	////////////////////////////////////////////////////////////////////
102
-	////////////////////////// DATABASE HELPERS ////////////////////////
103
-	////////////////////////////////////////////////////////////////////
104
-
105
-	/**
106
-	 * Transforms an array of models into an associative array
107
-	 *
108
-	 * @param  array|object    $query      The array of results
109
-	 * @param  string|function $text       The value to use as text
110
-	 * @param  string|array    $attributes The data to use as attributes
111
-	 *
112
-	 * @return array               A data array
113
-	 */
114
-	public static function queryToArray($query, $text = null, $attributes = null)
115
-	{
116
-		// Automatically fetch Lang objects for people who store translated options lists
117
-		// Same of unfetched queries
118
-		if (!$query instanceof Collection) {
119
-			if (method_exists($query, 'get')) {
120
-				$query = $query->get();
121
-			}
122
-			if (!is_array($query)) {
123
-				$query = (array) $query;
124
-			}
125
-		}
126
-
127
-		//Convert parametrs of old format to new format
128
-		if (!is_callable($text)) {
129
-			$optionTextValue = $text;
130
-			$text = function ($model) use($optionTextValue) {
131
-				if ($optionTextValue and isset($model->$optionTextValue)) {
132
-					return $model->$optionTextValue;
133
-				} elseif (method_exists($model, '__toString')) {
134
-					return  $model->__toString();
135
-				} else {
136
-					return null;
137
-				}
138
-			};
139
-		}
140
-
141
-		if (!is_array($attributes)) {
142
-			if (is_string($attributes)) {
143
-				$attributes = ['value' => $attributes];
144
-			} else {
145
-				$attributes = ['value' => null];
146
-			}
147
-		}
148
-
149
-		if (!isset($attributes['value'])) {
150
-			$attributes['value'] = null;
151
-		}
152
-		//-------------------------------------------------
153
-
154
-		// Populates the new options
155
-		foreach ($query as $model) {
156
-			// If it's an array, convert to object
157
-			if (is_array($model)) {
158
-				$model = (object) $model;
159
-			}
160
-
161
-			// Calculate option text
162
-			$optionText = $text($model);
163
-
164
-			// Skip if no text value found
165
-			if (!$optionText) {
166
-				continue;
167
-			}
168
-
169
-			//Collect option attributes
170
-			foreach ($attributes as $optionAttributeName => $modelAttributeName) {
171
-				if (is_callable($modelAttributeName)) {
172
-					$optionAttributeValue = $modelAttributeName($model);
173
-				} elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
174
-					$optionAttributeValue = $model->$modelAttributeName;
175
-				} elseif($optionAttributeName === 'value') {
176
-					//For backward compatibility
177
-					if (method_exists($model, 'getKey')) {
178
-						$optionAttributeValue = $model->getKey();
179
-					} elseif (isset($model->id)) {
180
-						$optionAttributeValue = $model->id;
181
-					} else {
182
-						$optionAttributeValue = $optionText;
183
-					}
184
-				} else {
185
-					$optionAttributeValue = '';
186
-				}
187
-
188
-				//For backward compatibility
189
-				if (count($attributes) === 1) {
190
-					$array[$optionAttributeValue] = (string) $optionText;
191
-				} else {
192
-					$array[$optionText][$optionAttributeName] = (string) $optionAttributeValue;
193
-				}
194
-			}
195
-		}
196
-
197
-		return !empty($array) ? $array : $query;
198
-	}
12
+    /**
13
+     * The IoC Container
14
+     *
15
+     * @var Container
16
+     */
17
+    protected static $app;
18
+
19
+    /**
20
+     * Bind a Container to the Helpers class
21
+     *
22
+     * @param Container $app
23
+     */
24
+    public static function setApp(Container $app)
25
+    {
26
+        static::$app = $app;
27
+    }
28
+
29
+    /**
30
+     * Encodes HTML
31
+     *
32
+     * @param string $value The string to encode
33
+     *
34
+     * @return string
35
+     */
36
+    public static function encode($value)
37
+    {
38
+        return htmlentities($value, ENT_QUOTES, 'UTF-8', true);
39
+    }
40
+
41
+    ////////////////////////////////////////////////////////////////////
42
+    ///////////////////////// LOCALIZATION HELPERS /////////////////////
43
+    ////////////////////////////////////////////////////////////////////
44
+
45
+    /**
46
+     * Translates a string by trying several fallbacks
47
+     *
48
+     * @param  string $key      The key to translate
49
+     * @param  string $fallback The ultimate fallback
50
+     *
51
+     * @return string           A translated string
52
+     */
53
+    public static function translate($key, $fallback = null)
54
+    {
55
+        // If nothing was given, return nothing, bitch
56
+        if (!$key) {
57
+            return null;
58
+        }
59
+
60
+        // If no fallback, use the key
61
+        if (!$fallback) {
62
+            $fallback = $key;
63
+        }
64
+
65
+        // Assure we don't already have a Lang object
66
+        if (is_object($key) and method_exists($key, 'get')) {
67
+            return $key->get();
68
+        }
69
+
70
+        $translation   = null;
71
+        $translateFrom = static::$app['former']->getOption('translate_from');
72
+        if (substr($translateFrom, -1) !== '/') {
73
+            $translateFrom .= '.';
74
+        }
75
+        $translateFrom .= $key;
76
+
77
+        // Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']]
78
+        if (strpos($translateFrom, ']') !== false) {
79
+            $translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom);
80
+        }
81
+
82
+        // Search for the key itself, if it is valid
83
+        $validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1;
84
+        if ($validKey && static::$app['translator']->has($key)) {
85
+            $translation = static::$app['translator']->get($key);
86
+        } elseif (static::$app['translator']->has($translateFrom)) {
87
+            $translation = static::$app['translator']->get($translateFrom);
88
+        }
89
+
90
+        // Replace by fallback if invalid
91
+        if (!$translation or is_array($translation)) {
92
+            $translation = $fallback;
93
+        }
94
+
95
+        // Capitalize
96
+        $capitalize = static::$app['former']->getOption('capitalize_translations');
97
+
98
+        return $capitalize ? ucfirst($translation) : $translation;
99
+    }
100
+
101
+    ////////////////////////////////////////////////////////////////////
102
+    ////////////////////////// DATABASE HELPERS ////////////////////////
103
+    ////////////////////////////////////////////////////////////////////
104
+
105
+    /**
106
+     * Transforms an array of models into an associative array
107
+     *
108
+     * @param  array|object    $query      The array of results
109
+     * @param  string|function $text       The value to use as text
110
+     * @param  string|array    $attributes The data to use as attributes
111
+     *
112
+     * @return array               A data array
113
+     */
114
+    public static function queryToArray($query, $text = null, $attributes = null)
115
+    {
116
+        // Automatically fetch Lang objects for people who store translated options lists
117
+        // Same of unfetched queries
118
+        if (!$query instanceof Collection) {
119
+            if (method_exists($query, 'get')) {
120
+                $query = $query->get();
121
+            }
122
+            if (!is_array($query)) {
123
+                $query = (array) $query;
124
+            }
125
+        }
126
+
127
+        //Convert parametrs of old format to new format
128
+        if (!is_callable($text)) {
129
+            $optionTextValue = $text;
130
+            $text = function ($model) use($optionTextValue) {
131
+                if ($optionTextValue and isset($model->$optionTextValue)) {
132
+                    return $model->$optionTextValue;
133
+                } elseif (method_exists($model, '__toString')) {
134
+                    return  $model->__toString();
135
+                } else {
136
+                    return null;
137
+                }
138
+            };
139
+        }
140
+
141
+        if (!is_array($attributes)) {
142
+            if (is_string($attributes)) {
143
+                $attributes = ['value' => $attributes];
144
+            } else {
145
+                $attributes = ['value' => null];
146
+            }
147
+        }
148
+
149
+        if (!isset($attributes['value'])) {
150
+            $attributes['value'] = null;
151
+        }
152
+        //-------------------------------------------------
153
+
154
+        // Populates the new options
155
+        foreach ($query as $model) {
156
+            // If it's an array, convert to object
157
+            if (is_array($model)) {
158
+                $model = (object) $model;
159
+            }
160
+
161
+            // Calculate option text
162
+            $optionText = $text($model);
163
+
164
+            // Skip if no text value found
165
+            if (!$optionText) {
166
+                continue;
167
+            }
168
+
169
+            //Collect option attributes
170
+            foreach ($attributes as $optionAttributeName => $modelAttributeName) {
171
+                if (is_callable($modelAttributeName)) {
172
+                    $optionAttributeValue = $modelAttributeName($model);
173
+                } elseif ($modelAttributeName and isset($model->$modelAttributeName)) {
174
+                    $optionAttributeValue = $model->$modelAttributeName;
175
+                } elseif($optionAttributeName === 'value') {
176
+                    //For backward compatibility
177
+                    if (method_exists($model, 'getKey')) {
178
+                        $optionAttributeValue = $model->getKey();
179
+                    } elseif (isset($model->id)) {
180
+                        $optionAttributeValue = $model->id;
181
+                    } else {
182
+                        $optionAttributeValue = $optionText;
183
+                    }
184
+                } else {
185
+                    $optionAttributeValue = '';
186
+                }
187
+
188
+                //For backward compatibility
189
+                if (count($attributes) === 1) {
190
+                    $array[$optionAttributeValue] = (string) $optionText;
191
+                } else {
192
+                    $array[$optionText][$optionAttributeName] = (string) $optionAttributeValue;
193
+                }
194
+            }
195
+        }
196
+
197
+        return !empty($array) ? $array : $query;
198
+    }
199 199
 }
Please login to merge, or discard this patch.
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.