Completed
Push — master ( 2811d3...4b37a8 )
by Shcherbak
11:11
created

Hidden::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
  declare(strict_types=1);
4
5
  namespace Fiv\Form\Element;
6
7
  use Fiv\Form\Elements\StringDataElementInterface;
8
  use Fiv\Form\Elements\ValidatableElementInterface;
9
  use Fiv\Form\FormData;
10
  use Fiv\Form\Validation\StringValidation\StringValidationTrait;
11
12
  class Hidden implements ValidatableElementInterface, StringDataElementInterface {
13
14
    use StringValidationTrait;
15
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var string
23
     */
24
    private $value;
25
26
27 10
    public function __construct(string $name, string $value = null) {
28 10
      $this->name = $name;
29 10
      $this->value = $value ?? '';
30 10
    }
31
32
33 6
    public function setValue(string $value) : self {
34 6
      $this->value = $value;
35 6
      return $this;
36
    }
37
38
39 6
    public function getValue() : string {
40 6
      return $this->value;
41
    }
42
43
44
    /**
45
     * @param FormData $request
46
     * @return $this
47
     */
48 5
    public function handle(FormData $request) {
49 5
      $value = $request->get($this->getName());
50 5
      $this->setValue((string) $value);
51 5
      return $this;
52
    }
53
54
55
    /**
56
     * @return string
57
     */
58 9
    public function getName() {
59 9
      return $this->name;
60
    }
61
62
63
    /**
64
     * @return string
65
     */
66 3
    public function render() {
67 3
      return Html::tag('input', [
68 3
        'type' => 'hidden',
69 3
        'name' => $this->getName(),
70 3
        'value' => htmlentities($this->value, ENT_QUOTES),
71
      ]);
72
    }
73
74
75
  }