ArrayChoice::view()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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