Completed
Push — master ( b0c988...0a4514 )
by Vincent
11:48
created

SubmitButton::name()   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 0
crap 1
1
<?php
2
3
namespace Bdf\Form\Button;
4
5
use Bdf\Form\Button\View\ButtonView;
6
use Bdf\Form\Button\View\ButtonViewInterface;
7
use Bdf\Form\Child\Http\HttpFieldPath;
8
9
/**
10
 * Simple button implementation
11
 * The button is considered as clicked when its value is equals to the registered value
12
 */
13
final class SubmitButton implements ButtonInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $name;
19
20
    /**
21
     * @var string
22
     */
23
    private $value;
24
25
    /**
26
     * @var array
27
     */
28
    private $groups;
29
30
    /**
31
     * @var bool
32
     */
33
    private $clicked = false;
34
35
36
    /**
37
     * SubmitButton constructor.
38
     *
39
     * @param string $name
40
     * @param string $value
41
     * @param string[] $groups
42
     */
43 15
    public function __construct(string $name, string $value = 'ok', array $groups = [])
44
    {
45 15
        $this->name = $name;
46 15
        $this->value = $value;
47 15
        $this->groups = $groups;
48 15
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 7
    public function name(): string
54
    {
55 7
        return $this->name;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 7
    public function clicked(): bool
62
    {
63 7
        return $this->clicked;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 5
    public function constraintGroups(): array
70
    {
71 5
        return $this->groups;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 10
    public function submit($data): bool
78
    {
79 10
        return $this->clicked = isset($data[$this->name]) && (string) $data[$this->name] === $this->value;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function toHttp(): array
86
    {
87 2
        if (!$this->clicked) {
88 2
            return [];
89
        }
90
91 2
        return [$this->name => $this->value];
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 3
    public function view(?HttpFieldPath $parent = null): ButtonViewInterface
98
    {
99 3
        return new ButtonView($parent ? $parent->add($this->name)->get() : $this->name, $this->value, $this->clicked());
100
    }
101
}
102