Completed
Push — master ( 51a3e9...1c8cca )
by Cheren
07:18
created

HelperTrait::_addClass()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 6
nc 2
nop 3
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\View\Helper\Traits;
17
18
use JBZoo\Utils\Str;
19
use Cake\Utility\Hash;
20
use Cake\Core\Configure;
21
use Core\View\Helper\HtmlHelper;
22
23
/**
24
 * Class HelperTrait
25
 *
26
 * @package Core\View\Helper\Traits
27
 */
28
trait HelperTrait
29
{
30
31
    /**
32
     * Adds the given class to the element options
33
     *
34
     * @param array $options Array options/attributes to add a class to
35
     * @param string $class The class name being added.
36
     * @param string $key the key to use for class.
37
     * @return array Array of options with $key set.
38
     */
39
    protected function _addClass(array $options = [], $class = null, $key = 'class')
40
    {
41
        if (isset($options[$key]) && Str::trim($options[$key])) {
42
            $options[$key] .= ' ' . $class;
43
        } else {
44
            $options[$key] = $class;
45
        }
46
        return $options;
47
    }
48
49
    /**
50
     * Get class with union prefix.
51
     *
52
     * @param string $class
53
     * @return string
54
     */
55
    protected function _class($class = 'cms')
56
    {
57
        return $this->_configRead('classPrefix') . '-' . Str::trim(Str::slug($class));
0 ignored issues
show
Bug introduced by
It seems like _configRead() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
58
    }
59
60
    /**
61
     * Create current icon.
62
     *
63
     * @param HtmlHelper $html
64
     * @param string|int $title
65
     * @param array $options
66
     * @return array
67
     */
68
    protected function _createIcon(HtmlHelper $html, $title, array $options = [])
69
    {
70
        list($options, $iconOptions) = $this->_createIconAttr($options);
71
        if (isset($iconOptions['createIcon'])) {
72
            unset($iconOptions['createIcon']);
73
            $title = $html->icon($options['icon'], $iconOptions) . PHP_EOL . $title;
74
            unset($options['icon']);
75
        }
76
77
        if (isset($options['iconInline'])) {
78
            unset($options['iconInline']);
79
        }
80
81
        return [$title, $options];
82
    }
83
84
    /**
85
     * Create icon attributes.
86
     *
87
     * @param array $options
88
     * @return array
89
     */
90
    protected function _createIconAttr(array $options = [])
91
    {
92
        $iconOptions = ['class' => ''];
93
        if (isset($options['icon'])) {
94
            if (isset($options['iconClass'])) {
95
                $iconOptions = $this->_addClass($iconOptions, $options['iconClass']);
96
                unset($options['iconClass']);
97
            }
98
99
            list ($options, $iconOptions) = $this->_setIconOptions($options, $iconOptions);
100
        }
101
102
        return [$options, $iconOptions];
103
    }
104
105
    /**
106
     * Create and get button classes.
107
     *
108
     * @param array $options
109
     * @return array
110
     */
111
    protected function _getBtnClass(array $options = [])
112
    {
113
        if (isset($options['button'])) {
114
115
            $button = $options['button'];
116
            unset($options['button']);
117
118
            if (is_callable($this->config('prepareBtnClass'))) {
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119
                return (array) call_user_func($this->config('prepareBtnClass'), $this, $options, $button);
120
            }
121
122
            $options = $this->_setBtnClass($button, $options);
123
        }
124
125
        return $options;
126
    }
127
128
    /**
129
     * Create and get tooltip attributes.
130
     *
131
     * @param array $options
132
     * @param string $toggle
133
     * @return array
134
     */
135
    protected function _getToolTipAttr(array $options = [], $toggle = 'tooltip')
136
    {
137
        if (isset($options['tooltip'])) {
138
139
            $tooltip = $options['tooltip'];
140
            unset($options['tooltip']);
141
142
            if (is_callable($this->config('prepareTooltip'))) {
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
143
                return (array) call_user_func($this->config('prepareTooltip'), $this, $options, $tooltip);
144
            }
145
146
            $_options = [
147
                'data-toggle'    => $toggle,
148
                'data-placement' => 'top',
149
            ];
150
151
            if (isset($options['tooltipPos'])) {
152
                $_options['data-placement'] = (string) $options['tooltipPos'];
153
                unset($options['tooltipPos']);
154
            }
155
156
            $options = $this->_setTooltipTitle($tooltip, $options);
157
158
            return Hash::merge($_options, $options);
159
        }
160
161
        return $options;
162
    }
163
164
    /**
165
     * Setup button classes by options.
166
     *
167
     * @param string $button
168
     * @param array $options
169
     * @return array
170
     */
171
    protected function _setBtnClass($button, array $options = [])
172
    {
173
        if ($button !== true) {
174
            $classes = [$this->_configRead('btnPref')];
0 ignored issues
show
Bug introduced by
It seems like _configRead() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
175
            foreach ((array) $button as $button) {
176
                $classes[] = $this->_configRead('btnPref') . '-' . $button;
0 ignored issues
show
Bug introduced by
It seems like _configRead() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
177
            }
178
            $options = $this->_addClass($options, implode(' ', $classes));
179
        }
180
181
        return $options;
182
    }
183
184
    /**
185
     * Setup icon options.
186
     *
187
     * @param array $options
188
     * @param array $iconOptions
189
     * @return array
190
     */
191
    protected function _setIconOptions(array $options = [], array $iconOptions = [])
192
    {
193
        $icon = $options['icon'];
194
        if (isset($options['iconInline'])) {
195
            $iconPrefix = $this->_configRead('iconPref');
0 ignored issues
show
Bug introduced by
It seems like _configRead() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
196
            $options = $this->_addClass($options, implode(' ', [
197
                $this->_class('icon'),
198
                $iconPrefix,
199
                $iconPrefix . '-' . $icon
200
            ]));
201
202
            unset($options['icon']);
203
        } else {
204
            $options['escape'] = false;
205
            $iconOptions['createIcon'] = true;
206
        }
207
208
        return [$options, $iconOptions];
209
    }
210
211
    /**
212
     * Setup tooltip title by options.
213
     *
214
     * @param string $tooltip
215
     * @param array $options
216
     * @return array
217
     */
218
    protected function _setTooltipTitle($tooltip, array $options = [])
219
    {
220
        if ($tooltip === true && !isset($options['title'])) {
221
            $options['title'] = strip_tags($options['label']);
222
        }
223
224
        if (is_string($tooltip)) {
225
            $options['title'] = $tooltip;
226
        }
227
228
        return $options;
229
    }
230
}
231