ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ListTransformation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
31 {
34  use ProblemBuilder;
35 
37 
38  public function __construct(Transformation $transformation)
39  {
40  $this->transformation = $transformation;
41  }
42 
46  public function transform($from): array
47  {
48  $this->check($from);
49 
50  $result = [];
51  foreach ($from as $value) {
52  $transformedValue = $this->transformation->transform($value);
53  $result[] = $transformedValue;
54  }
55 
56  return $result;
57  }
58 
62  public function getError(): string
63  {
64  return 'The value MUST be of type array.';
65  }
66 
70  public function check($value)
71  {
72  if (!$this->accepts($value)) {
73  throw new UnexpectedValueException($this->getErrorMessage($value));
74  }
75 
76  return null;
77  }
78 
82  public function accepts($value): bool
83  {
84  return is_array($value);
85  }
86 
90  public function problemWith($value): ?string
91  {
92  if (!$this->accepts($value)) {
93  return $this->getErrorMessage($value);
94  }
95 
96  return null;
97  }
98 }
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getErrorMessage($value)
A transformation is a function from one datatype to another.