Skip to content

Commit 44ec800

Browse files
committed
Create BootstrapTrait trait to avoid code duplication for various utility methods.
1 parent 764ddbd commit 44ec800

File tree

6 files changed

+134
-123
lines changed

6 files changed

+134
-123
lines changed

src/View/Helper/BootstrapFormHelper.php

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
class BootstrapFormHelper extends FormHelper {
2828

29+
use BootstrapTrait ;
30+
2931
public $helpers = [
3032
'Html',
3133
'Url',
@@ -151,57 +153,6 @@ public function __construct (\Cake\View\View $view, array $config = []) {
151153
parent::__construct($view, $config);
152154
}
153155

154-
/**
155-
* Adds the given class to the element options
156-
*
157-
* @param array $options Array options/attributes to add a class to
158-
* @param string|array $class The class name being added.
159-
* @param string $key the key to use for class.
160-
* @return array Array of options with $key set.
161-
*/
162-
public function addClass(array $options = [], $class = null, $key = 'class') {
163-
if (is_array($class)) {
164-
$class = implode(' ', array_unique(array_map('trim', $class))) ;
165-
}
166-
if (isset($options[$key])) {
167-
$optClass = $options[$key];
168-
if (is_array($optClass)) {
169-
$optClass = trim(implode(' ', array_unique(array_map('trim', $optClass))));
170-
}
171-
}
172-
if (isset($optClass) && $optClass) {
173-
$options[$key] = $optClass.' '.$class ;
174-
}
175-
else {
176-
$options[$key] = $class ;
177-
}
178-
return $options ;
179-
}
180-
181-
/**
182-
*
183-
* Add classes to options according to values of bootstrap-type and bootstrap-size for button.
184-
*
185-
* @param $options The initial options with bootstrap-type and/or bootstrat-size values
186-
*
187-
* @return The new options with class values (btn, and btn-* according to initial options)
188-
*
189-
*/
190-
protected function _addButtonClasses ($options) {
191-
$type = $this->_extractOption('bootstrap-type', $options, $this->_defaultButtonType);
192-
$size = $this->_extractOption('bootstrap-size', $options, FALSE);
193-
unset($options['bootstrap-size']) ;
194-
unset($options['bootstrap-type']) ;
195-
$options = $this->addClass($options, 'btn') ;
196-
if (in_array($type, $this->buttonTypes)) {
197-
$options = $this->addClass($options, 'btn-'.$type) ;
198-
}
199-
if (in_array($size, $this->buttonSizes)) {
200-
$options = $this->addClass($options, 'btn-'.$size) ;
201-
}
202-
return $options ;
203-
}
204-
205156
/**
206157
*
207158
* Replace the templates with the ones specified by newTemplates, call the specified function

src/View/Helper/BootstrapHtmlHelper.php

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
class BootstrapHtmlHelper extends HtmlHelper {
2828

29+
use BootstrapTrait ;
30+
2931
/**
3032
* Use font awesome icons instead of glyphicons.
3133
*
@@ -40,59 +42,6 @@ public function __construct (\Cake\View\View $view, array $config = []) {
4042
parent::__construct($view, $config);
4143
}
4244

43-
protected function _extractOption ($key, $options, $default = null) {
44-
if (isset($options[$key])) {
45-
return $options[$key] ;
46-
}
47-
return $default ;
48-
}
49-
50-
/**
51-
* Adds the given class to the element options
52-
*
53-
* @param array $options Array options/attributes to add a class to
54-
* @param string|array $class The class name being added.
55-
* @param string $key the key to use for class.
56-
* @return array Array of options with $key set.
57-
**/
58-
public function addClass(array $options = [], $class = null, $key = 'class') {
59-
if (is_array($class)) {
60-
$class = implode(' ', array_unique(array_map('trim', $class))) ;
61-
}
62-
if (isset($options[$key])) {
63-
$optClass = $options[$key];
64-
if (is_array($optClass)) {
65-
$optClass = trim(implode(' ', array_unique(array_map('trim', $optClass))));
66-
}
67-
}
68-
if (isset($optClass) && $optClass) {
69-
$options[$key] = $optClass.' '.$class ;
70-
}
71-
else {
72-
$options[$key] = $class ;
73-
}
74-
return $options ;
75-
}
76-
77-
/**
78-
*
79-
* Check type values in $options, returning null if no option is found or if
80-
* option is not in $avail.
81-
* If type == $default, $default is returned (even if it is not in $avail).
82-
*
83-
**/
84-
protected function _extractType ($options, $key = 'type', $default = 'info',
85-
$avail = array('info', 'success', 'warning', 'error')) {
86-
$type = $this->_extractOption($key, $options, $default) ;
87-
if ($default !== false && $type == $default) {
88-
return $default ;
89-
}
90-
if (!in_array($type, $avail)) {
91-
return null ;
92-
}
93-
return $type ;
94-
}
95-
9645
/**
9746
*
9847
* Create a glyphicon or font awesome icon depending on $this->_useFontAwesome.

src/View/Helper/BootstrapModalHelper.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,12 @@
2626

2727
class BootstrapModalHelper extends Helper {
2828

29+
use BootstrapTrait ;
30+
2931
public $helpers = ['Html'];
3032

3133
public $current = NULL ;
3234

33-
protected function _extractOption ($key, $options, $default = null) {
34-
if (isset($options[$key])) {
35-
return $options[$key] ;
36-
}
37-
return $default ;
38-
}
39-
4035
/**
4136
*
4237
* Create a Twitter Bootstrap like modal.

src/View/Helper/BootstrapNavbarHelper.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
class BootstrapNavbarHelper extends Helper {
2929

30+
use BootstrapTrait ;
31+
3032
public $helpers = [
3133
'Html',
3234
'Form' => [
@@ -322,18 +324,6 @@ public function end () {
322324
}
323325
return $res ;
324326
}
325-
326-
/**
327-
*
328-
* Extract options from $options, returning $default if $key is not found.
329-
*
330-
**/
331-
protected function _extractOption ($key, $options, $default = null) {
332-
if (isset($options[$key])) {
333-
return $options[$key] ;
334-
}
335-
return $default ;
336-
}
337327

338328
}
339329

src/View/Helper/BootstrapPaginatorHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
class BootstrapPaginatorHelper extends PaginatorHelper {
2828

29+
use BootstrapTrait ;
2930

3031
public function __construct ($view, $config = []) {
3132
$this->templates([

src/View/Helper/BootstrapTrait.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
/**
4+
* Bootstrap Trait
5+
*
6+
*
7+
* PHP 5
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
*
16+
* @copyright Copyright (c) Mikaël Capelle (http://mikael-capelle.fr)
17+
* @link http://mikael-capelle.fr
18+
* @package app.View.Helper
19+
* @since Apache v2
20+
* @license http://www.apache.org/licenses/LICENSE-2.0
21+
*/
22+
23+
namespace Bootstrap3\View\Helper;
24+
25+
trait BootstrapTrait {
26+
27+
/**
28+
* Adds the given class to the element options
29+
*
30+
* @param array $options Array options/attributes to add a class to
31+
* @param string|array $class The class name being added.
32+
* @param string $key the key to use for class.
33+
*
34+
* @return array Array of options with $key set.
35+
*/
36+
public function addClass(array $options = [], $class = null, $key = 'class') {
37+
if (is_array($class)) {
38+
$class = implode(' ', array_unique(array_map('trim', $class))) ;
39+
}
40+
if (isset($options[$key])) {
41+
$optClass = $options[$key];
42+
if (is_array($optClass)) {
43+
$optClass = trim(implode(' ', array_unique(array_map('trim', $optClass))));
44+
}
45+
}
46+
if (isset($optClass) && $optClass) {
47+
$options[$key] = $optClass.' '.$class ;
48+
}
49+
else {
50+
$options[$key] = $class ;
51+
}
52+
return $options ;
53+
}
54+
55+
/**
56+
*
57+
* Add classes to options according to values of bootstrap-type and bootstrap-size for button.
58+
*
59+
* @param $options The initial options with bootstrap-type and/or bootstrat-size values
60+
*
61+
* @return The new options with class values (btn, and btn-* according to initial options)
62+
*
63+
*/
64+
protected function _addButtonClasses ($options) {
65+
$type = $this->_extractOption('bootstrap-type', $options, $this->_defaultButtonType);
66+
$size = $this->_extractOption('bootstrap-size', $options, FALSE);
67+
unset($options['bootstrap-size']) ;
68+
unset($options['bootstrap-type']) ;
69+
$options = $this->addClass($options, 'btn') ;
70+
if (in_array($type, $this->buttonTypes)) {
71+
$options = $this->addClass($options, 'btn-'.$type) ;
72+
}
73+
if (in_array($size, $this->buttonSizes)) {
74+
$options = $this->addClass($options, 'btn-'.$size) ;
75+
}
76+
return $options ;
77+
}
78+
79+
/**
80+
*
81+
* Extract options from $options, returning $default if $key is not found.
82+
*
83+
* @param $key The key to search for.
84+
* @param $options The array from which to extract the value.
85+
* @param $default The default value returned if the key is not found.
86+
*
87+
* @return mixed $options[$key] if $key is in $options, otherwize $default.
88+
*
89+
**/
90+
protected function _extractOption ($key, $options, $default = null) {
91+
if (isset($options[$key])) {
92+
return $options[$key] ;
93+
}
94+
return $default ;
95+
}
96+
97+
/**
98+
*
99+
* Check type values in $options, returning null if no option is found or if
100+
* option is not in $avail.
101+
* If type == $default, $default is returned (even if it is not in $avail).
102+
*
103+
* @param $options The array from which to extract the type.
104+
* @param $key The key of the value.
105+
* @param $default The default value if the key is not present or if the value is not correct.
106+
* @param $avail An array of possible values.
107+
*
108+
* @return mixed
109+
*
110+
**/
111+
protected function _extractType ($options, $key = 'type', $default = 'info',
112+
$avail = array('info', 'success', 'warning', 'error')) {
113+
$type = $this->_extractOption($key, $options, $default) ;
114+
if ($default !== false && $type == $default) {
115+
return $default ;
116+
}
117+
if (!in_array($type, $avail)) {
118+
return null ;
119+
}
120+
return $type ;
121+
}
122+
123+
}
124+
125+
?>

0 commit comments

Comments
 (0)