ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
Parameter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Refinery\Factory as Refinery;
25
42{
43 public function __construct(
44 private readonly Refinery $refinery,
45 ) {
46 }
47
54 public function id(array $raw_parameters, string $name, string $missing): int
55 {
56 $value = $raw_parameters[$name] ?? null;
57
58 if ($value === null || $value === '') {
59 throw new \InvalidArgumentException($missing, 400);
60 }
61
62 return $this->apply(
63 $this->refinery->in()->series([
64 $this->refinery->byTrying([
65 $this->refinery->to()->int(),
66 $this->refinery->to()->string(),
67 ]),
68 $this->refinery->kindlyTo()->int(),
69 $this->refinery->int()->isGreaterThan(0),
70 ]),
71 $value,
72 "'{$name}' has to be a positive whole number."
73 );
74 }
75
81 public function string(array $raw_parameters, string $name): ?string
82 {
83 $value = $raw_parameters[$name] ?? null;
84
85 if ($value === null) {
86 return null;
87 }
88
89 return $this->apply($this->toString(), $value, "'{$name}' has to be a text.");
90 }
91
98 public function text(array $raw_parameters, string $name): ?string
99 {
100 $value = $raw_parameters[$name] ?? null;
101
102 if ($value === null) {
103 return null;
104 }
105
106 return $this->apply(
107 $this->refinery->in()->series([
108 $this->toString(),
109 $this->refinery->string()->stripTags(),
110 $this->trim(),
111 ]),
112 $value,
113 "'{$name}' has to be a text."
114 );
115 }
116
125 public function flag(array $raw_parameters, string $name, bool $default): bool
126 {
127 $value = $raw_parameters[$name] ?? null;
128
129 if ($value === null) {
130 return $default;
131 }
132
133 return $this->apply(
134 $this->refinery->kindlyTo()->bool(),
135 $value,
136 "'{$name}' has to be true or false."
137 );
138 }
139
148 public function list(array $raw_parameters, string $name): array
149 {
150 $value = $raw_parameters[$name] ?? [];
151
152 $list = $this->apply(
153 $this->refinery->in()->series([
154 $this->refinery->byTrying([
155 $this->refinery->in()->series([
156 $this->refinery->to()->string(),
157 $this->refinery->string()->splitString(','),
158 ]),
159 $this->refinery->identity(),
160 ]),
161 $this->refinery->to()->listOf(
162 $this->refinery->in()->series([
163 $this->refinery->to()->string(),
164 $this->trim(),
165 ])
166 ),
167 ]),
168 $value,
169 "'{$name}' has to be a list of texts."
170 );
171
172 return array_values(array_filter($list, static fn(string $entry): bool => $entry !== ''));
173 }
174
178 private function toString(): Transformation
179 {
180 return $this->refinery->in()->series([
181 $this->refinery->byTrying([
182 $this->refinery->to()->string(),
183 $this->refinery->to()->int(),
184 $this->refinery->to()->float(),
185 ]),
186 $this->refinery->kindlyTo()->string(),
187 $this->trim(),
188 ]);
189 }
190
191 private function trim(): Transformation
192 {
193 return $this->refinery->custom()->transformation(
194 static fn(string $value): string => trim($value)
195 );
196 }
197
202 private function apply(Transformation $transformation, mixed $value, string $refusal): mixed
203 {
204 try {
205 return $transformation->transform($value);
206 } catch (\Exception $e) {
207 throw new \InvalidArgumentException($refusal, 400, $e);
208 }
209 }
210}
Builds data types.
Definition: Factory.php:36
Reads the raw parameters of an activity strictly, with the Refinery.
Definition: Parameter.php:42
text(array $raw_parameters, string $name)
Text a user wrote, e.g.
Definition: Parameter.php:98
string(array $raw_parameters, string $name)
A plain value as it was sent, trimmed; null if it was not sent.
Definition: Parameter.php:81
list(array $raw_parameters, string $name)
A list of plain values, sent as a list or comma separated; empty entries are dropped.
Definition: Parameter.php:148
apply(Transformation $transformation, mixed $value, string $refusal)
The Refinery says why in its own words and in its own exceptions; the caller is told which parameter ...
Definition: Parameter.php:202
__construct(private readonly Refinery $refinery,)
Definition: Parameter.php:43
id(array $raw_parameters, string $name, string $missing)
A positive whole number, e.g.
Definition: Parameter.php:54
toString()
Text, or a number sent as one - a JSON body may well carry a title 42.
Definition: Parameter.php:178
flag(array $raw_parameters, string $name, bool $default)
A yes or a no: true, false, 1, 0, also as text.
Definition: Parameter.php:125
A transformation is a function from one datatype to another.
transform($from)
Perform the transformation.