Skip to content

Commit 954fc0a

Browse files
committed
Clean code.
1 parent 9aee906 commit 954fc0a

File tree

1 file changed

+84
-69
lines changed

1 file changed

+84
-69
lines changed

src/View/Helper/BootstrapFormHelper.php

Lines changed: 84 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
class BootstrapFormHelper extends FormHelper {
2020

21-
use BootstrapTrait ;
21+
use BootstrapTrait;
2222

2323
public $helpers = [
2424
'Html',
2525
'Url',
2626
'bHtml' => [
2727
'className' => 'Bootstrap.BootstrapHtml'
2828
]
29-
] ;
29+
];
3030

3131
/**
3232
* Default config for the helper.
@@ -105,13 +105,13 @@ class BootstrapFormHelper extends FormHelper {
105105
'_default' => ['Cake\View\Widget\BasicWidget'],
106106
];
107107

108-
public $horizontal = false ;
109-
public $inline = false ;
108+
public $horizontal = false;
109+
public $inline = false;
110110

111111
/**
112112
*
113-
* Replace the templates with the ones specified by newTemplates, call the specified function
114-
* with the specified parameters, and then restore the old templates.
113+
* Replace the templates with the ones specified by newTemplates, call the
114+
* specified function with the specified parameters, and then restore the old templates.
115115
*
116116
* @params $templates The new templates
117117
* @params $callback The function to call
@@ -121,11 +121,13 @@ class BootstrapFormHelper extends FormHelper {
121121
*
122122
**/
123123
protected function _wrapTemplates ($templates, $callback, $params) {
124-
$oldTemplates = array_map ([$this, 'templates'], array_combine(array_keys($templates), array_keys($templates))) ;
125-
$this->templates ($templates) ;
126-
$result = call_user_func_array ($callback, $params) ;
127-
$this->templates ($oldTemplates) ;
128-
return $result ;
124+
$oldTemplates = array_map ([$this, 'templates'],
125+
array_combine(array_keys($templates),
126+
array_keys($templates)));
127+
$this->templates ($templates);
128+
$result = call_user_func_array ($callback, $params);
129+
$this->templates ($oldTemplates);
130+
return $result;
129131
}
130132

131133
/**
@@ -138,7 +140,7 @@ protected function _wrapTemplates ($templates, $callback, $params) {
138140
*
139141
**/
140142
protected function _matchButton ($html) {
141-
return strpos($html, '<button') !== FALSE || strpos($html, 'type="submit"') !== FALSE ;
143+
return strpos($html, '<button') !== FALSE || strpos($html, 'type="submit"') !== FALSE;
142144
}
143145

144146
protected function _getDefaultTemplateVars (&$options) {
@@ -211,15 +213,15 @@ public function create($model = null, Array $options = array()) {
211213
$this->colSize = $options['columns'];
212214
$this->horizontal = $options['horizontal'];
213215
$this->inline = $options['inline'];
214-
unset($options['columns'], $options['horizontal'], $options['inline']) ;
216+
unset($options['columns'], $options['horizontal'], $options['inline']);
215217
if ($this->horizontal) {
216-
$options = $this->addClass($options, 'form-horizontal') ;
218+
$options = $this->addClass($options, 'form-horizontal');
217219
}
218220
else if ($this->inline) {
219-
$options = $this->addClass($options, 'form-inline') ;
221+
$options = $this->addClass($options, 'form-inline');
220222
}
221-
$options['role'] = 'form' ;
222-
return parent::create($model, $options) ;
223+
$options['role'] = 'form';
224+
return parent::create($model, $options);
223225
}
224226

225227
/**
@@ -228,47 +230,50 @@ public function create($model = null, Array $options = array()) {
228230
*
229231
**/
230232
protected function _getColClass ($what, $offset = false) {
231-
if ($what === 'error' && isset($this->colSize['error']) && $this->colSize['error'] == 0) {
233+
if ($what === 'error'
234+
&& isset($this->colSize['error']) && $this->colSize['error'] == 0) {
232235
return $this->_getColClass('label', true).' '.$this->_getColClass('input');
233236
}
234237
if (isset($this->colSize[$what])) {
235-
return 'col-md-'.($offset ? 'offset-' : '').$this->colSize[$what] ;
238+
return 'col-md-'.($offset ? 'offset-' : '').$this->colSize[$what];
236239
}
237-
$classes = [] ;
240+
$classes = [];
238241
foreach ($this->colSize as $cl => $arr) {
239242
if (isset($arr[$what])) {
240-
$classes[] = 'col-'.$cl.'-'.($offset ? 'offset-' : '').$arr[$what] ;
243+
$classes[] = 'col-'.$cl.'-'.($offset ? 'offset-' : '').$arr[$what];
241244
}
242245
}
243-
return implode(' ', $classes) ;
246+
return implode(' ', $classes);
244247
}
245248

246249
protected function _wrapInputGroup ($addonOrButtons) {
247250
if ($addonOrButtons) {
248251
if (is_string($addonOrButtons)) {
249252
$addonOrButtons = $this->_makeIcon($addonOrButtons);
250253
$addonOrButtons = '<span class="input-group-'.
251-
($this->_matchButton($addonOrButtons) ? 'btn' : 'addon').'">'.$addonOrButtons.'</span>' ;
254+
($this->_matchButton($addonOrButtons) ?
255+
'btn' : 'addon').'">'.$addonOrButtons.'</span>';
252256
}
253257
else if ($addonOrButtons !== false) {
254-
$addonOrButtons = '<span class="input-group-btn">'.implode('', $addonOrButtons).'</span>' ;
258+
$addonOrButtons = '<span class="input-group-btn">'
259+
.implode('', $addonOrButtons).'</span>';
255260
}
256261
}
257-
return $addonOrButtons ;
262+
return $addonOrButtons;
258263
}
259264

260265
public function prepend ($input, $prepend) {
261266
$prepend = $this->_wrapInputGroup ($prepend);
262267
if ($input === null) {
263-
return '<div class="input-group">'.$prepend ;
268+
return '<div class="input-group">'.$prepend;
264269
}
265270
return $this->_wrap($input, $prepend, null);
266271
}
267272

268273
public function append ($input, $append) {
269274
$append = $this->_wrapInputGroup($append);
270275
if ($input === null) {
271-
return $append.'</div>' ;
276+
return $append.'</div>';
272277
}
273278
return $this->_wrap($input, null, $append);
274279
}
@@ -278,7 +283,7 @@ public function wrap ($input, $prepend, $append) {
278283
}
279284

280285
protected function _wrap ($input, $prepend, $append) {
281-
return '<div class="input-group">'.$prepend.$input.$append.'</div>' ;
286+
return '<div class="input-group">'.$prepend.$input.$append.'</div>';
282287
}
283288

284289
/**
@@ -316,20 +321,20 @@ public function input($fieldName, array $options = array()) {
316321
$help = $options['help'];
317322
unset($options['help']);
318323
if ($help) {
319-
$append .= '<p class="help-block">'.$help.'</p>' ;
324+
$append .= '<p class="help-block">'.$help.'</p>';
320325
}
321326

322327
$inline = $options['inline'];
323-
unset ($options['inline']) ;
328+
unset ($options['inline']);
324329

325330
if ($options['type'] === 'radio') {
326-
$options['templates'] = [] ;
331+
$options['templates'] = [];
327332
if ($inline) {
328333
$options['templates'] = [
329334
'label' => $this->templates('label'),
330335
'radioWrapper' => '{{label}}',
331336
'nestingLabel' => '{{hidden}}<label{{attrs}} class="radio-inline">{{input}}{{text}}</label>'
332-
] ;
337+
];
333338
}
334339
if ($this->horizontal) {
335340
$options['templates']['radioContainer'] = '<div class="form-group">{{content}}</div>';
@@ -344,29 +349,29 @@ public function input($fieldName, array $options = array()) {
344349
'append' => $append
345350
];
346351

347-
return parent::input($fieldName, $options) ;
352+
return parent::input($fieldName, $options);
348353
}
349354

350355
protected function _getDatetimeTemplate ($fields, $options) {
351-
$inputs = [] ;
356+
$inputs = [];
352357
foreach ($fields as $field => $in) {
353358
$in = isset($options[$field]) ? $options[$field] : $in;
354359
if ($in) {
355360
if ($field === 'timeFormat')
356-
$field = 'meridian' ; // Template uses "meridian" instead of timeFormat
361+
$field = 'meridian'; // Template uses "meridian" instead of timeFormat
357362
$inputs[$field] = '<div class="col-md-{{colsize}}">{{'.$field.'}}</div>';
358363
}
359364
}
360365
$tplt = $this->templates('dateWidget');
361366
$tplt = explode('}}{{', substr($tplt, 2, count($tplt) - 3));
362-
$html = '' ;
367+
$html = '';
363368
foreach ($tplt as $v) {
364369
if (isset($inputs[$v])) {
365-
$html .= $inputs[$v] ;
370+
$html .= $inputs[$v];
366371
}
367372
}
368373
return str_replace('{{colsize}}', round(12 / count($inputs)),
369-
'<div class="row">'.$html.'</div>') ;
374+
'<div class="row">'.$html.'</div>');
370375
}
371376

372377
/**
@@ -379,7 +384,8 @@ protected function _getDatetimeTemplate ($fields, $options) {
379384
*/
380385
public function file($fieldName, array $options = []) {
381386

382-
if (!$this->config('useCustomFileInput') || (isset($options['default']) && $options['default'])) {
387+
if (!$this->config('useCustomFileInput')
388+
|| (isset($options['default']) && $options['default'])) {
383389
return parent::file($fieldName, $options);
384390
}
385391

@@ -413,24 +419,28 @@ public function file($fieldName, array $options = []) {
413419
'onclick' => "document.getElementById('".$options['id']."').click();"
414420
]));
415421
$buttonLabel = $options['button-label'];
416-
unset($options['button-label']) ;
422+
unset($options['button-label']);
417423

418424
$fakeButton = $this->button($buttonLabel, array_merge($fakeButtonCustomOptions, [
419425
'type' => 'button',
420426
'onclick' => "document.getElementById('".$options['id']."').click();"
421427
]));
422-
return $fileInput.$this->Html->div('input-group', $this->Html->div('input-group-btn', $fakeButton).$fakeInput) ;
428+
return $fileInput.$this->Html->div('input-group',
429+
$this->Html->div('input-group-btn',
430+
$fakeButton).$fakeInput);
423431
}
424432

425433
/**
426-
* Returns a set of SELECT elements for a full datetime setup: day, month and year, and then time.
434+
* Returns a set of SELECT elements for a full datetime setup: day, month and year, and
435+
* then time.
427436
*
428437
* ### Date Options:
429438
*
430439
* - `empty` - If true, the empty select option is shown. If a string,
431440
* that string is displayed as the empty element.
432-
* - `value` | `default` The default value to be used by the input. A value in `$this->data`
433-
* matching the field name will override this value. If no default is provided `time()` will be used.
441+
* - `value` | `default` The default value to be used by the input. A value in
442+
* `$this->data matching the field name will override this value. If no default is
443+
* provided `time()` will be used.
434444
* - `monthNames` If false, 2 digit numbers will be used instead of text.
435445
* If an array, the given array will be used.
436446
* - `minYear` The lowest year to use in the year select
@@ -441,11 +451,13 @@ public function file($fieldName, array $options = []) {
441451
* ### Time options:
442452
*
443453
* - `empty` - If true, the empty select option is shown. If a string,
444-
* - `value` | `default` The default value to be used by the input. A value in `$this->data`
445-
* matching the field name will override this value. If no default is provided `time()` will be used.
454+
* - `value` | `default` The default value to be used by the input. A value in
455+
* `$this->data` matching the field name will override this value. If no default
456+
* is provided `time()` will be used.
446457
* - `timeFormat` The time format to use, either 12 or 24.
447458
* - `interval` The interval for the minutes select. Defaults to 1
448-
* - `round` - Set to `up` or `down` if you want to force rounding in either direction. Defaults to null.
459+
* - `round` - Set to `up` or `down` if you want to force rounding in either direction.
460+
* Defaults to null.
449461
* - `second` Set to true to enable seconds drop down.
450462
*
451463
* To control the order of inputs, and any elements/content between the inputs you
@@ -459,7 +471,9 @@ public function file($fieldName, array $options = []) {
459471
* @link http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-date-and-time-inputs
460472
*/
461473
public function dateTime($fieldName, array $options = []) {
462-
$fields = ['year' => true, 'month' => true, 'day' => true, 'hour' => true, 'minute' => true, 'second' => false, 'timeFormat' => false];
474+
$fields = ['year' => true, 'month' => true, 'day' => true,
475+
'hour' => true, 'minute' => true, 'second' => false,
476+
'timeFormat' => false];
463477
return $this->_wrapTemplates ([
464478
'dateWidget' => $this->_getDatetimeTemplate($fields, $options)
465479
], 'parent::dateTime', [$fieldName, $options]);
@@ -516,9 +530,9 @@ protected function _createButtonOptions (array $options = []) {
516530
$block = $options['bootstrap-block'];
517531
unset($options['bootstrap-block']);
518532
if ($block) {
519-
$options = $this->addClass($options, 'btn-block') ;
533+
$options = $this->addClass($options, 'btn-block');
520534
}
521-
return $options ;
535+
return $options;
522536
}
523537

524538
/**
@@ -532,7 +546,8 @@ protected function _createButtonOptions (array $options = []) {
532546
*
533547
*/
534548
public function button($title, array $options = []) {
535-
return $this->_easyIcon ('parent::button', $title, $this->_createButtonOptions($options));
549+
return $this->_easyIcon ('parent::button', $title,
550+
$this->_createButtonOptions($options));
536551
}
537552

538553
/**
@@ -551,12 +566,12 @@ public function buttonGroup ($buttons, array $options = []) {
551566
'vertical' => false
552567
];
553568
$vertical = $options['vertical'];
554-
unset($options['vertical']) ;
555-
$options = $this->addClass($options, 'btn-group') ;
569+
unset($options['vertical']);
570+
$options = $this->addClass($options, 'btn-group');
556571
if ($vertical) {
557-
$options = $this->addClass($options, 'btn-group-vertical') ;
572+
$options = $this->addClass($options, 'btn-group-vertical');
558573
}
559-
return $this->Html->tag('div', implode('', $buttons), $options) ;
574+
return $this->Html->tag('div', implode('', $buttons), $options);
560575
}
561576

562577
/**
@@ -568,8 +583,8 @@ public function buttonGroup ($buttons, array $options = []) {
568583
*
569584
**/
570585
public function buttonToolbar (array $buttonGroups, array $options = array()) {
571-
$options = $this->addClass($options, 'btn-toolbar') ;
572-
return $this->Html->tag('div', implode('', $buttonGroups), $options) ;
586+
$options = $this->addClass($options, 'btn-toolbar');
587+
return $this->Html->tag('div', implode('', $buttonGroups), $options);
573588
}
574589

575590
/**
@@ -589,9 +604,9 @@ public function buttonToolbar (array $buttonGroups, array $options = array()) {
589604
*/
590605
public function dropdownButton ($title, array $menu = [], array $options = []) {
591606

592-
$options['type'] = false ;
593-
$options['data-toggle'] = 'dropdown' ;
594-
$options = $this->addClass($options, "dropdown-toggle") ;
607+
$options['type'] = false;
608+
$options['data-toggle'] = 'dropdown';
609+
$options = $this->addClass($options, "dropdown-toggle");
595610

596611
return $this->buttonGroup([
597612
$this->button($title.' <span class="caret"></span>', $options),
@@ -612,7 +627,7 @@ public function dropdownButton ($title, array $menu = [], array $options = []) {
612627
*
613628
**/
614629
public function submit($caption = null, array $options = array()) {
615-
return parent::submit($caption, $this->_createButtonOptions($options)) ;
630+
return parent::submit($caption, $this->_createButtonOptions($options));
616631
}
617632

618633
/** SPECIAL FORM **/
@@ -660,24 +675,24 @@ public function searchForm ($model = null, $options = [], $inpOpts = [], $btnOpt
660675
'label' => $options['label']
661676
];
662677

663-
unset($options['id']) ;
664-
unset($options['label']) ;
665-
unset($options['placeholder']) ;
678+
unset($options['id']);
679+
unset($options['label']);
680+
unset($options['placeholder']);
666681

667682
$btnName = $options['button'];
668-
unset($options['button']) ;
683+
unset($options['button']);
669684

670685
$inpOpts['append'] = $this->button($btnName, $btnOpts);
671686

672687
$options['inline'] = (bool)$inpOpts['label'];
673688

674-
$output = '' ;
689+
$output = '';
675690

676-
$output .= $this->create($model, $options) ;
691+
$output .= $this->create($model, $options);
677692
$output .= $this->input($inpOpts['id'], $inpOpts);
678-
$output .= $this->end() ;
693+
$output .= $this->end();
679694

680-
return $output ;
695+
return $output;
681696
}
682697

683698
}

0 commit comments

Comments
 (0)