ChoiceView   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 13
c 1
b 0
f 0
dl 0
loc 92
ccs 16
cts 19
cp 0.8421
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setLabel() 0 3 1
A __construct() 0 7 2
A label() 0 3 1
A setSelected() 0 3 1
A selected() 0 3 1
A setValue() 0 3 1
A value() 0 3 1
1
<?php
2
3
namespace Bdf\Form\Choice;
4
5
/**
6
 * View object for choice
7
 *
8
 * <code>
9
 * <?php foreach ($element->choices() as $choice): ?>
10
 *     <label>
11
 *         <?php echo $choice->label(); ?>
12
 *         <input type="radio" name="<?php echo $element->name(); ?>" value="<?php echo $choice->value()" <?php echo $choice->selected() ? 'selected' : ''; ?> />
13
 *     </label>
14
 * <?php endforeach; ?>
15
 * </code>
16
 */
17
class ChoiceView
18
{
19
    /**
20
     * The label displayed to humans.
21
     *
22
     * @var string
23
     */
24
    private $label;
25
26
    /**
27
     * The view representation of the choice.
28
     *
29
     * @var mixed
30
     */
31
    private $value;
32
33
    /**
34
     * The view representation of the choice.
35
     *
36
     * @var boolean
37
     */
38
    private $selected;
39
40
    /**
41
     * Creates a new choice view.
42
     *
43
     * @param mixed $value The option value
44
     * @param string|int $label The label displayed to humans
45
     * @param boolean $selected This choice is selected
46
     */
47 15
    public function __construct($value, $label, bool $selected = false)
48
    {
49 15
        $this->value = $value;
50 15
        $this->selected = $selected;
51
52
        // If there is no label, we take the value as label
53 15
        $this->label = !is_string($label) ? $value : $label;
54 15
    }
55
56
    /**
57
     * Get the display label
58
     * If the label is not declared, return the value
59
     *
60
     * @return string
61
     */
62 11
    public function label(): string
63
    {
64 11
        return $this->label;
65
    }
66
67
    /**
68
     * @param string $label
69
     */
70
    public function setLabel(string $label): void
71
    {
72
        $this->label = $label;
73
    }
74
75
    /**
76
     * Get the option value
77
     *
78
     * @return mixed
79
     */
80 15
    public function value()
81
    {
82 15
        return $this->value;
83
    }
84
85
    /**
86
     * @param mixed $value
87
     */
88 11
    public function setValue($value): void
89
    {
90 11
        $this->value = $value;
91 11
    }
92
93
    /**
94
     * Does the current option is selected ?
95
     *
96
     * @return bool
97
     */
98 12
    public function selected(): bool
99
    {
100 12
        return $this->selected;
101
    }
102
103
    /**
104
     * @param bool $selected
105
     */
106 11
    public function setSelected(bool $selected): void
107
    {
108 11
        $this->selected = $selected;
109 11
    }
110
}
111