Completed
Push — master ( a52f5a...cc2f3c )
by Alex
01:33
created
src/Former/Former.php 1 patch
Indentation   +486 added lines, -486 removed lines patch added patch discarded remove patch
@@ -14,490 +14,490 @@
 block discarded – undo
14 14
  */
15 15
 class Former
16 16
 {
17
-	// Instances
18
-	////////////////////////////////////////////////////////////////////
19
-
20
-
21
-	/**
22
-	 * The current environment
23
-	 *
24
-	 * @var \Illuminate\Container\Container
25
-	 */
26
-	protected $app;
27
-
28
-	/**
29
-	 * The Method Dispatcher
30
-	 *
31
-	 * @var MethodDispatcher
32
-	 */
33
-	protected $dispatch;
34
-
35
-	// Informations
36
-	////////////////////////////////////////////////////////////////////
37
-
38
-	/**
39
-	 * The form's errors
40
-	 *
41
-	 * @var Message
42
-	 */
43
-	protected $errors;
44
-
45
-	/**
46
-	 * An array of rules to use
47
-	 *
48
-	 * @var array
49
-	 */
50
-	protected $rules = array();
51
-
52
-	/**
53
-	 * An array of field macros
54
-	 *
55
-	 * @var array
56
-	 */
57
-	protected $macros = array();
58
-
59
-	/**
60
-	 * The labels created so far
61
-	 *
62
-	 * @var array
63
-	 */
64
-	public $labels = array();
65
-
66
-	/**
67
-	 * The IDs created so far
68
-	 *
69
-	 * @var array
70
-	 */
71
-	public $ids = array();
72
-
73
-	/**
74
-	 * A lookup table where the key is the input name,
75
-	 * and the value is number of times seen. This is
76
-	 * used to calculate unique ids.
77
-	 *
78
-	 * @var array
79
-	 */
80
-	public $names = array();
81
-
82
-	// Namespaces
83
-	////////////////////////////////////////////////////////////////////
84
-
85
-	/**
86
-	 * The namespace of Form elements
87
-	 */
88
-	const FORMSPACE = 'Former\Form\\';
89
-
90
-	/**
91
-	 * The namespace of fields
92
-	 */
93
-	const FIELDSPACE = 'Former\Form\Fields\\';
94
-
95
-	/**
96
-	 * Build a new Former instance
97
-	 *
98
-	 * @param Container        $app
99
-	 * @param MethodDispatcher $dispatcher
100
-	 */
101
-	public function __construct(Container $app, MethodDispatcher $dispatcher)
102
-	{
103
-		$this->app      = $app;
104
-		$this->dispatch = $dispatcher;
105
-	}
106
-
107
-	////////////////////////////////////////////////////////////////////
108
-	//////////////////////////// INTERFACE /////////////////////////////
109
-	////////////////////////////////////////////////////////////////////
110
-
111
-	/**
112
-	 * Acts as a router that redirects methods to all of Former classes
113
-	 *
114
-	 * @param  string $method     The method called
115
-	 * @param  array  $parameters An array of parameters
116
-	 *
117
-	 * @return mixed
118
-	 */
119
-	public function __call($method, $parameters)
120
-	{
121
-		// Dispatch to Form\Elements
122
-		// Explicitly check false since closeGroup() may return an empty string
123
-		if (($element = $this->dispatch->toElements($method, $parameters)) !== false) {
124
-			return $element;
125
-		}
126
-
127
-		// Dispatch to Form\Form
128
-		if ($form = $this->dispatch->toForm($method, $parameters)) {
129
-			$this->app->instance('former.form', $form);
130
-
131
-			return $this->app['former.form'];
132
-		}
133
-
134
-		// Dispatch to Form\Group
135
-		if ($group = $this->dispatch->toGroup($method, $parameters)) {
136
-			return $group;
137
-		}
138
-
139
-		// Dispatch to Form\Actions
140
-		if ($actions = $this->dispatch->toActions($method, $parameters)) {
141
-			return $actions;
142
-		}
143
-
144
-		// Dispatch to macros
145
-		if ($macro = $this->dispatch->toMacros($method, $parameters)) {
146
-			return $macro;
147
-		}
148
-
149
-		// Checking for any supplementary classes
150
-		$modifiers = explode('_', $method);
151
-		$method  = array_pop($modifiers);
152
-
153
-		// Dispatch to the different Form\Fields
154
-		$field     = $this->dispatch->toFields($method, $parameters);
155
-		$field->setModifiers($modifiers);
156
-		$field->addClass('');
157
-
158
-		// Else bind field
159
-		$this->app->instance('former.field', $field);
160
-
161
-		return $this->app['former.field'];
162
-	}
163
-
164
-	////////////////////////////////////////////////////////////////////
165
-	//////////////////////////////// MACROS ////////////////////////////
166
-	////////////////////////////////////////////////////////////////////
167
-
168
-	/**
169
-	 * Register a macro with Former
170
-	 *
171
-	 * @param  string   $name  The name of the macro
172
-	 * @param  Callable $macro The macro itself
173
-	 *
174
-	 * @return mixed
175
-	 */
176
-	public function macro($name, $macro)
177
-	{
178
-		$this->macros[$name] = $macro;
179
-	}
180
-
181
-	/**
182
-	 * Check if a macro exists
183
-	 *
184
-	 * @param  string $name
185
-	 *
186
-	 * @return boolean
187
-	 */
188
-	public function hasMacro($name)
189
-	{
190
-		return isset($this->macros[$name]);
191
-	}
192
-
193
-	/**
194
-	 * Get a registered macro
195
-	 *
196
-	 * @param  string $name
197
-	 *
198
-	 * @return Closure
199
-	 */
200
-	public function getMacro($name)
201
-	{
202
-		return $this->macros[$name];
203
-	}
204
-
205
-	////////////////////////////////////////////////////////////////////
206
-	///////////////////////////// POPULATOR ////////////////////////////
207
-	////////////////////////////////////////////////////////////////////
208
-
209
-	/**
210
-	 * Add values to populate the array
211
-	 *
212
-	 * @param mixed $values Can be an Eloquent object or an array
213
-	 */
214
-	public function populate($values)
215
-	{
216
-		$this->app['former.populator']->replace($values);
217
-	}
218
-
219
-	/**
220
-	 * Set the value of a particular field
221
-	 *
222
-	 * @param string $field The field's name
223
-	 * @param mixed  $value Its new value
224
-	 */
225
-	public function populateField($field, $value)
226
-	{
227
-		$this->app['former.populator']->put($field, $value);
228
-	}
229
-
230
-	/**
231
-	 * Get the value of a field
232
-	 *
233
-	 * @param string $field The field's name
234
-	 * @param null   $fallback
235
-	 *
236
-	 * @return mixed
237
-	 */
238
-	public function getValue($field, $fallback = null)
239
-	{
240
-		return $this->app['former.populator']->get($field, $fallback);
241
-	}
242
-
243
-	/**
244
-	 * Fetch a field value from both the new and old POST array
245
-	 *
246
-	 * @param  string $name     A field name
247
-	 * @param  string $fallback A fallback if nothing was found
248
-	 *
249
-	 * @return string           The results
250
-	 */
251
-	public function getPost($name, $fallback = null)
252
-	{
253
-		$name     = str_replace(array('[', ']'), array('.', ''), $name);
254
-		$name     = trim($name, '.');
255
-		$oldValue = $this->app['request']->old($name, $fallback);
256
-
257
-		return $this->app['request']->input($name, $oldValue, true);
258
-	}
259
-
260
-	////////////////////////////////////////////////////////////////////
261
-	////////////////////////////// TOOLKIT /////////////////////////////
262
-	////////////////////////////////////////////////////////////////////
263
-
264
-	/**
265
-	 * Set the errors to use for validations
266
-	 *
267
-	 * @param Message $validator The result from a validation
268
-	 *
269
-	 * @return  void
270
-	 */
271
-	public function withErrors($validator = null)
272
-	{
273
-		// Try to get the errors form the session
274
-		if ($this->app['session']->has('errors')) {
275
-			$this->errors = $this->app['session']->get('errors');
276
-		}
277
-
278
-		// If we're given a raw Validator, go fetch the errors in it
279
-		if ($validator instanceof Validator) {
280
-			$this->errors = $validator->getMessageBag();
281
-		} else {
282
-			if ($validator instanceof MessageBag) {
283
-				$this->errors = $validator;
284
-			}
285
-		}
286
-
287
-		return $this->errors;
288
-	}
289
-
290
-	/**
291
-	 * Add live validation rules
292
-	 *
293
-	 * @param  array *$rules An array of Laravel rules
294
-	 *
295
-	 * @return  void
296
-	 */
297
-	public function withRules()
298
-	{
299
-		$rules = call_user_func_array('array_merge', func_get_args());
300
-
301
-		// Parse the rules according to Laravel conventions
302
-		foreach ($rules as $name => $fieldRules) {
303
-			$expFieldRules = $fieldRules;
304
-			if (!is_array($expFieldRules)) {
305
-				if (is_object($expFieldRules)) {
306
-					continue;
307
-				}
308
-
309
-				$expFieldRules = explode('|', $expFieldRules);
310
-				$expFieldRules = array_map('trim', $expFieldRules);
311
-			}
312
-
313
-			foreach ($expFieldRules as $rule) {
314
-				if (is_object($rule)) {
315
-					continue;
316
-				}
317
-
318
-				$parameters = null;
319
-
320
-				if (($colon = strpos($rule, ':')) !== false) {
321
-					$rulename = substr($rule, 0, $colon);
322
-
323
-					/**
324
-					 * Regular expressions may contain commas and should not be divided by str_getcsv.
325
-					 * For regular expressions we are just using the complete expression as a parameter.
326
-					 */
327
-					if ($rulename !== 'regex') {
328
-						$parameters = str_getcsv(substr($rule, $colon + 1));
329
-					} else {
330
-						$parameters = [substr($rule, $colon + 1)];
331
-					}
332
-				}
333
-
334
-				// Exclude unsupported rules
335
-				$rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule;
336
-
337
-				// Store processed rule in Former's array
338
-				if (!isset($parameters)) {
339
-					$parameters = array();
340
-				}
341
-
342
-				$this->rules[$name][$rule] = $parameters;
343
-			}
344
-		}
345
-	}
346
-
347
-	/**
348
-	 * Switch the framework used by Former
349
-	 *
350
-	 * @param string $framework The name of the framework to use
351
-	 */
352
-	public function framework($framework = null)
353
-	{
354
-		if (!$framework) {
355
-			return $this->app['former.framework']->current();
356
-		}
357
-
358
-		$this->setOption('framework', $framework);
359
-
360
-		$framework = $this->getFrameworkInstance($framework);
361
-		$this->app->bind('former.framework', function ($app) use ($framework) {
362
-			return $framework;
363
-		});
364
-	}
365
-
366
-	/**
367
-	 * Get a new framework instance
368
-	 *
369
-	 * @param string $framework
370
-	 *
371
-	 * @throws Exceptions\InvalidFrameworkException
372
-	 * @return \Former\Interfaces\FrameworkInterface
373
-	 */
374
-	public function getFrameworkInstance($framework)
375
-	{
376
-		$formerClass = __NAMESPACE__.'\Framework\\'.$framework;
377
-
378
-		//get interfaces of the given framework
379
-		$interfaces = class_exists($framework) ? class_implements($framework) : array();
380
-
381
-		if(class_exists($formerClass)) {
382
-			$returnClass = $formerClass;
383
-		} elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) {
384
-			// We have some outside class, lets return it.
385
-			$returnClass = $framework;
386
-		} else {
387
-			throw (new InvalidFrameworkException())->setFramework($framework);
388
-		}
389
-
390
-		return new $returnClass($this->app);
391
-	}
392
-
393
-	/**
394
-	 * Get an option from the config
395
-	 *
396
-	 * @param string $option  The option
397
-	 * @param mixed  $default Optional fallback
398
-	 *
399
-	 * @return mixed
400
-	 */
401
-	public function getOption($option, $default = null)
402
-	{
403
-		return $this->app['config']->get('former.'.$option, $default);
404
-	}
405
-
406
-	/**
407
-	 * Set an option on the config
408
-	 *
409
-	 * @param string $option
410
-	 * @param string $value
411
-	 */
412
-	public function setOption($option, $value)
413
-	{
414
-		return $this->app['config']->set('former.'.$option, $value);
415
-	}
416
-
417
-	////////////////////////////////////////////////////////////////////
418
-	////////////////////////////// BUILDERS ////////////////////////////
419
-	////////////////////////////////////////////////////////////////////
420
-
421
-	/**
422
-	 * Closes a form
423
-	 *
424
-	 * @return string A form closing tag
425
-	 */
426
-	public function close()
427
-	{
428
-		if ($this->app->bound('former.form')) {
429
-			$closing = $this->app['former.form']->close();
430
-		}
431
-
432
-		// Destroy instances
433
-		$instances = array('former.form', 'former.form.framework');
434
-		foreach ($instances as $instance) {
435
-			$this->app[$instance] = null;
436
-			unset($this->app[$instance]);
437
-		}
438
-
439
-		// Reset populator
440
-		$this->app['former.populator']->reset();
441
-
442
-		// Reset all values
443
-		$this->errors = null;
444
-		$this->rules  = array();
445
-
446
-		return isset($closing) ? $closing : null;
447
-	}
448
-
449
-	////////////////////////////////////////////////////////////////////
450
-	////////////////////////////// HELPERS /////////////////////////////
451
-	////////////////////////////////////////////////////////////////////
452
-
453
-	/**
454
-	 * Get the errors for the current field
455
-	 *
456
-	 * @param  string $name A field name
457
-	 *
458
-	 * @return string       An error message
459
-	 */
460
-	public function getErrors($name = null)
461
-	{
462
-		// Get name and translate array notation
463
-		if (!$name and $this->app['former.field']) {
464
-			$name = $this->app['former.field']->getName();
465
-
466
-			// Always return empty string for anonymous fields (i.e. fields with no name/id)
467
-			if (!$name) {
468
-				return '';
469
-			}
470
-		}
471
-
472
-		if ($this->errors and $name) {
473
-			$name = str_replace(array('[', ']'), array('.', ''), $name);
474
-			$name = trim($name, '.');
475
-
476
-			return $this->errors->first($name);
477
-		}
478
-
479
-		return $this->errors;
480
-	}
481
-
482
-	/**
483
-	 * Get a rule from the Rules array
484
-	 *
485
-	 * @param  string $name The field to fetch
486
-	 *
487
-	 * @return array        An array of rules
488
-	 */
489
-	public function getRules($name)
490
-	{
491
-		// Check the rules for the name as given
492
-		$ruleset = Arr::get($this->rules, $name);
493
-
494
-		// If no rules found, convert to dot notation and try again
495
-		if (is_null($ruleset)) {
496
-			$name = str_replace(array('[', ']'), array('.', ''), $name);
497
-			$name = trim($name, '.');
498
-			$ruleset = Arr::get($this->rules, $name);
499
-		}
500
-
501
-		return $ruleset;
502
-	}
17
+    // Instances
18
+    ////////////////////////////////////////////////////////////////////
19
+
20
+
21
+    /**
22
+     * The current environment
23
+     *
24
+     * @var \Illuminate\Container\Container
25
+     */
26
+    protected $app;
27
+
28
+    /**
29
+     * The Method Dispatcher
30
+     *
31
+     * @var MethodDispatcher
32
+     */
33
+    protected $dispatch;
34
+
35
+    // Informations
36
+    ////////////////////////////////////////////////////////////////////
37
+
38
+    /**
39
+     * The form's errors
40
+     *
41
+     * @var Message
42
+     */
43
+    protected $errors;
44
+
45
+    /**
46
+     * An array of rules to use
47
+     *
48
+     * @var array
49
+     */
50
+    protected $rules = array();
51
+
52
+    /**
53
+     * An array of field macros
54
+     *
55
+     * @var array
56
+     */
57
+    protected $macros = array();
58
+
59
+    /**
60
+     * The labels created so far
61
+     *
62
+     * @var array
63
+     */
64
+    public $labels = array();
65
+
66
+    /**
67
+     * The IDs created so far
68
+     *
69
+     * @var array
70
+     */
71
+    public $ids = array();
72
+
73
+    /**
74
+     * A lookup table where the key is the input name,
75
+     * and the value is number of times seen. This is
76
+     * used to calculate unique ids.
77
+     *
78
+     * @var array
79
+     */
80
+    public $names = array();
81
+
82
+    // Namespaces
83
+    ////////////////////////////////////////////////////////////////////
84
+
85
+    /**
86
+     * The namespace of Form elements
87
+     */
88
+    const FORMSPACE = 'Former\Form\\';
89
+
90
+    /**
91
+     * The namespace of fields
92
+     */
93
+    const FIELDSPACE = 'Former\Form\Fields\\';
94
+
95
+    /**
96
+     * Build a new Former instance
97
+     *
98
+     * @param Container        $app
99
+     * @param MethodDispatcher $dispatcher
100
+     */
101
+    public function __construct(Container $app, MethodDispatcher $dispatcher)
102
+    {
103
+        $this->app      = $app;
104
+        $this->dispatch = $dispatcher;
105
+    }
106
+
107
+    ////////////////////////////////////////////////////////////////////
108
+    //////////////////////////// INTERFACE /////////////////////////////
109
+    ////////////////////////////////////////////////////////////////////
110
+
111
+    /**
112
+     * Acts as a router that redirects methods to all of Former classes
113
+     *
114
+     * @param  string $method     The method called
115
+     * @param  array  $parameters An array of parameters
116
+     *
117
+     * @return mixed
118
+     */
119
+    public function __call($method, $parameters)
120
+    {
121
+        // Dispatch to Form\Elements
122
+        // Explicitly check false since closeGroup() may return an empty string
123
+        if (($element = $this->dispatch->toElements($method, $parameters)) !== false) {
124
+            return $element;
125
+        }
126
+
127
+        // Dispatch to Form\Form
128
+        if ($form = $this->dispatch->toForm($method, $parameters)) {
129
+            $this->app->instance('former.form', $form);
130
+
131
+            return $this->app['former.form'];
132
+        }
133
+
134
+        // Dispatch to Form\Group
135
+        if ($group = $this->dispatch->toGroup($method, $parameters)) {
136
+            return $group;
137
+        }
138
+
139
+        // Dispatch to Form\Actions
140
+        if ($actions = $this->dispatch->toActions($method, $parameters)) {
141
+            return $actions;
142
+        }
143
+
144
+        // Dispatch to macros
145
+        if ($macro = $this->dispatch->toMacros($method, $parameters)) {
146
+            return $macro;
147
+        }
148
+
149
+        // Checking for any supplementary classes
150
+        $modifiers = explode('_', $method);
151
+        $method  = array_pop($modifiers);
152
+
153
+        // Dispatch to the different Form\Fields
154
+        $field     = $this->dispatch->toFields($method, $parameters);
155
+        $field->setModifiers($modifiers);
156
+        $field->addClass('');
157
+
158
+        // Else bind field
159
+        $this->app->instance('former.field', $field);
160
+
161
+        return $this->app['former.field'];
162
+    }
163
+
164
+    ////////////////////////////////////////////////////////////////////
165
+    //////////////////////////////// MACROS ////////////////////////////
166
+    ////////////////////////////////////////////////////////////////////
167
+
168
+    /**
169
+     * Register a macro with Former
170
+     *
171
+     * @param  string   $name  The name of the macro
172
+     * @param  Callable $macro The macro itself
173
+     *
174
+     * @return mixed
175
+     */
176
+    public function macro($name, $macro)
177
+    {
178
+        $this->macros[$name] = $macro;
179
+    }
180
+
181
+    /**
182
+     * Check if a macro exists
183
+     *
184
+     * @param  string $name
185
+     *
186
+     * @return boolean
187
+     */
188
+    public function hasMacro($name)
189
+    {
190
+        return isset($this->macros[$name]);
191
+    }
192
+
193
+    /**
194
+     * Get a registered macro
195
+     *
196
+     * @param  string $name
197
+     *
198
+     * @return Closure
199
+     */
200
+    public function getMacro($name)
201
+    {
202
+        return $this->macros[$name];
203
+    }
204
+
205
+    ////////////////////////////////////////////////////////////////////
206
+    ///////////////////////////// POPULATOR ////////////////////////////
207
+    ////////////////////////////////////////////////////////////////////
208
+
209
+    /**
210
+     * Add values to populate the array
211
+     *
212
+     * @param mixed $values Can be an Eloquent object or an array
213
+     */
214
+    public function populate($values)
215
+    {
216
+        $this->app['former.populator']->replace($values);
217
+    }
218
+
219
+    /**
220
+     * Set the value of a particular field
221
+     *
222
+     * @param string $field The field's name
223
+     * @param mixed  $value Its new value
224
+     */
225
+    public function populateField($field, $value)
226
+    {
227
+        $this->app['former.populator']->put($field, $value);
228
+    }
229
+
230
+    /**
231
+     * Get the value of a field
232
+     *
233
+     * @param string $field The field's name
234
+     * @param null   $fallback
235
+     *
236
+     * @return mixed
237
+     */
238
+    public function getValue($field, $fallback = null)
239
+    {
240
+        return $this->app['former.populator']->get($field, $fallback);
241
+    }
242
+
243
+    /**
244
+     * Fetch a field value from both the new and old POST array
245
+     *
246
+     * @param  string $name     A field name
247
+     * @param  string $fallback A fallback if nothing was found
248
+     *
249
+     * @return string           The results
250
+     */
251
+    public function getPost($name, $fallback = null)
252
+    {
253
+        $name     = str_replace(array('[', ']'), array('.', ''), $name);
254
+        $name     = trim($name, '.');
255
+        $oldValue = $this->app['request']->old($name, $fallback);
256
+
257
+        return $this->app['request']->input($name, $oldValue, true);
258
+    }
259
+
260
+    ////////////////////////////////////////////////////////////////////
261
+    ////////////////////////////// TOOLKIT /////////////////////////////
262
+    ////////////////////////////////////////////////////////////////////
263
+
264
+    /**
265
+     * Set the errors to use for validations
266
+     *
267
+     * @param Message $validator The result from a validation
268
+     *
269
+     * @return  void
270
+     */
271
+    public function withErrors($validator = null)
272
+    {
273
+        // Try to get the errors form the session
274
+        if ($this->app['session']->has('errors')) {
275
+            $this->errors = $this->app['session']->get('errors');
276
+        }
277
+
278
+        // If we're given a raw Validator, go fetch the errors in it
279
+        if ($validator instanceof Validator) {
280
+            $this->errors = $validator->getMessageBag();
281
+        } else {
282
+            if ($validator instanceof MessageBag) {
283
+                $this->errors = $validator;
284
+            }
285
+        }
286
+
287
+        return $this->errors;
288
+    }
289
+
290
+    /**
291
+     * Add live validation rules
292
+     *
293
+     * @param  array *$rules An array of Laravel rules
294
+     *
295
+     * @return  void
296
+     */
297
+    public function withRules()
298
+    {
299
+        $rules = call_user_func_array('array_merge', func_get_args());
300
+
301
+        // Parse the rules according to Laravel conventions
302
+        foreach ($rules as $name => $fieldRules) {
303
+            $expFieldRules = $fieldRules;
304
+            if (!is_array($expFieldRules)) {
305
+                if (is_object($expFieldRules)) {
306
+                    continue;
307
+                }
308
+
309
+                $expFieldRules = explode('|', $expFieldRules);
310
+                $expFieldRules = array_map('trim', $expFieldRules);
311
+            }
312
+
313
+            foreach ($expFieldRules as $rule) {
314
+                if (is_object($rule)) {
315
+                    continue;
316
+                }
317
+
318
+                $parameters = null;
319
+
320
+                if (($colon = strpos($rule, ':')) !== false) {
321
+                    $rulename = substr($rule, 0, $colon);
322
+
323
+                    /**
324
+                     * Regular expressions may contain commas and should not be divided by str_getcsv.
325
+                     * For regular expressions we are just using the complete expression as a parameter.
326
+                     */
327
+                    if ($rulename !== 'regex') {
328
+                        $parameters = str_getcsv(substr($rule, $colon + 1));
329
+                    } else {
330
+                        $parameters = [substr($rule, $colon + 1)];
331
+                    }
332
+                }
333
+
334
+                // Exclude unsupported rules
335
+                $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule;
336
+
337
+                // Store processed rule in Former's array
338
+                if (!isset($parameters)) {
339
+                    $parameters = array();
340
+                }
341
+
342
+                $this->rules[$name][$rule] = $parameters;
343
+            }
344
+        }
345
+    }
346
+
347
+    /**
348
+     * Switch the framework used by Former
349
+     *
350
+     * @param string $framework The name of the framework to use
351
+     */
352
+    public function framework($framework = null)
353
+    {
354
+        if (!$framework) {
355
+            return $this->app['former.framework']->current();
356
+        }
357
+
358
+        $this->setOption('framework', $framework);
359
+
360
+        $framework = $this->getFrameworkInstance($framework);
361
+        $this->app->bind('former.framework', function ($app) use ($framework) {
362
+            return $framework;
363
+        });
364
+    }
365
+
366
+    /**
367
+     * Get a new framework instance
368
+     *
369
+     * @param string $framework
370
+     *
371
+     * @throws Exceptions\InvalidFrameworkException
372
+     * @return \Former\Interfaces\FrameworkInterface
373
+     */
374
+    public function getFrameworkInstance($framework)
375
+    {
376
+        $formerClass = __NAMESPACE__.'\Framework\\'.$framework;
377
+
378
+        //get interfaces of the given framework
379
+        $interfaces = class_exists($framework) ? class_implements($framework) : array();
380
+
381
+        if(class_exists($formerClass)) {
382
+            $returnClass = $formerClass;
383
+        } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) {
384
+            // We have some outside class, lets return it.
385
+            $returnClass = $framework;
386
+        } else {
387
+            throw (new InvalidFrameworkException())->setFramework($framework);
388
+        }
389
+
390
+        return new $returnClass($this->app);
391
+    }
392
+
393
+    /**
394
+     * Get an option from the config
395
+     *
396
+     * @param string $option  The option
397
+     * @param mixed  $default Optional fallback
398
+     *
399
+     * @return mixed
400
+     */
401
+    public function getOption($option, $default = null)
402
+    {
403
+        return $this->app['config']->get('former.'.$option, $default);
404
+    }
405
+
406
+    /**
407
+     * Set an option on the config
408
+     *
409
+     * @param string $option
410
+     * @param string $value
411
+     */
412
+    public function setOption($option, $value)
413
+    {
414
+        return $this->app['config']->set('former.'.$option, $value);
415
+    }
416
+
417
+    ////////////////////////////////////////////////////////////////////
418
+    ////////////////////////////// BUILDERS ////////////////////////////
419
+    ////////////////////////////////////////////////////////////////////
420
+
421
+    /**
422
+     * Closes a form
423
+     *
424
+     * @return string A form closing tag
425
+     */
426
+    public function close()
427
+    {
428
+        if ($this->app->bound('former.form')) {
429
+            $closing = $this->app['former.form']->close();
430
+        }
431
+
432
+        // Destroy instances
433
+        $instances = array('former.form', 'former.form.framework');
434
+        foreach ($instances as $instance) {
435
+            $this->app[$instance] = null;
436
+            unset($this->app[$instance]);
437
+        }
438
+
439
+        // Reset populator
440
+        $this->app['former.populator']->reset();
441
+
442
+        // Reset all values
443
+        $this->errors = null;
444
+        $this->rules  = array();
445
+
446
+        return isset($closing) ? $closing : null;
447
+    }
448
+
449
+    ////////////////////////////////////////////////////////////////////
450
+    ////////////////////////////// HELPERS /////////////////////////////
451
+    ////////////////////////////////////////////////////////////////////
452
+
453
+    /**
454
+     * Get the errors for the current field
455
+     *
456
+     * @param  string $name A field name
457
+     *
458
+     * @return string       An error message
459
+     */
460
+    public function getErrors($name = null)
461
+    {
462
+        // Get name and translate array notation
463
+        if (!$name and $this->app['former.field']) {
464
+            $name = $this->app['former.field']->getName();
465
+
466
+            // Always return empty string for anonymous fields (i.e. fields with no name/id)
467
+            if (!$name) {
468
+                return '';
469
+            }
470
+        }
471
+
472
+        if ($this->errors and $name) {
473
+            $name = str_replace(array('[', ']'), array('.', ''), $name);
474
+            $name = trim($name, '.');
475
+
476
+            return $this->errors->first($name);
477
+        }
478
+
479
+        return $this->errors;
480
+    }
481
+
482
+    /**
483
+     * Get a rule from the Rules array
484
+     *
485
+     * @param  string $name The field to fetch
486
+     *
487
+     * @return array        An array of rules
488
+     */
489
+    public function getRules($name)
490
+    {
491
+        // Check the rules for the name as given
492
+        $ruleset = Arr::get($this->rules, $name);
493
+
494
+        // If no rules found, convert to dot notation and try again
495
+        if (is_null($ruleset)) {
496
+            $name = str_replace(array('[', ']'), array('.', ''), $name);
497
+            $name = trim($name, '.');
498
+            $ruleset = Arr::get($this->rules, $name);
499
+        }
500
+
501
+        return $ruleset;
502
+    }
503 503
 }
Please login to merge, or discard this patch.