Completed
Push — master ( 0d8a7c...91d2ee )
by Cheren
07:16
created

PrepareHelpers::_prepareTooltip()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 3
1
<?php
2
/**
3
 * CakeCMS Backend
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   Backend
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Backend".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Backend\View\Helper\Traits;
17
18
use JBZoo\Utils\Str;
19
use Cake\View\Helper;
20
use Cake\Utility\Hash;
21
22
/**
23
 * Class PrepareHelpers
24
 *
25
 * @package Backend\View\Helper\Traits
26
 */
27
trait PrepareHelpers
28
{
29
30
    /**
31
     * Prepare form buttons.
32
     *
33
     * @param Helper $helper
34
     * @param array $options
35
     * @param string $button
36
     * @return array
37
     */
38
    protected function _prepareBtn(Helper $helper, array $options, $button)
39
    {
40
        $options = $helper->addClass($options, 'waves-effect waves-light btn');
41
        if (!empty($button)) {
42
            $options = $helper->addClass($options, Str::trim((string) $button));
43
        }
44
45
        return $options;
46
    }
47
48
    /**
49
     * Prepare tooltip attrs.
50
     *
51
     * @param Helper $helper
52
     * @param array $options
53
     * @param string $tooltip
54
     * @return array
55
     */
56
    protected function _prepareTooltip(Helper $helper, array $options, $tooltip)
57
    {
58
        $_options = [
59
            'data-position' => 'top',
60
        ];
61
62
        if (isset($options['tooltipPos'])) {
63
            $_options['data-position'] = (string) $options['tooltipPos'];
64
            unset($options['tooltipPos']);
65
        }
66
67
        $options = $this->_tooltipTitle($options, $tooltip);
68
        $options = $this->_dataTooltip($options, $tooltip);
69
        $options = $helper->addClass($options, 'hasTooltip');
70
71
        return Hash::merge($_options, $options);
72
    }
73
74
    /**
75
     * Setup tooltip title.
76
     *
77
     * @param array $options
78
     * @param string $tooltip
79
     * @return array
80
     */
81
    protected function _tooltipTitle(array $options, $tooltip)
82
    {
83
        if ($tooltip === true && !isset($options['title'])) {
84
            $options['title'] = strip_tags($options['label']);
85
        }
86
87
        if (is_string($tooltip)) {
88
            $options['title'] = $tooltip;
89
        }
90
91
        return $options;
92
    }
93
94
    /**
95
     * Setup tooltip data-tooltip attr.
96
     *
97
     * @param array $options
98
     * @param string $tooltip
99
     * @return array
100
     */
101
    protected function _dataTooltip(array $options, $tooltip)
102
    {
103
        if (isset($options['title'])) {
104
            $options['data-tooltip'] = $options['title'];
105
        }
106
107
        if (is_string($tooltip)) {
108
            $options['data-tooltip'] = $tooltip;
109
        }
110
111
        return $options;
112
    }
113
}
114