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

ArrayChoice::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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