BootstrapBase::input()   F
last analyzed

Complexity

Conditions 25
Paths 2944

Size

Total Lines 99
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 2
c 0
b 0
f 0
cc 25
eloc 76
nc 2944
nop 7

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Cornford\Bootstrapper;
2
3
namespace Cornford\Bootstrapper;
4
5
use Illuminate\Html\FormBuilder;
6
use Illuminate\Html\HtmlBuilder;
7
use Illuminate\Http\Request;
8
9
abstract class BootstrapBase {
10
11
	const FORM_VERTICAL = 'vertical';
12
	const FORM_HORIZONTAL = 'horizontal';
13
	const FORM_INLINE = 'inline';
14
15
	const CSS_BOOTSTRAP_CDN = '//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css';
16
	const CSS_BOOTSTRAP_LOCAL = 'packages/cornford/bootstrapper/assets/css/bootstrap.min.css';
17
18
	const JS_BOOTSTRAP_CDN = '//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js';
19
	const JS_BOOTSTRAP_LOCAL = 'packages/cornford/bootstrapper/assets/js/bootstrap.min.js';
20
21
	const JS_JQUERY_CDN = '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js';
22
	const JS_JQUERY_LOCAL = 'packages/cornford/bootstrapper/assets/js/jquery.min.js';
23
24
	const JS_MOMENT_CDN = '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js';
25
	const JS_MOMENT_LOCAL = 'packages/cornford/bootstrapper/assets/js/moment.min.js';
26
27
	const CSS_DATETIME_CDN = '//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.1.3/css/bootstrap-datetimepicker.min.css';
28
	const CSS_DATETIME_LOCAL = 'packages/cornford/bootstrapper/assets/css/bootstrap-datetimepicker.min.css';
29
30
	const JS_DATETIME_CDN = '//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.1.3/js/bootstrap-datetimepicker.min.js';
31
	const JS_DATETIME_LOCAL = 'packages/cornford/bootstrapper/assets/js/bootstrap-datetimepicker.min.js';
32
33
	/**
34
	 * Form
35
	 *
36
	 * @var \Illuminate\Html\FormBuilder
37
	 */
38
	protected $form;
39
40
	/**
41
	 * HTML
42
	 *
43
	 * @var \Illuminate\Html\HtmlBuilder
44
	 */
45
	protected $html;
46
47
	/**
48
	 * Input
49
	 *
50
	 * @var \Illuminate\Http\Request
51
	 */
52
	protected $input;
53
54
	/**
55
	 * Form Type
56
	 *
57
	 * @var string
58
	 */
59
	protected $formType = self::FORM_VERTICAL;
60
61
	/**
62
	 * Form Input Class
63
	 *
64
	 * @var string
65
	 */
66
	protected $inputClass;
67
68
	/**
69
	 * Form Label Class
70
	 *
71
	 * @var string
72
	 */
73
	protected $labelClass;
74
75
	public function __construct(FormBuilder $form, HtmlBuilder $html, Request $input)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $input. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
76
	{
77
		$this->form = $form;
78
		$this->html = $html;
79
		$this->input = $input;
80
	}
81
82
	/**
83
	 * Set the form type
84
	 *
85
	 * @param string $type
86
	 * @param string $inputClass
87
	 * @param string $labelClass
88
	 *
89
	 * @return void
90
	 */
91
	protected function form($type = self::FORM_VERTICAL, $inputClass = '', $labelClass = '') {
92
		$this->formType = $type;
93
		$this->inputClass = $inputClass;
94
		$this->labelClass = $labelClass;
95
	}
96
97
	/**
98
	 * Set the form type to vertical
99
	 *
100
	 * @return self
101
	 */
102
	public function vertical() {
103
		$this->form();
104
105
		return $this;
106
	}
107
108
	/**
109
	 * Set the form type to horizontal
110
	 *
111
	 * @param string $inputClass
112
	 * @param string $labelClass
113
	 *
114
	 * @return self
115
	 */
116
	public function horizontal($inputClass = '', $labelClass = '') {
117
		$this->form(self::FORM_HORIZONTAL, $inputClass, $labelClass);
118
119
		return $this;
120
	}
121
122
	/**
123
	 * Set the form type to inline
124
	 *
125
	 * @param string $labelClass
126
	 *
127
	 * @return self
128
	 */
129
	public function inline($labelClass = '') {
130
		$this->form(self::FORM_INLINE, '', $labelClass);
131
132
		return $this;
133
	}
134
135
	/**
136
	 * Get the form type
137
	 *
138
	 * @return string
139
	 */
140
	public function getFormType() {
141
		return $this->formType;
142
	}
143
144
	/**
145
	 * Include the Bootstrap file
146
	 *
147
	 * @param string $type
148
	 * @param string $location
149
	 * @param array  $attributes
150
	 *
151
	 * @return string
152
	 */
153
	protected function add($type, $location, array $attributes = array())
154
	{
155
		return $this->html->$type($location, $attributes);
156
	}
157
158
	/**
159
	 * Create a form label field.
160
	 *
161
	 * @param string $name
162
	 * @param string $label
163
	 * @param array  $options
164
	 * @param string $content
165
	 *
166
	 * @return string
167
	 */
168
	protected function label($name, $label = null, array $options = array(), $content = null) {
169
		$return = '';
170
		$options = array_merge(array('class' => 'control-label ' . $this->labelClass, 'for' => (!$content ? $name : null)), $options);
171
172
		if ($label !== null) {
173
			$return .= '<label' . $this->html->attributes($options) . '>' . $content . ucwords(str_replace('_', ' ', $label)) . '</label>' . "\n";
174
		}
175
176
		return $return;
177
	}
178
179
	/**
180
	 * Create a form error helper field.
181
	 *
182
	 * @param string                         $name
183
	 * @param \Illuminate\Support\MessageBag $errors
184
	 * @param string                         $wrap
185
	 *
186
	 * @return string
187
	 */
188
	protected function errors($name, $errors = null, $wrap = '<span class="help-block">:message</span>') {
189
		$return = '';
190
191
		if ($errors && $errors->has($name)) {
192
			$return .= $errors->first($name, $wrap) . "\n";
193
		}
194
195
		return $return;
196
	}
197
198
	/**
199
	 * Create a form group element.
200
	 *
201
	 * @param string                         $name
202
	 * @param \Illuminate\Support\MessageBag $errors
203
	 * @param string                         $class
204
	 * @param array                          $options
205
	 *
206
	 * @return string
207
	 */
208
	protected function group($name, $errors = null, $class = 'form-group', array $options = array())
209
	{
210
		$options = array_merge(array('class' => $class), $options);
211
		$options['class'] .= ($errors && $errors->has($name) ? ' has-error' : '');
212
		$return = '<div' . $this->html->attributes($options) . '>' . "\n";
213
214
		return $return;
215
	}
216
217
	/**
218
	 * Create a form input field.
219
	 *
220
	 * @param string                         $type
221
	 * @param string                         $name
222
	 * @param string                         $value
223
	 * @param string                         $label
224
	 * @param \Illuminate\Support\MessageBag $errors
225
	 * @param array                          $options
226
	 * @param array                          $parameters
227
	 *
228
	 * @return string
229
	 */
230
	protected function input($type = 'text', $name, $label = null, $value = null, $errors = null, array $options = array(), array $parameters = array())
231
	{
232
		$return = '';
233
		$options = array_merge(array('class' => 'form-control', 'placeholder' => $label, 'id' => $name), $options);
234
		$containerAttributes = $this->getContainerAttributes($options);
235
		$labelAttributes = $this->getLabelAttributes($options);
236
237
		if (!isset($containerAttributes['display'])) {
238
			$return .= $this->group($name, $errors, 'form-group', $containerAttributes);
239
		}
240
241
		if ($type != 'search') {
242
			$return .= $this->label($name, $label, $labelAttributes);
243
		}
244
245
		if ($value === null) {
246
			$value = $this->input->get($name);
247
		}
248
249
		if ($this->formType == self::FORM_HORIZONTAL) {
250
			$return .= $this->group('', null, $this->inputClass);
251
		}
252
253
		switch ($type) {
254
			case 'datetime':
255
			case 'date':
256
			case 'time':
257
					if (isset($parameters['displayIcon']) && !$parameters['displayIcon']) {
258
						unset($parameters['displayIcon']);
259
						$return .= $this->form->text($name, $value, $options);
260
					} else {
261
						$return .= '<div id="' . $name . '_' . $type .  '" class="input-group ' . $type . '">';
262
						$return .= $this->form->text($name, $value, $options);
263
						$return .= '<span class="input-group-addon">' . "\n" . '<span class="glyphicon glyphicon-' . ($type == 'time' ? 'time' : 'calendar')  . '"></span>' . "\n" . '</span>' . "\n" . '</div>' . "\n";
264
					}
265
266
					$return .= '<script type="text/javascript">$(function() { $("#' . $name . ', #' . $name . '_' . $type . '").datetimepicker({ ';
267
268
					switch ($type) {
269
						case 'time':
270
							$return .= 'pickDate: false, ';
271
							break;
272
						case 'date':
273
							$return .= 'pickTime: false, ';
274
							break;
275
						case 'datetime':
276
						default:
277
					}
278
279
					$return .= implode(', ', array_map(function ($value, $key) { return $key . ': "' . $value . '"'; }, $parameters, array_keys($parameters)));
280
					$return = rtrim($return, ', ');
281
					$return .= ' }); });</script>' . "\n";
282
				break;
283
			case 'password':
284
			case 'file':
285
				unset($options['placeholder']);
286
				$return .= $this->form->$type($name, $options) . "\n";
287
				break;
288
			case 'search':
289
				$return .= '<div class="input-group">' . "\n";
290
				$return .= $this->form->input('search', $name, $value, $options) . "\n";
291
				$return .= '<div class="input-group-btn">' . "\n";
292
				$return .= '<button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>' . "\n";
293
				$return .= '</div>' . "\n";
294
				$return .= '</div>' . "\n";
295
				break;
296
			case 'telephone':
297
				$return .= $this->form->input('tel', $name, $value, $options) . "\n";
298
				break;
299
			case 'range':
300
				$return .= '<div class="input-group">' . "\n";
301
				$return .= '<div class="form-control">' . "\n";
302
				$return .= $this->form->input('range', $name, $value, array_merge($options, array('class' => '', 'onchange' => $name . 'value.value=value', 'oninput' => $name . 'value.value=value'))) . "\n";
303
				$return .= '</div>' . "\n";
304
				$return .= '<output id="' . $name . 'value" class="input-group-addon">0</output>' . "\n";
305
				$return .= '</div>' . "\n";
306
				break;
307
			case 'text':
308
			case 'hidden':
309
			case 'email':
310
			case 'textarea':
311
				$return .= $this->form->$type($name, $value, $options) . "\n";
312
				break;
313
			default:
314
				$return .= $this->form->input($type, $name, $value, $options) . "\n";
315
		}
316
317
		$return .= $this->errors($name, $errors);
318
319
		if ($this->formType == self::FORM_HORIZONTAL) {
320
			$return .= '</div>' . "\n";
321
		}
322
323
		if (!isset($containerAttributes['display'])) {
324
			$return .= '</div>' . "\n";
325
		}
326
327
		return $return;
328
	}
329
330
	/**
331
	 * Create a form select field.
332
	 *
333
	 * @param string                         $name
334
	 * @param string                         $label
335
	 * @param array                          $list
336
	 * @param string                         $selected
337
	 * @param \Illuminate\Support\MessageBag $errors
338
	 * @param array                          $options
339
	 *
340
	 * @return string
341
	 */
342
	protected function options($name, $label = null, array $list = array(), $selected = null, $errors = null, array $options = array())
343
	{
344
		$return = '';
345
		$options = array_merge(array('class' => 'form-control', 'id' => $name), $options);
346
		$containerAttributes = $this->getContainerAttributes($options);
347
		$labelAttributes = $this->getLabelAttributes($options);
348
349
		if (!isset($containerAttributes['display'])) {
350
			$return .= $this->group($name, $errors, 'form-group', $containerAttributes);
351
		}
352
353
		$return .= $this->label($name, $label, $labelAttributes);
354
355
		if ($this->formType == self::FORM_HORIZONTAL) {
356
			$return .= $this->group('', null, $this->inputClass);
357
		}
358
359
		$return .= $this->form->select($name, $list, $selected, $options) . "\n";
360
		$return .= $this->errors($name, $errors);
361
362
		if ($this->formType == self::FORM_HORIZONTAL) {
363
			$return .= '</div>' . "\n";
364
		}
365
366
		if (!isset($containerAttributes['display'])) {
367
			$return .= '</div>' . "\n";
368
		}
369
370
		return $return;
371
	}
372
373
	/**
374
	 * Create a form field.
375
	 *
376
	 * @param string $type
377
	 * @param string $name
378
	 * @param string $label
379
	 * @param string $value
380
	 * @param string $checked
381
	 * @param array  $options
382
	 *
383
	 * @return string
384
	 */
385
	protected function field($type, $name, $label = null, $value = null, $checked = null, array $options = array())
386
	{
387
		$return = '';
388
		$options = array_merge(array('id' => $name), $options);
389
		$containerAttributes = $this->getContainerAttributes($options);
390
		$labelAttributes = $this->getLabelAttributes($options);
391
392
		if ($this->formType != self::FORM_INLINE && !isset($containerAttributes['display'])) {
393
			$return .= $this->group($name, null, 'form-group', $containerAttributes);
394
		}
395
396
		if ($this->formType == self::FORM_HORIZONTAL) {
397
			$return .= $this->group('', null, $this->inputClass);
398
		}
399
400
		$return .= '<div class="' . $type . '">' . "\n";
401
		$return .= $this->label($name, $label, $labelAttributes, $this->form->$type($name, $value, $checked, $options));
402
		$return .= '</div>' . "\n";
403
404
		if ($this->formType == self::FORM_HORIZONTAL) {
405
			$return .= '</div>' . "\n";
406
		}
407
408
		if ($this->formType != self::FORM_INLINE && !isset($containerAttributes['display'])) {
409
			$return .= '</div>';
410
		}
411
412
		return $return;
413
	}
414
415
	/**
416
	 * Create a form action button.
417
	 *
418
	 * @param string $value
419
	 * @param string $type
420
	 * @param array  $attributes
421
	 *
422
	 * @return string
423
	 */
424
	protected function action($type, $value, array $attributes = array())
425
	{
426
		$return = '';
427
		$containerAttributes = $this->getContainerAttributes($attributes);
428
429
		switch ($type) {
430
			case 'submit':
431
				$attributes = array_merge(array('class' => 'btn btn-primary pull-right'), $attributes);
432
				break;
433
			case 'button':
434
			case 'reset':
435
			default:
436
				$attributes = array_merge(array('class' => 'btn btn-default'), $attributes);
437
		}
438
439
		if (!isset($containerAttributes['display'])) {
440
			$return .= $this->group('', null, 'form-group', $containerAttributes);
441
		}
442
443
		if ($this->formType == self::FORM_HORIZONTAL) {
444
			$return .= $this->group('', null, $this->inputClass);
445
		}
446
447
		$return .= $this->form->$type($value, $attributes) . "\n";
448
449
		if ($this->formType == self::FORM_HORIZONTAL) {
450
			$return .= '</div>' . "\n";
451
		}
452
453
		if (!isset($containerAttributes['display'])) {
454
			$return .= '</div>' . "\n";
455
		}
456
457
		return $return;
458
	}
459
460
	/**
461
	 * Create a button link.
462
	 *
463
	 * @param string $type
464
	 * @param string $location
465
	 * @param string $title
466
	 * @param array  $parameters
467
	 * @param string $secure
468
	 * @param array  $attributes
469
	 *
470
	 * @return string
471
	 */
472
	protected function hyperlink($type, $location, $title = null, array $parameters = array(), array $attributes = array(), $secure = null)
473
	{
474
		$attributes = array_merge(array('class' => 'btn btn-default'), $attributes);
475
476
		switch ($type) {
477
			case 'linkRoute':
478
			case 'linkAction':
479
				$return = $this->html->$type($location, $title, $parameters, $attributes);
480
				break;
481
			case 'mailto':
482
				$return = $this->html->$type($location, $title, $attributes);
483
				break;
484
			case 'link':
485
			case 'secureLink':
486
			default:
487
				$return = $this->html->$type($location, $title, $attributes, $secure);
488
		}
489
490
		return $return;
491
	}
492
493
	/**
494
	 * Create an alert item with optional emphasis.
495
	 *
496
	 * @param string  $type
497
	 * @param string  $content
498
	 * @param string  $emphasis
499
	 * @param boolean $dismissible
500
	 * @param array   $attributes
501
	 *
502
	 * @return string
503
	 */
504
	protected function alert($type = 'message', $content = null, $emphasis = null, $dismissible = false, array $attributes = array())
505
	{
506
		$attributes = array_merge(array('class' => 'alert' . ($dismissible ? ' alert-dismissable' : '') . ' alert-' . ($type != 'message' ? $type : 'default')), $attributes);
507
		$return = '<div ' . $this->html->attributes($attributes) . '>';
508
509
		if ($dismissible !== false) {
510
			$return .= '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
511
		}
512
513
		$return .= ($emphasis !== null && is_string($emphasis) ? '<strong>' . $emphasis . '</strong> ' : '') . $content . '</div>';
514
515
		return $return;
516
	}
517
518
	/**
519
	 * Get container attributes.
520
	 *
521
	 * @param array &$attributes
522
	 *
523
	 * @return array
524
	 */
525
	protected function getContainerAttributes(array &$attributes = array())
526
	{
527
		$containerAttributes = array();
528
529
		if (isset($attributes['container'])) {
530
			$containerAttributes = $attributes['container'];
531
			unset($attributes['container']);
532
		}
533
534
		return $containerAttributes;
535
	}
536
537
	/**
538
	 * Get label attributes.
539
	 *
540
	 * @param array &$attributes
541
	 *
542
	 * @return array
543
	 */
544
	protected function getLabelAttributes(array &$attributes = array())
545
	{
546
		$labelAttributes = array();
547
548
		if (isset($attributes['label'])) {
549
			$labelAttributes = $attributes['label'];
550
			unset($attributes['label']);
551
		}
552
553
		return $labelAttributes;
554
	}
555
556
}
557