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

CheckboxButton   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 54
ccs 15
cts 16
cp 0.9375
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 4 1
B createCheckable() 0 33 2
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