Passed
Branch master (3daac1)
by Vincent
07:53
created

ArrayChoice   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 46
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A values() 0 3 1
A __construct() 0 3 1
A view() 0 13 3
1
<?php
2
3
namespace Bdf\Form\Choice;
4
5
/**
6
 * Implementation of choice using an array
7
 */
8
final class ArrayChoice implements ChoiceInterface
9
{
10
    /**
11
     * The list of choices
12
     *
13
     * Key: should be the label of the choice
14
     * Value: the value
15
     *
16
     * @var array
17
     */
18
    private $choices;
19
20
    /**
21
     * ArrayChoice constructor.
22
     *
23
     * @param array $choices The choices. To declare label, use associative array with key as label
24
     */
25 18
    public function __construct(array $choices)
26
    {
27 18
        $this->choices = $choices;
28 18
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 15
    public function values(): array
34
    {
35 15
        return $this->choices;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 9
    public function view(?callable $configurator = null): array
42
    {
43 9
        $view = [];
44
45 9
        foreach ($this->choices as $label => $value) {
46 9
            $view[] = $choice = new ChoiceView($value, $label);
47
48 9
            if ($configurator) {
49 8
                $configurator($choice);
50
            }
51
        }
52
53 9
        return $view;
54
    }
55
}
56