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