Button::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
1
<?php namespace Arcanesoft\Core\Helpers\UI;
2
3
/**
4
 * Class     Button
5
 *
6
 * @package  Arcanesoft\Core\Helpers\UI
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
class Button extends AbstractClickable
10
{
11
    /* -----------------------------------------------------------------
12
     |  Properties
13
     | -----------------------------------------------------------------
14
     */
15
16
    /** @var string */
17
    protected $type;
18
19
    /* -----------------------------------------------------------------
20
     |  Constructor
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Button constructor.
26
     *
27
     * @param  string  $action
28
     * @param  string  $type
29
     * @param  array   $attributes
30
     * @param  bool    $disabled
31
     */
32 36
    public function __construct($action, $type = 'button', array $attributes = [], $disabled)
33
    {
34 36
        $this->setAction($action);
35 36
        $this->setType($type);
36 36
        $this->setAttributes($attributes);
37 36
        $this->setDisabled($disabled);
38 36
    }
39
40
    /* -----------------------------------------------------------------
41
     |  Getters & Setters
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Set the button type.
47
     *
48
     * @param  string  $type
49
     *
50
     * @return self
51
     */
52 36
    public function setType($type)
53
    {
54 36
        $this->type = in_array($type = strtolower($type), ['button', 'submit', 'reset']) ? $type : 'button';
55
56 36
        return $this;
57
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Main Methods
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * Make a button instance.
66
     *
67
     * @param  string  $action
68
     * @param  string  $type
69
     * @param  array   $attributes
70
     * @param  bool    $disabled
71
     *
72
     * @return self
73
     */
74 36
    public static function make($action, $type = 'button', array $attributes = [], $disabled = false)
75
    {
76 36
        return new static($action, $type, $attributes, $disabled);
77
    }
78
79
    /**
80
     * Get content as a string of HTML.
81
     *
82
     * @return string
83
     */
84 36
    public function toHtml()
85
    {
86 36
        return '<button'.$this->renderAttributes().'>'.$this->renderValue().'</button>';
87
    }
88
89
    /* -----------------------------------------------------------------
90
     |  Other Methods
91
     | -----------------------------------------------------------------
92
     */
93
94
    /**
95
     * Render the attributes.
96
     *
97
     * @return string
98
     */
99 36
    protected function renderAttributes()
100
    {
101 36
        $attributes = collect();
102 36
        $attributes->put('type', $this->type);
103 36
        $attributes->put('class', $this->getStyleClass());
104
105 36
        if ($this->withTooltip) {
106
            // This is for Bootstrap
107 4
            $attributes->put('data-toggle', 'tooltip');
108 4
            $attributes->put('data-original-title', $this->getTitle());
109
        }
110
111 36
        if ($this->disabled) {
112 2
            $attributes->put('type', 'button');
113 2
            $attributes->put('disabled', 'disabled');
114
        }
115
116 36
        return html()->attributes($attributes->merge($this->attributes)->toArray());
117
    }
118
119
    /**
120
     * Get the value from config.
121
     *
122
     * @param  string  $key
123
     *
124
     * @return mixed
125
     */
126 36
    protected function getConfig($key)
127
    {
128 36
        return config("arcanesoft.core.ui.buttons.{$key}");
129
    }
130
}
131