ILIAS  trunk Revision v11.0_alpha-1861-g09f3d197f78
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DictionaryTransformation.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 
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:31
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getErrorMessage($value)
A transformation is a function from one datatype to another.