Completed
Push — master ( 14565d...9435d2 )
by Shcherbak
09:17
created

Hidden::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
  declare(strict_types=1);
4
5
  namespace Fiv\Form\Element;
6
7
  use Fiv\Form\Element\Hidden\HiddenValidatorInterface;
8
  use Fiv\Form\Elements\DataElementInterface;
9
  use Fiv\Form\Elements\ValidatableElementInterface;
10
  use Fiv\Form\FormData;
11
  use Fiv\Form\Validator\ValidationResult;
12
  use Fiv\Form\Validator\ValidationResultInterface;
13
14
  class Hidden implements DataElementInterface, ValidatableElementInterface {
15
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var string
23
     */
24
    private $value;
25
26
    /**
27
     * @var HiddenValidatorInterface[]
28
     */
29
    private $validators = [];
30
31
    /**
32
     * @var ValidationResult|null
33
     */
34
    private $validationResult;
35
36
37 10
    public function __construct(string $name, string $value = null) {
38 10
      $this->name = $name;
39 10
      $this->value = $value ?? '';
40 10
    }
41
42
43 6
    public function setValue(string $value) : self {
44 6
      $this->value = $value;
45 6
      $this->validationResult = null; //Flash validation result
46 6
      return $this;
47
    }
48
49
50 6
    public function getValue() : string {
51 6
      return $this->value;
52
    }
53
54
55
    /**
56
     * @param FormData $request
57
     * @return $this
58
     */
59 5
    public function handle(FormData $request) {
60 5
      $value = $request->get($this->getName());
61 5
      $this->setValue((string) $value);
62 5
      return $this;
63
    }
64
65
66
    /**
67
     * @return string
68
     */
69 9
    public function getName() {
70 9
      return $this->name;
71
    }
72
73
74
    /**
75
     * @return string
76
     */
77 3
    public function render() {
78 3
      return Html::tag('input', [
79 3
        'type' => 'hidden',
80 3
        'name' => $this->getName(),
81 3
        'value' => htmlentities($this->value, ENT_QUOTES),
82
      ]);
83
    }
84
85
86 2
    public function addValidator(HiddenValidatorInterface $validator) : self {
87 2
      $this->validators[] = $validator;
88 2
      return $this;
89
    }
90
91
92 2
    public function validate() : ValidationResultInterface {
93
94 2
      if ($this->validationResult !== null) {
95 1
        return $this->validationResult;
96
      }
97
98 2
      $this->validationResult = new ValidationResult();
99 2
      foreach ($this->validators as $validator) {
100 2
        $result = $validator->validate($this);
101 2
        $this->validationResult->merge($result);
0 ignored issues
show
Compatibility introduced by
$result of type object<Fiv\Form\Validato...idationResultInterface> is not a sub-type of object<Fiv\Form\Validator\ValidationResult>. It seems like you assume a concrete implementation of the interface Fiv\Form\Validator\ValidationResultInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
102
      }
103
104 2
      return $this->validationResult;
105
    }
106
107
  }