Completed
Push — develop ( e17ce1...c70dd4 )
by Mohamed
06:57
created

CheckboxButton::createCheckable()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 14

Duplication

Lines 33
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 33
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 View Code Duplication
class CheckboxButton extends Checkbox
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
{
23
    /**
24
     * Render radio buttons.
25
     *
26
     * @return string
27
     */
28 3
    public function render()
29
    {
30
        try {
31 3
            return '<div class="btn-toolbar checkbox-btn" data-toggle="buttons">' . parent::render() . '</div>';
32
        } catch (\Exception $e) {
33
            echo $e;
34
            die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method render() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
35
        }
36
    }
37
38
    /**
39
     * Render checkable radio button.
40
     *
41
     * @param array|string $item
42
     * @param int          $fallbackValue
43
     *
44
     * @return string
45
     */
46 3
    protected function createCheckable($item, $fallbackValue = 1)
47
    {
48
        // Make sure the parent class will create inline radios
49 3
        $this->inline = true;
50 3
        $this->grouped();
51
//        $this=
52
        // Extract the color for this button/radio & unset it to prevent creating attribute
53 3
        $color = $item['attributes']['color'];
54 3
        unset($item['attributes']['color']);
55
56
        // Color for selected button/radio
57 3
        $selectedColor = ';color:' . $color . ';';
58
59
        // Data attribute for the button color
60 3
        $item['attributes']['data-color'] = $color;
61
62
        // Generate the radio button
63 3
        $item = parent::createCheckable($item, $fallbackValue);
64
65
        // If property checked found, then make the button selected with styles
66 3
        if (strpos($item, 'checked') !== false) {
67
            $selectedColor = ';background:' . $color . ';color:white;';
68
        }
69
70
        // Add bootstrap classes for styling the buttons
71 3
        $item = str_replace('inline', 'btn btn-default', $item);
72
73
        // Add styles to the wrapper label tag
74 3
        $style = 'border-color: ' . $color . $selectedColor;
75 3
        $item  = str_replace('<label', '<label style="' . $style . '"', $item);
76
77 3
        return $item;
78
    }
79
}
80