ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ListTransformation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use UnexpectedValueException;
29
31{
35
37
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:32
A transformation is a function from one datatype to another.
getErrorMessage($value)