Skip to content

Commit 9ebfb24

Browse files
committed
Remove _extractOption.
1 parent 8c050e2 commit 9ebfb24

File tree

4 files changed

+125
-97
lines changed

4 files changed

+125
-97
lines changed

src/View/Helper/BootstrapFormHelper.php

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,15 @@ protected function _inputContainerTemplate($options) {
214214
*
215215
**/
216216
public function create($model = null, Array $options = array()) {
217-
if (isset($options['cols'])) {
218-
$this->colSize = $options['cols'] ;
219-
unset($options['cols']) ;
220-
}
221-
else {
222-
$this->colSize = $this->config('columns') ;
223-
}
224-
$this->horizontal = $this->_extractOption('horizontal', $options, false);
225-
unset($options['horizontal']);
226-
$this->inline = $this->_extractOption('inline', $options, false) ;
227-
unset($options['inline']) ;
217+
$options += [
218+
'columns' => $this->config('columns'),
219+
'horizontal' => false,
220+
'inline' => false
221+
];
222+
$this->colSize = $options['columns'];
223+
$this->horizontal = $options['horizontal'];
224+
$this->inline = $options['inline'];
225+
unset($options['columns'], $options['horizontal'], $options['inline']) ;
228226
if ($this->horizontal) {
229227
$options = $this->addClass($options, 'form-horizontal') ;
230228
}
@@ -363,7 +361,8 @@ public function input($fieldName, array $options = array()) {
363361
protected function _getDatetimeTemplate ($fields, $options) {
364362
$inputs = [] ;
365363
foreach ($fields as $field => $in) {
366-
if ($this->_extractOption($field, $options, $in)) {
364+
$in = isset($options[$field]) ? $options[$field] : $in;
365+
if ($in) {
367366
if ($field === 'timeFormat')
368367
$field = 'meridian' ; // Template uses "meridian" instead of timeFormat
369368
$inputs[$field] = '<div class="col-md-{{colsize}}">{{'.$field.'}}</div>';
@@ -390,23 +389,28 @@ protected function _getDatetimeTemplate ($fields, $options) {
390389
* @link http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-file-inputs
391390
*/
392391
public function file($fieldName, array $options = []) {
392+
393393
if (!$this->config('useCustomFileInput') || (isset($options['default']) && $options['default'])) {
394394
return parent::file($fieldName, $options);
395395
}
396396

397-
$fakeInputCustomOptions = $this->_extractOption('_input', $options, []);
398-
unset($options['_input']);
399-
$fakeButtonCustomOptions = $this->_extractOption('_button', $options, []);
400-
unset($options['_button']);
397+
$options += [
398+
'_input' => [],
399+
'_button' => [],
400+
'id' => $fieldName,
401+
'secure' => true,
402+
'count-label' => __('files selected'),
403+
'button-label' => __('Choose File')
404+
];
401405

402-
if (!isset($options['id'])) {
403-
$options['id'] = $fieldName;
404-
}
406+
$fakeInputCustomOptions = $options['_input'];
407+
$fakeButtonCustomOptions = $options['_button'];
408+
unset($options['_input'], $options['_button']);
405409

406410
$options += ['secure' => true];
407411
$options = $this->_initInputField($fieldName, $options);
408412
unset($options['type']);
409-
$countLabel = $this->_extractOption('count-label', $options, __('files selected'));
413+
$countLabel = $options['count-label'];
410414
unset($options['count-label']);
411415
$fileInput = $this->widget('file', array_merge($options, [
412416
'style' => 'display: none;',
@@ -419,7 +423,7 @@ public function file($fieldName, array $options = []) {
419423
'id' => $options['id'].'-input',
420424
'onclick' => "document.getElementById('".$options['id']."').click();"
421425
]));
422-
$buttonLabel = $this->_extractOption('button-label', $options, __('Choose File'));
426+
$buttonLabel = $options['button-label'];
423427
unset($options['button-label']) ;
424428

425429
$fakeButton = $this->button($buttonLabel, array_merge($fakeButtonCustomOptions, [
@@ -515,9 +519,12 @@ public function date($fieldName, array $options = []) {
515519
* Create & return a Cakephp options array from the $options specified.
516520
*
517521
*/
518-
protected function _createButtonOptions (array $options = array()) {
522+
protected function _createButtonOptions (array $options = []) {
523+
$options += [
524+
'bootstrap-block' => false
525+
];
519526
$options = $this->_addButtonClasses($options);
520-
$block = $this->_extractOption('bootstrap-block', $options, false) ;
527+
$block = $options['bootstrap-block'];
521528
unset($options['bootstrap-block']);
522529
if ($block) {
523530
$options = $this->addClass($options, 'btn-block') ;
@@ -550,8 +557,11 @@ public function button($title, array $options = []) {
550557
* - vertical true/false
551558
*
552559
**/
553-
public function buttonGroup ($buttons, array $options = array()) {
554-
$vertical = $this->_extractOption('vertical', $options, false) ;
560+
public function buttonGroup ($buttons, array $options = []) {
561+
$options += [
562+
'vertical' => false
563+
];
564+
$vertical = $options['vertical'];
555565
unset($options['vertical']) ;
556566
$options = $this->addClass($options, 'btn-group') ;
557567
if ($vertical) {

src/View/Helper/BootstrapHtmlHelper.php

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ public function label ($text, $type = 'default', $options = []) {
122122
else if (is_array($type)) {
123123
$options = $type ;
124124
}
125-
$type = $this->_extractOption('type', $options, 'default');
125+
$options += [
126+
'type' => 'default'
127+
];
128+
$type = $options['type'];
126129
unset ($options['type']) ;
127130
$options = $this->addClass($options, 'label') ;
128131
$options = $this->addClass($options, 'label-'.$type) ;
@@ -182,9 +185,12 @@ public function alert ($text, $type = 'warning', $options = []) {
182185
else if (is_array($type)) {
183186
$options = $type ;
184187
}
188+
$options += [
189+
'type' => 'warning'
190+
];
185191
$button = '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">'
186192
.'&times;</button>' ;
187-
$type = $this->_extractOption('type', $options, 'warning');
193+
$type = $options['type'];
188194
unset($options['type']) ;
189195
$options = $this->addClass($options, 'alert') ;
190196
if ($type) {
@@ -246,45 +252,55 @@ public function tooltip($text, $tooltip, $options = []) {
246252
*
247253
**/
248254
public function progress ($widths, $options = []) {
249-
$striped = $this->_extractOption('striped', $options, false)
250-
|| in_array('striped', $options);
251-
unset($options['striped']) ;
252-
$active = $this->_extractOption('active', $options, false) || in_array('active', $options);
253-
unset($options['active']) ;
255+
$options += [
256+
'striped' => false,
257+
'active' => false
258+
];
259+
$striped = $options['striped'];
260+
$active = $options['active'];
261+
unset($options['active'], $options['striped']) ;
254262
$bars = '' ;
255263
if (is_array($widths)) {
256-
foreach ($widths as $w) {
257-
$type = $this->_extractOption('type', $w, 'primary');
258-
$class = 'progress-bar progress-bar-'.$type ;
259-
$min = $this->_extractOption('min', $w, 0);
260-
$max = $this->_extractOption('max', $w, 100);
261-
$display = $this->_extractOption('display', $w, false);
262-
$bars .= $this->div($class, $display ? $w['width'].'%' : '', array(
263-
'aria-valuenow' => $w['width'],
264-
'aria-valuemin' => $min,
265-
'aria-valuemax' => $max,
266-
'role' => 'progressbar',
267-
'style' => 'width: '.$w['width'].'%;'
268-
)) ;
264+
foreach ($widths as $width) {
265+
$width += [
266+
'type' => 'primary',
267+
'min' => 0,
268+
'max' => 100,
269+
'display' => false
270+
];
271+
$class = 'progress-bar progress-bar-'.$width['type'];
272+
$bars .= $this->div($class,
273+
$width['display'] ? $width['width'].'%' : '',
274+
[
275+
'aria-valuenow' => $width['width'],
276+
'aria-valuemin' => $width['min'],
277+
'aria-valuemax' => $width['max'],
278+
'role' => 'progressbar',
279+
'style' => 'width: '.$width['width'].'%;'
280+
]
281+
);
269282
}
270283
}
271284
else {
272-
$type = $this->_extractOption('type', $options, 'primary');
273-
unset($options['type']) ;
274-
$class = 'progress-bar progress-bar-'.$type ;
275-
$min = $this->_extractOption('min', $options, 0);
276-
unset ($options['min']) ;
277-
$max = $this->_extractOption('max', $options, 100);
278-
unset ($options['max']) ;
279-
$display = $this->_extractOption('display', $options, false);
280-
unset ($options['display']) ;
281-
$bars = $this->div($class, $display ? $widths.'%' : '', array(
282-
'aria-valuenow' => $widths,
283-
'aria-valuemin' => $min,
284-
'aria-valuemax' => $max,
285-
'role' => 'progressbar',
286-
'style' => 'width: '.$widths.'%;'
287-
)) ;
285+
$options += [
286+
'type' => 'primary',
287+
'min' => 0,
288+
'max' => 100,
289+
'display' => false
290+
];
291+
$class = 'progress-bar progress-bar-'.$options['type'];
292+
$bars = $this->div($class,
293+
$options['display'] ? $widths.'%' : '',
294+
[
295+
'aria-valuenow' => $widths,
296+
'aria-valuemin' => $options['min'],
297+
'aria-valuemax' => $options['max'],
298+
'role' => 'progressbar',
299+
'style' => 'width: '.$widths.'%;'
300+
]
301+
);
302+
unset($options['type'], $options['min'],
303+
$options['max'], $options['display']);
288304
}
289305
$options = $this->addClass($options, 'progress') ;
290306
if ($active) {

src/View/Helper/BootstrapNavbarHelper.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,23 @@ public function addClass(array $options = [], $class = null, $key = 'class') {
106106
*
107107
**/
108108
public function create ($brand, $options = []) {
109-
$this->_fixed = $this->_extractOption('fixed', $options, false) ;
110-
unset($options['fixed']) ;
111-
$this->_responsive = $this->_extractOption('responsive', $options, false) ;
112-
unset($options['responsive']) ;
113-
$this->_static = $this->_extractOption('static', $options, false) ;
114-
unset($options['static']) ;
115-
$this->_inverse = $this->_extractOption('inverse', $options, false) ;
116-
unset($options['inverse']) ;
117-
$this->_fluid = $this->_extractOption('fluid', $options, false);
118-
unset($options['fluid']);
109+
110+
$options += [
111+
'fixed' => false,
112+
'responsive' => false,
113+
'static' => false,
114+
'inverse' => false,
115+
'fluid' => false
116+
];
117+
118+
$this->_fixed = $options['fixed'];
119+
$this->_responsive = $options['responsive'];
120+
$this->_static = $options['static'];
121+
$this->_inverse = $options['inverse'];
122+
$this->_fluid = $options['fluid'];
123+
unset($options['fixed'], $options['responsive'],
124+
$options['fluid'], $options['static'],
125+
$options['inverse']);
119126

120127
/** Generate options for outer div. **/
121128
$options = $this->addClass($options, 'navbar navbar-default') ;
@@ -157,9 +164,11 @@ public function create ($brand, $options = []) {
157164
]) ;
158165
}
159166
else if (is_array($brand) && array_key_exists('url', $brand)) {
160-
$brandOptions = $this->_extractOption ('options', $brand, []) ;
161-
$brandOptions = $this->addClass ($brandOptions, 'navbar-brand') ;
162-
$brand = $this->Html->link ($brand['name'], $brand['url'], $brandOptions) ;
167+
$brand += [
168+
'options' => []
169+
];
170+
$brand['options'] = $this->addClass ($brand['options'], 'navbar-brand') ;
171+
$brand = $this->Html->link ($brand['name'], $brand['url'], $brand['options']) ;
163172
}
164173
$rightOpen = $this->Html->tag('div', $toggleButton.$brand,
165174
['class' => 'navbar-header']).$rightOpen ;
@@ -244,7 +253,11 @@ public function header ($name, array $options = []) {
244253
*
245254
**/
246255
public function text ($text, $options = []) {
247-
$tag = $this->_extractOption ('tag', $options, 'p') ;
256+
$options += [
257+
'tag' => 'p'
258+
];
259+
$tag = $options['tag'];
260+
unset($options['tag']);
248261
$options = $this->addClass ($options, 'navbar-text') ;
249262
$text = preg_replace_callback ('/<a([^>]*)?>([^<]*)?<\/a>/i', function ($matches) {
250263
$attrs = preg_replace_callback ('/class="(.*)?"/', function ($m) {
@@ -269,7 +282,10 @@ public function text ($text, $options = []) {
269282
*
270283
**/
271284
public function searchForm ($model = null, $options = []) {
272-
$align = $this->_extractOption ('align', $options, 'left') ;
285+
$options += [
286+
'align' => false
287+
];
288+
$align = $options['align'];
273289
unset ($options['align']) ;
274290
$options = $this->addClass($options, ['navbar-form', 'navbar-'.$align]) ;
275291
return $this->Form->searchForm($model, $options) ;

src/View/Helper/BootstrapTrait.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ public function addClass(array $options = [], $class = null, $key = 'class') {
6868
*
6969
*/
7070
protected function _addButtonClasses ($options) {
71-
$type = $this->_extractOption('bootstrap-type', $options, $this->config('buttons.type'));
72-
$size = $this->_extractOption('bootstrap-size', $options, false);
73-
unset($options['bootstrap-size']) ;
74-
unset($options['bootstrap-type']) ;
71+
$options += [
72+
'bootstrap-type' => $this->config('buttons.type'),
73+
'bootstrap-size' => false
74+
];
75+
$type = $options['bootstrap-type'];
76+
$size = $options['bootstrap-size'];
77+
unset($options['bootstrap-type'], $options['bootstrap-size']) ;
7578
$options = $this->addClass($options, 'btn') ;
7679
if (in_array($type, $this->buttonTypes)) {
7780
$options = $this->addClass($options, 'btn-'.$type) ;
@@ -82,23 +85,6 @@ protected function _addButtonClasses ($options) {
8285
return $options ;
8386
}
8487

85-
/**
86-
* Extract options from $options, returning $default if $key is not found.
87-
*
88-
* @param $key The key to search for.
89-
* @param $options The array from which to extract the value.
90-
* @param $default The default value returned if the key is not found.
91-
*
92-
* @return mixed $options[$key] if $key is in $options, otherwize $default.
93-
*
94-
**/
95-
protected function _extractOption ($key, $options, $default = null) {
96-
if (isset($options[$key])) {
97-
return $options[$key] ;
98-
}
99-
return $default ;
100-
}
101-
10288
/**
10389
*
10490
* Check weither the specified array is associative or not.

0 commit comments

Comments
 (0)