ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
DictionaryTransformation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193
getErrorMessage($value)
Get the problem message.
A transformation is a function from one datatype to another.