Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

RadioButton   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.24%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 4
c 3
b 2
f 0
lcom 1
cbo 1
dl 0
loc 58
ccs 15
cts 17
cp 0.8824
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 4 1
B createCheckable() 0 37 3
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\Radio;
15
16
/**
17
 * RadioButton is a Former field class for converting radios to buttons with custom colors for each.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class RadioButton extends Radio
22
{
23
    /**
24
     * Render radio buttons.
25
     *
26
     * @return string
27
     */
28 7
    public function render()
29
    {
30 7
        return '<div class="btn-toolbar radio-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 7
    protected function createCheckable($item, $fallbackValue = 1)
42
    {
43
        // Check for required values
44 7
        if (!isset($item['attributes']['color'])) {
45
            return '';
46
        }
47
48
        // Make sure the parent class will create inline radios
49 7
        $this->inline = true;
50
51
        // Extract the color for this button/radio & unset it to prevent creating attribute
52 7
        $color = $item['attributes']['color'];
53 7
        unset($item['attributes']['color']);
54
55
        // Color for selected button/radio
56 7
        $selectedColor = ';color:' . $color . ';';
57
58
        // Data attribute for the button color
59 7
        $item['attributes']['data-color'] = $color;
60
61
        // Generate the radio button
62 7
        $item = parent::createCheckable($item, $fallbackValue);
63
64
        // If property checked found, then make the button selected with styles
65 7
        if (strpos($item, 'checked') !== false) {
66
            $selectedColor = ';background:' . $color . ';color:white;';
67
        }
68
69
        // Add bootstrap classes for styling the buttons
70 7
        $item = str_replace('inline', 'btn btn-default', $item);
71
72
        // Add styles to the wrapper label tag
73 7
        $style = 'border-color: ' . $color . $selectedColor;
74 7
        $item  = str_replace('<label', '<label style="' . $style . '"', $item);
75
76 7
        return $item;
77
    }
78
}
79