Completed
Push — master ( e16c78...ea460f )
by Cheren
02:25
created

HelperTrait::_addClass()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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