Hidden::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
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
    public function __construct(string $name, string $value = null) {
28
      $this->name = $name;
29
      $this->value = $value ?? '';
30
    }
31
32
33
    public function setValue(string $value) : self {
34
      $this->value = $value;
35
      return $this;
36
    }
37
38
39
    public function getValue() : string {
40
      return $this->value;
41
    }
42
43
44
    /**
45
     * @param FormData $request
46
     * @return $this
47
     */
48
    public function handle(FormData $request) {
49
      $value = $request->get($this->getName());
50
      $this->setValue((string) $value);
51
      return $this;
52
    }
53
54
55
    /**
56
     * @return string
57
     */
58
    public function getName() {
59
      return $this->name;
60
    }
61
62
63
    /**
64
     * @return string
65
     */
66
    public function render() {
67
      return Html::tag('input', [
68
        'type' => 'hidden',
69
        'name' => $this->getName(),
70
        'value' => htmlentities($this->value, ENT_QUOTES),
71
      ]);
72
    }
73
74
75
  }