Completed
Push — master ( ea460f...fd0685 )
by Cheren
02:34
created

HelperTrait::_createIconAttr()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
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
22
/**
23
 * Class HelperTrait
24
 *
25
 * @package Core\View\Helper\Traits
26
 */
27
trait HelperTrait
28
{
29
30
    /**
31
     * Adds the given class to the element options
32
     *
33
     * @param array $options Array options/attributes to add a class to
34
     * @param string $class The class name being added.
35
     * @param string $key the key to use for class.
36
     * @return array Array of options with $key set.
37
     */
38
    protected function _addClass(array $options = [], $class = null, $key = 'class')
39
    {
40
        if (isset($options[$key]) && Str::trim($options[$key])) {
41
            $options[$key] .= ' ' . $class;
42
        } else {
43
            $options[$key] = $class;
44
        }
45
        return $options;
46
    }
47
48
    /**
49
     * Get class with union prefix.
50
     *
51
     * @param string $class
52
     * @return string
53
     */
54
    protected function _class($class = 'cms')
55
    {
56
        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...
57
    }
58
59
    /**
60
     * Create icon attributes.
61
     *
62
     * @param array $options
63
     * @return array
64
     */
65
    protected function _createIconAttr(array $options = [])
66
    {
67
        $iconOptions = ['class' => ''];
68
        if (isset($options['icon'])) {
69
            if (isset($options['iconClass'])) {
70
                $iconOptions = $this->_addClass($iconOptions, $options['iconClass']);
71
                unset($options['iconClass']);
72
            }
73
74
            list ($options, $iconOptions) = $this->_setIconOptions($options, $iconOptions);
75
        }
76
77
        return [$options, $iconOptions];
78
    }
79
80
    /**
81
     * Create and get button classes.
82
     *
83
     * @param array $options
84
     * @return array
85
     */
86
    protected function _getBtnClass(array $options = [])
87
    {
88
        if (isset($options['button'])) {
89
90
            $button = $options['button'];
91
            unset($options['button']);
92
93
            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...
94
                return (array) call_user_func($this->config('prepareBtnClass'), $this, $options);
95
            }
96
97
            $options = $this->_setBtnClass($button, $options);
98
        }
99
100
        return $options;
101
    }
102
103
    /**
104
     * Create and get tooltip attributes.
105
     *
106
     * @param array $options
107
     * @param string $toggle
108
     * @return array
109
     */
110
    protected function _getToolTipAttr(array $options = [], $toggle = 'tooltip')
111
    {
112
        if (isset($options['tooltip'])) {
113
114
            $tooltip = $options['tooltip'];
115
            unset($options['tooltip']);
116
117
            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...
118
                return (array) call_user_func($this->config('prepareTooltip'), $this, $options);
119
            }
120
121
            $_options = [
122
                'data-toggle'    => $toggle,
123
                'data-placement' => 'top',
124
            ];
125
126
            if (isset($options['tooltipPos'])) {
127
                $_options['data-placement'] = (string) $options['tooltipPos'];
128
                unset($options['tooltipPos']);
129
            }
130
131
            $options = $this->_setTooltipTitle($tooltip, $options);
132
133
            return Hash::merge($_options, $options);
134
        }
135
136
        return $options;
137
    }
138
139
    /**
140
     * Setup button classes by options.
141
     *
142
     * @param string $button
143
     * @param array $options
144
     * @return array
145
     */
146
    protected function _setBtnClass($button, array $options = [])
147
    {
148
        if ($button !== true) {
149
            $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...
150
            foreach ((array) $button as $button) {
151
                $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...
152
            }
153
            $options = $this->_addClass($options, implode(' ', $classes));
154
        }
155
156
        return $options;
157
    }
158
159
    /**
160
     * Setup icon options.
161
     *
162
     * @param array $options
163
     * @param array $iconOptions
164
     * @return array
165
     */
166
    protected function _setIconOptions(array $options = [], array $iconOptions = [])
167
    {
168
        $icon = $options['icon'];
169
        if (isset($options['iconInline'])) {
170
            $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...
171
            $options = $this->_addClass($options, implode(' ', [
172
                $this->_class('icon'),
173
                $iconPrefix,
174
                $iconPrefix . '-' . $icon
175
            ]));
176
177
            unset($options['icon']);
178
        } else {
179
            $options['escape'] = false;
180
            $iconOptions['createIcon'] = true;
181
        }
182
183
        return [$options, $iconOptions];
184
    }
185
186
    /**
187
     * Setup tooltip title by options.
188
     *
189
     * @param string $tooltip
190
     * @param array $options
191
     * @return array
192
     */
193
    protected function _setTooltipTitle($tooltip, array $options = [])
194
    {
195
        if ($tooltip === true && !isset($options['title'])) {
196
            $options['title'] = strip_tags($options['label']);
197
        }
198
199
        if (is_string($tooltip)) {
200
            $options['title'] = $tooltip;
201
        }
202
203
        return $options;
204
    }
205
}
206