ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
DMap.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Data\Description;
22 
23 use ILIAS\Data\Text;
24 
25 class DMap extends Description
26 {
27  use ErrorHandling;
28 
29  public function __construct(
30  Text\SimpleDocumentMarkdown $description,
31  protected DValue $key_type,
32  protected Description $value_type
33  ) {
34  parent::__construct($description);
35  }
36 
37  public function getPrimitiveRepresentation(mixed $data): mixed
38  {
39  if (!is_array($data)) {
40  return fn() => yield "Expected an array.";
41  }
42 
43  $repr = [];
44  $errors = [];
45 
46  foreach ($data as $k => $v) {
47  $key = $this->key_type->getPrimitiveRepresentation($k);
48  $value = $this->value_type->getPrimitiveRepresentation($v);
49 
50  $key_is_error = $key instanceof \Closure;
51  $value_is_error = $value instanceof \Closure;
52 
53  if ($key_is_error) {
54  $errors[] = $key;
55  }
56  if ($value_is_error) {
57  $errors[] = $value;
58  }
59 
60  if (!$key_is_error && !$value_is_error) {
61  $repr[$key] = $value;
62  }
63  }
64 
65  if ($errors) {
66  return $this->mergeErrors($errors);
67  }
68 
69  return $repr;
70  }
71 
72  public function getKeyType(): DValue
73  {
74  return $this->key_type;
75  }
76 
77  public function getValueType(): Description
78  {
79  return $this->value_type;
80  }
81 }
__construct(Text\SimpleDocumentMarkdown $description, protected DValue $key_type, protected Description $value_type)
Definition: DMap.php:29
This describes some datastructure in terms of standard data structures such as primitives, lists, maps and objects and helpful (hopefully...) human readable texts.
Definition: Description.php:32
getPrimitiveRepresentation(mixed $data)
Definition: DMap.php:37
__construct(Container $dic, ilPlugin $plugin)