Completed
Push — master ( 195b94...858be2 )
by Mohamed
07:34
created

CheckboxButton::createCheckable()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
ccs 13
cts 14
cp 0.9286
rs 8.8571
cc 2
eloc 14
nc 2
nop 2
crap 2.0014
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Form\Former\Fields;
13
14
use Former\Form\Fields\Checkbox;
15
16
/**
17
 * CheckboxButton is a Former field class for converting checkboxes to buttons with custom colors for each.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class CheckboxButton extends Checkbox
22
{
23
    /**
24
     * Render radio buttons.
25
     *
26
     * @return string
27
     */
28 3
    public function render()
29
    {
30 3
        return '<div class="btn-toolbar checkbox-btn" data-toggle="buttons">' . parent::render() . '</div>';
31
    }
32
33
    /**
34
     * Render checkable radio button.
35
     *
36
     * @param array|string $item
37
     * @param int          $fallbackValue
38
     *
39
     * @return string
40
     */
41 3
    protected function createCheckable($item, $fallbackValue = 1)
42
    {
43
        // Make sure the parent class will create inline radios
44 3
        $this->inline = true;
45 3
        $this->grouped();
46
//        $this=
47
        // Extract the color for this button/radio & unset it to prevent creating attribute
48 3
        $color = $item['attributes']['color'];
49 3
        unset($item['attributes']['color']);
50
51
        // Color for selected button/radio
52 3
        $selectedColor = ';color:' . $color . ';';
53
54
        // Data attribute for the button color
55 3
        $item['attributes']['data-color'] = $color;
56
57
        // Generate the radio button
58 3
        $item = parent::createCheckable($item, $fallbackValue);
59
60
        // If property checked found, then make the button selected with styles
61 3
        if (strpos($item, 'checked') !== false) {
62
            $selectedColor = ';background:' . $color . ';color:white;';
63
        }
64
65
        // Add bootstrap classes for styling the buttons
66 3
        $item = str_replace('inline', 'btn btn-default', $item);
67
68
        // Add styles to the wrapper label tag
69 3
        $style = 'border-color: ' . $color . $selectedColor;
70 3
        $item  = str_replace('<label', '<label style="' . $style . '"', $item);
71
72 3
        return $item;
73
    }
74
}
75