1 | <?php |
||
2 | |||
3 | namespace kalanis\Restful\Http; |
||
4 | |||
5 | |||
6 | use ArrayIterator; |
||
7 | use Exception; |
||
8 | use IteratorAggregate; |
||
9 | use kalanis\Restful\Validation\Error; |
||
10 | use kalanis\Restful\Validation\IDataProvider; |
||
11 | use kalanis\Restful\Validation\IField; |
||
12 | use kalanis\Restful\Validation\IValidationScope; |
||
13 | use kalanis\Restful\Validation\IValidationScopeFactory; |
||
14 | use Nette\MemberAccessException; |
||
15 | |||
16 | |||
17 | /** |
||
18 | * Request Input parser |
||
19 | * @package kalanis\Restful\Http |
||
20 | * @template TK of string |
||
21 | * @template TVal of mixed |
||
22 | * @implements IteratorAggregate<TK, TVal> |
||
23 | */ |
||
24 | 1 | class Input implements IteratorAggregate, IInput, IDataProvider |
|
25 | { |
||
26 | |||
27 | private ?IValidationScope $validationScope = null; |
||
28 | |||
29 | /** |
||
30 | * @param IValidationScopeFactory $validationScopeFactory |
||
31 | * @param array<TK, TVal> $data |
||
32 | */ |
||
33 | 1 | public function __construct( |
|
34 | private readonly IValidationScopeFactory $validationScopeFactory, |
||
35 | private array $data = [], |
||
36 | ) |
||
37 | { |
||
38 | 1 | } |
|
39 | |||
40 | /** |
||
41 | * @param string $name |
||
42 | * @throws Exception|MemberAccessException |
||
43 | * @return mixed |
||
44 | * |
||
45 | */ |
||
46 | public function &__get(string $name) |
||
47 | { |
||
48 | 1 | $data = $this->getData(); |
|
49 | 1 | if (array_key_exists($name, $data)) { |
|
50 | 1 | return $data[$name]; |
|
51 | } |
||
52 | 1 | throw new MemberAccessException('Cannot read an undeclared property ' . static::class . '::$' . $name . '.'); |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Get parsed input data |
||
57 | * @return array<TK, TVal> |
||
58 | */ |
||
59 | public function getData(): array |
||
60 | { |
||
61 | 1 | return $this->data; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Set input data |
||
66 | * @param array<TK, TVal> $data |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function setData(array $data): static |
||
70 | { |
||
71 | 1 | $this->data = $data; |
|
72 | 1 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string $name |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function __isset(string $name) |
||
80 | { |
||
81 | $data = $this->getData(); |
||
82 | return array_key_exists($name, $data); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Get input data iterator |
||
87 | * @return ArrayIterator<TK, TVal> |
||
88 | */ |
||
89 | public function getIterator(): ArrayIterator |
||
90 | { |
||
91 | return new ArrayIterator($this->getData()); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get validation field |
||
96 | * @param string $name |
||
97 | * @return IField |
||
98 | */ |
||
99 | public function field(string $name): IField |
||
100 | { |
||
101 | 1 | return $this->getValidationScope()->field($name); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get validation scope |
||
106 | * @return IValidationScope |
||
107 | */ |
||
108 | public function getValidationScope(): IValidationScope |
||
109 | { |
||
110 | 1 | if (empty($this->validationScope)) { |
|
111 | 1 | $this->validationScope = $this->validationScopeFactory->create(); |
|
112 | } |
||
113 | 1 | return $this->validationScope; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Is input valid |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function isValid(): bool |
||
121 | { |
||
122 | 1 | return !$this->validate(); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Validate input data |
||
127 | * @return array<Error> |
||
128 | */ |
||
129 | public function validate(): array |
||
130 | { |
||
131 | 1 | return $this->getValidationScope()->validate($this->getData()); |
|
132 | } |
||
133 | } |
||
134 |