ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ILIAS\FileUpload\Activities\Parameter Class Reference

Reads the raw parameters of an activity strictly, with the Refinery. More...

+ Collaboration diagram for ILIAS\FileUpload\Activities\Parameter:

Public Member Functions

 __construct (private readonly Refinery $refinery,)
 
 id (array $raw_parameters, string $name, string $missing)
 A positive whole number, e.g. More...
 
 string (array $raw_parameters, string $name)
 A plain value as it was sent, trimmed; null if it was not sent. More...
 
 text (array $raw_parameters, string $name)
 Text a user wrote, e.g. More...
 
 flag (array $raw_parameters, string $name, bool $default)
 A yes or a no: true, false, 1, 0, also as text. More...
 
 list (array $raw_parameters, string $name)
 A list of plain values, sent as a list or comma separated; empty entries are dropped. More...
 

Private Member Functions

 toString ()
 Text, or a number sent as one - a JSON body may well carry a title 42. More...
 
 trim ()
 
 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 was wrong, as a 400. More...
 

Detailed Description

Reads the raw parameters of an activity strictly, with the Refinery.

Raw parameters come from outside - a query string, a JSON body - and a cast makes something of everything: ‘(int) ['5’]is 1, the root of the repository,(int) '102abc'is 102.kindlyTo()` is the right tool for values that arrive as text, but it is kind to more than text: it makes 1 of true and rounds 1.7. So every parameter first has to be of a type that can carry it (to()), and only then is it converted (kindlyTo()). A value that is not what the parameter takes is refused with a 400, so a client never gets an answer to a question it did not ask.

Once maybePerformAs() reads raw parameters through the input description of an activity, this goes away.

Definition at line 41 of file Parameter.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\FileUpload\Activities\Parameter::__construct ( private readonly Refinery  $refinery)

Definition at line 43 of file Parameter.php.

45 {
46 }

Member Function Documentation

◆ apply()

ILIAS\FileUpload\Activities\Parameter::apply ( Transformation  $transformation,
mixed  $value,
string  $refusal 
)
private

The Refinery says why in its own words and in its own exceptions; the caller is told which parameter was wrong, as a 400.

Definition at line 202 of file Parameter.php.

202 : mixed
203 {
204 try {
205 return $transformation->transform($value);
206 } catch (\Exception $e) {
207 throw new \InvalidArgumentException($refusal, 400, $e);
208 }
209 }
transform($from)
Perform the transformation.

References Vendor\Package\$e, and ILIAS\Refinery\Transformation\transform().

Referenced by ILIAS\FileUpload\Activities\Parameter\flag(), ILIAS\FileUpload\Activities\Parameter\id(), ILIAS\FileUpload\Activities\Parameter\list(), ILIAS\FileUpload\Activities\Parameter\string(), and ILIAS\FileUpload\Activities\Parameter\text().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ flag()

ILIAS\FileUpload\Activities\Parameter::flag ( array  $raw_parameters,
string  $name,
bool  $default 
)

A yes or a no: true, false, 1, 0, also as text.

Anything else is refused rather than read as "no" - for keep_previous_version a typo would be "drop the history". Only a missing value falls back to the default.

Parameters
array<string,mixed>$raw_parameters

Definition at line 125 of file Parameter.php.

125 : 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 }
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

References ILIAS\FileUpload\Activities\Parameter\apply(), and ILIAS\Repository\refinery().

+ Here is the call graph for this function:

◆ id()

ILIAS\FileUpload\Activities\Parameter::id ( array  $raw_parameters,
string  $name,
string  $missing 
)

A positive whole number, e.g.

a reference id.

Parameters
array<string,mixed>$raw_parameters
string$missingwhat to answer when the parameter is not there

Definition at line 54 of file Parameter.php.

54 : 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 }

References ILIAS\FileUpload\Activities\Parameter\apply(), and ILIAS\Repository\refinery().

+ Here is the call graph for this function:

◆ list()

ILIAS\FileUpload\Activities\Parameter::list ( array  $raw_parameters,
string  $name 
)

A list of plain values, sent as a list or comma separated; empty entries are dropped.

Parameters
array<string,mixed>$raw_parameters
Returns
list<string>

Definition at line 148 of file Parameter.php.

148 : 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 }

References ILIAS\FileUpload\Activities\Parameter\apply(), and ILIAS\Repository\refinery().

+ Here is the call graph for this function:

◆ string()

ILIAS\FileUpload\Activities\Parameter::string ( array  $raw_parameters,
string  $name 
)

A plain value as it was sent, trimmed; null if it was not sent.

Parameters
array<string,mixed>$raw_parameters

Definition at line 81 of file Parameter.php.

81 : ?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 }
toString()
Text, or a number sent as one - a JSON body may well carry a title 42.
Definition: Parameter.php:178

References ILIAS\FileUpload\Activities\Parameter\apply(), and ILIAS\FileUpload\Activities\Parameter\toString().

+ Here is the call graph for this function:

◆ text()

ILIAS\FileUpload\Activities\Parameter::text ( array  $raw_parameters,
string  $name 
)

Text a user wrote, e.g.

a title: tags are stripped, as the fields of the UI framework do. Null if it was not sent.

Parameters
array<string,mixed>$raw_parameters

Definition at line 98 of file Parameter.php.

98 : ?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 }

References ILIAS\FileUpload\Activities\Parameter\apply(), and ILIAS\Repository\refinery().

+ Here is the call graph for this function:

◆ toString()

ILIAS\FileUpload\Activities\Parameter::toString ( )
private

Text, or a number sent as one - a JSON body may well carry a title 42.

Definition at line 178 of file Parameter.php.

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 }
A transformation is a function from one datatype to another.

References ILIAS\Repository\refinery().

Referenced by ILIAS\FileUpload\Activities\Parameter\string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trim()

ILIAS\FileUpload\Activities\Parameter::trim ( )
private

Definition at line 191 of file Parameter.php.

192 {
193 return $this->refinery->custom()->transformation(
194 static fn(string $value): string => trim($value)
195 );
196 }

References ILIAS\Repository\refinery(), and ILIAS\FileUpload\Activities\Parameter\trim().

Referenced by ILIAS\FileUpload\Activities\Parameter\trim().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: