19declare(strict_types=1);
54 public function id(array $raw_parameters,
string $name,
string $missing):
int
56 $value = $raw_parameters[$name] ??
null;
58 if ($value ===
null || $value ===
'') {
59 throw new \InvalidArgumentException($missing, 400);
64 $this->refinery->byTrying([
65 $this->refinery->to()->int(),
66 $this->refinery->to()->string(),
68 $this->refinery->kindlyTo()->int(),
69 $this->refinery->int()->isGreaterThan(0),
72 "'{$name}' has to be a positive whole number."
81 public function string(array $raw_parameters,
string $name): ?
string
83 $value = $raw_parameters[$name] ??
null;
85 if ($value ===
null) {
89 return $this->
apply($this->
toString(), $value,
"'{$name}' has to be a text.");
98 public function text(array $raw_parameters,
string $name): ?
string
100 $value = $raw_parameters[$name] ??
null;
102 if ($value ===
null) {
109 $this->refinery->string()->stripTags(),
113 "'{$name}' has to be a text."
125 public function flag(array $raw_parameters,
string $name,
bool $default): bool
127 $value = $raw_parameters[$name] ??
null;
129 if ($value ===
null) {
134 $this->
refinery->kindlyTo()->bool(),
136 "'{$name}' has to be true or false."
148 public function list(array $raw_parameters,
string $name): array
150 $value = $raw_parameters[$name] ?? [];
152 $list = $this->
apply(
154 $this->refinery->byTrying([
155 $this->refinery->in()->series([
156 $this->refinery->to()->string(),
157 $this->refinery->string()->splitString(
','),
159 $this->refinery->identity(),
161 $this->refinery->to()->listOf(
162 $this->refinery->in()->series([
163 $this->refinery->to()->string(),
169 "'{$name}' has to be a list of texts."
172 return array_values(array_filter($list,
static fn(
string $entry):
bool => $entry !==
''));
180 return $this->
refinery->in()->series([
182 $this->refinery->to()->string(),
183 $this->refinery->to()->int(),
184 $this->refinery->to()->float(),
186 $this->refinery->kindlyTo()->string(),
193 return $this->
refinery->custom()->transformation(
194 static fn(
string $value):
string =>
trim($value)
205 return $transformation->
transform($value);
206 }
catch (\Exception
$e) {
207 throw new \InvalidArgumentException($refusal, 400,
$e);
Reads the raw parameters of an activity strictly, with the Refinery.
text(array $raw_parameters, string $name)
Text a user wrote, e.g.
string(array $raw_parameters, string $name)
A plain value as it was sent, trimmed; null if it was not sent.
list(array $raw_parameters, string $name)
A list of plain values, sent as a list or comma separated; empty entries are dropped.
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 ...
__construct(private readonly Refinery $refinery,)
id(array $raw_parameters, string $name, string $missing)
A positive whole number, e.g.
toString()
Text, or a number sent as one - a JSON body may well carry a title 42.
flag(array $raw_parameters, string $name, bool $default)
A yes or a no: true, false, 1, 0, also as text.