ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DictionaryTransformation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use UnexpectedValueException;
29
31{
35
37
39 {
40 $this->transformation = $transformation;
41 }
42
47 public function transform($from): array
48 {
49 $this->check($from);
50
51 $result = [];
52 foreach ($from as $key => $value) {
53 $transformedValue = $this->transformation->transform($value);
54 $result[$key] = $transformedValue;
55 }
56
57 return $result;
58 }
59
63 public function getError(): string
64 {
65 return 'The value MUST be an array with only string keys.';
66 }
67
71 public function check($value)
72 {
73 if (!$this->accepts($value)) {
74 throw new UnexpectedValueException($this->getErrorMessage($value));
75 }
76
77 return null;
78 }
79
83 public function accepts($value): bool
84 {
85 if (!is_array($value)) {
86 return false;
87 }
88
89 return count(array_filter($value, static function ($key): bool {
90 return !is_string($key);
91 }, ARRAY_FILTER_USE_KEY)) === 0;
92 }
93
97 public function problemWith($value): ?string
98 {
99 if (!$this->accepts($value)) {
100 return $this->getErrorMessage($value);
101 }
102
103 return null;
104 }
105}
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
A transformation is a function from one datatype to another.
getErrorMessage($value)