Completed
Pull Request — master (#592)
by Alex
11:33
created
src/Former/Former.php 1 patch
Indentation   +482 added lines, -482 removed lines patch added patch discarded remove patch
@@ -14,486 +14,486 @@
 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
-				$expFieldRules = explode('|', $expFieldRules);
306
-				$expFieldRules = array_map('trim', $expFieldRules);
307
-			}
308
-
309
-			foreach ($expFieldRules as $rule) {
310
-				if (is_object($rule)) {
311
-					continue;
312
-				}
313
-
314
-				$parameters = null;
315
-
316
-				if (($colon = strpos($rule, ':')) !== false) {
317
-					$rulename = substr($rule, 0, $colon);
318
-
319
-					/**
320
-					 * Regular expressions may contain commas and should not be divided by str_getcsv.
321
-					 * For regular expressions we are just using the complete expression as a parameter.
322
-					 */
323
-					if ($rulename !== 'regex') {
324
-						$parameters = str_getcsv(substr($rule, $colon + 1));
325
-					} else {
326
-						$parameters = [substr($rule, $colon + 1)];
327
-					}
328
-				}
329
-
330
-				// Exclude unsupported rules
331
-				$rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule;
332
-
333
-				// Store processed rule in Former's array
334
-				if (!isset($parameters)) {
335
-					$parameters = array();
336
-				}
337
-
338
-				$this->rules[$name][$rule] = $parameters;
339
-			}
340
-		}
341
-	}
342
-
343
-	/**
344
-	 * Switch the framework used by Former
345
-	 *
346
-	 * @param string $framework The name of the framework to use
347
-	 */
348
-	public function framework($framework = null)
349
-	{
350
-		if (!$framework) {
351
-			return $this->app['former.framework']->current();
352
-		}
353
-
354
-		$this->setOption('framework', $framework);
355
-
356
-		$framework = $this->getFrameworkInstance($framework);
357
-		$this->app->bind('former.framework', function ($app) use ($framework) {
358
-			return $framework;
359
-		});
360
-	}
361
-
362
-	/**
363
-	 * Get a new framework instance
364
-	 *
365
-	 * @param string $framework
366
-	 *
367
-	 * @throws Exceptions\InvalidFrameworkException
368
-	 * @return \Former\Interfaces\FrameworkInterface
369
-	 */
370
-	public function getFrameworkInstance($framework)
371
-	{
372
-		$formerClass = __NAMESPACE__.'\Framework\\'.$framework;
373
-
374
-		//get interfaces of the given framework
375
-		$interfaces = class_exists($framework) ? class_implements($framework) : array();
376
-
377
-		if(class_exists($formerClass)) {
378
-			$returnClass = $formerClass;
379
-		} elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) {
380
-			// We have some outside class, lets return it.
381
-			$returnClass = $framework;
382
-		} else {
383
-			throw (new InvalidFrameworkException())->setFramework($framework);
384
-		}
385
-
386
-		return new $returnClass($this->app);
387
-	}
388
-
389
-	/**
390
-	 * Get an option from the config
391
-	 *
392
-	 * @param string $option  The option
393
-	 * @param mixed  $default Optional fallback
394
-	 *
395
-	 * @return mixed
396
-	 */
397
-	public function getOption($option, $default = null)
398
-	{
399
-		return $this->app['config']->get('former.'.$option, $default);
400
-	}
401
-
402
-	/**
403
-	 * Set an option on the config
404
-	 *
405
-	 * @param string $option
406
-	 * @param string $value
407
-	 */
408
-	public function setOption($option, $value)
409
-	{
410
-		return $this->app['config']->set('former.'.$option, $value);
411
-	}
412
-
413
-	////////////////////////////////////////////////////////////////////
414
-	////////////////////////////// BUILDERS ////////////////////////////
415
-	////////////////////////////////////////////////////////////////////
416
-
417
-	/**
418
-	 * Closes a form
419
-	 *
420
-	 * @return string A form closing tag
421
-	 */
422
-	public function close()
423
-	{
424
-		if ($this->app->bound('former.form')) {
425
-			$closing = $this->app['former.form']->close();
426
-		}
427
-
428
-		// Destroy instances
429
-		$instances = array('former.form', 'former.form.framework');
430
-		foreach ($instances as $instance) {
431
-			$this->app[$instance] = null;
432
-			unset($this->app[$instance]);
433
-		}
434
-
435
-		// Reset populator
436
-		$this->app['former.populator']->reset();
437
-
438
-		// Reset all values
439
-		$this->errors = null;
440
-		$this->rules  = array();
441
-
442
-		return isset($closing) ? $closing : null;
443
-	}
444
-
445
-	////////////////////////////////////////////////////////////////////
446
-	////////////////////////////// HELPERS /////////////////////////////
447
-	////////////////////////////////////////////////////////////////////
448
-
449
-	/**
450
-	 * Get the errors for the current field
451
-	 *
452
-	 * @param  string $name A field name
453
-	 *
454
-	 * @return string       An error message
455
-	 */
456
-	public function getErrors($name = null)
457
-	{
458
-		// Get name and translate array notation
459
-		if (!$name and $this->app['former.field']) {
460
-			$name = $this->app['former.field']->getName();
461
-
462
-			// Always return empty string for anonymous fields (i.e. fields with no name/id)
463
-			if (!$name) {
464
-				return '';
465
-			}
466
-		}
467
-
468
-		if ($this->errors and $name) {
469
-			$name = str_replace(array('[', ']'), array('.', ''), $name);
470
-			$name = trim($name, '.');
471
-
472
-			return $this->errors->first($name);
473
-		}
474
-
475
-		return $this->errors;
476
-	}
477
-
478
-	/**
479
-	 * Get a rule from the Rules array
480
-	 *
481
-	 * @param  string $name The field to fetch
482
-	 *
483
-	 * @return array        An array of rules
484
-	 */
485
-	public function getRules($name)
486
-	{
487
-		// Check the rules for the name as given
488
-		$ruleset = Arr::get($this->rules, $name);
489
-
490
-		// If no rules found, convert to dot notation and try again
491
-		if (is_null($ruleset)) {
492
-			$name = str_replace(array('[', ']'), array('.', ''), $name);
493
-			$name = trim($name, '.');
494
-			$ruleset = Arr::get($this->rules, $name);
495
-		}
496
-
497
-		return $ruleset;
498
-	}
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
+                $expFieldRules = explode('|', $expFieldRules);
306
+                $expFieldRules = array_map('trim', $expFieldRules);
307
+            }
308
+
309
+            foreach ($expFieldRules as $rule) {
310
+                if (is_object($rule)) {
311
+                    continue;
312
+                }
313
+
314
+                $parameters = null;
315
+
316
+                if (($colon = strpos($rule, ':')) !== false) {
317
+                    $rulename = substr($rule, 0, $colon);
318
+
319
+                    /**
320
+                     * Regular expressions may contain commas and should not be divided by str_getcsv.
321
+                     * For regular expressions we are just using the complete expression as a parameter.
322
+                     */
323
+                    if ($rulename !== 'regex') {
324
+                        $parameters = str_getcsv(substr($rule, $colon + 1));
325
+                    } else {
326
+                        $parameters = [substr($rule, $colon + 1)];
327
+                    }
328
+                }
329
+
330
+                // Exclude unsupported rules
331
+                $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule;
332
+
333
+                // Store processed rule in Former's array
334
+                if (!isset($parameters)) {
335
+                    $parameters = array();
336
+                }
337
+
338
+                $this->rules[$name][$rule] = $parameters;
339
+            }
340
+        }
341
+    }
342
+
343
+    /**
344
+     * Switch the framework used by Former
345
+     *
346
+     * @param string $framework The name of the framework to use
347
+     */
348
+    public function framework($framework = null)
349
+    {
350
+        if (!$framework) {
351
+            return $this->app['former.framework']->current();
352
+        }
353
+
354
+        $this->setOption('framework', $framework);
355
+
356
+        $framework = $this->getFrameworkInstance($framework);
357
+        $this->app->bind('former.framework', function ($app) use ($framework) {
358
+            return $framework;
359
+        });
360
+    }
361
+
362
+    /**
363
+     * Get a new framework instance
364
+     *
365
+     * @param string $framework
366
+     *
367
+     * @throws Exceptions\InvalidFrameworkException
368
+     * @return \Former\Interfaces\FrameworkInterface
369
+     */
370
+    public function getFrameworkInstance($framework)
371
+    {
372
+        $formerClass = __NAMESPACE__.'\Framework\\'.$framework;
373
+
374
+        //get interfaces of the given framework
375
+        $interfaces = class_exists($framework) ? class_implements($framework) : array();
376
+
377
+        if(class_exists($formerClass)) {
378
+            $returnClass = $formerClass;
379
+        } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) {
380
+            // We have some outside class, lets return it.
381
+            $returnClass = $framework;
382
+        } else {
383
+            throw (new InvalidFrameworkException())->setFramework($framework);
384
+        }
385
+
386
+        return new $returnClass($this->app);
387
+    }
388
+
389
+    /**
390
+     * Get an option from the config
391
+     *
392
+     * @param string $option  The option
393
+     * @param mixed  $default Optional fallback
394
+     *
395
+     * @return mixed
396
+     */
397
+    public function getOption($option, $default = null)
398
+    {
399
+        return $this->app['config']->get('former.'.$option, $default);
400
+    }
401
+
402
+    /**
403
+     * Set an option on the config
404
+     *
405
+     * @param string $option
406
+     * @param string $value
407
+     */
408
+    public function setOption($option, $value)
409
+    {
410
+        return $this->app['config']->set('former.'.$option, $value);
411
+    }
412
+
413
+    ////////////////////////////////////////////////////////////////////
414
+    ////////////////////////////// BUILDERS ////////////////////////////
415
+    ////////////////////////////////////////////////////////////////////
416
+
417
+    /**
418
+     * Closes a form
419
+     *
420
+     * @return string A form closing tag
421
+     */
422
+    public function close()
423
+    {
424
+        if ($this->app->bound('former.form')) {
425
+            $closing = $this->app['former.form']->close();
426
+        }
427
+
428
+        // Destroy instances
429
+        $instances = array('former.form', 'former.form.framework');
430
+        foreach ($instances as $instance) {
431
+            $this->app[$instance] = null;
432
+            unset($this->app[$instance]);
433
+        }
434
+
435
+        // Reset populator
436
+        $this->app['former.populator']->reset();
437
+
438
+        // Reset all values
439
+        $this->errors = null;
440
+        $this->rules  = array();
441
+
442
+        return isset($closing) ? $closing : null;
443
+    }
444
+
445
+    ////////////////////////////////////////////////////////////////////
446
+    ////////////////////////////// HELPERS /////////////////////////////
447
+    ////////////////////////////////////////////////////////////////////
448
+
449
+    /**
450
+     * Get the errors for the current field
451
+     *
452
+     * @param  string $name A field name
453
+     *
454
+     * @return string       An error message
455
+     */
456
+    public function getErrors($name = null)
457
+    {
458
+        // Get name and translate array notation
459
+        if (!$name and $this->app['former.field']) {
460
+            $name = $this->app['former.field']->getName();
461
+
462
+            // Always return empty string for anonymous fields (i.e. fields with no name/id)
463
+            if (!$name) {
464
+                return '';
465
+            }
466
+        }
467
+
468
+        if ($this->errors and $name) {
469
+            $name = str_replace(array('[', ']'), array('.', ''), $name);
470
+            $name = trim($name, '.');
471
+
472
+            return $this->errors->first($name);
473
+        }
474
+
475
+        return $this->errors;
476
+    }
477
+
478
+    /**
479
+     * Get a rule from the Rules array
480
+     *
481
+     * @param  string $name The field to fetch
482
+     *
483
+     * @return array        An array of rules
484
+     */
485
+    public function getRules($name)
486
+    {
487
+        // Check the rules for the name as given
488
+        $ruleset = Arr::get($this->rules, $name);
489
+
490
+        // If no rules found, convert to dot notation and try again
491
+        if (is_null($ruleset)) {
492
+            $name = str_replace(array('[', ']'), array('.', ''), $name);
493
+            $name = trim($name, '.');
494
+            $ruleset = Arr::get($this->rules, $name);
495
+        }
496
+
497
+        return $ruleset;
498
+    }
499 499
 }
Please login to merge, or discard this patch.