ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
KeyValueAccess.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Refinery;
22 
23 use Closure;
24 use ArrayAccess;
25 use Countable;
26 
28 {
29  private array $raw_values;
31 
32  public function __construct(array $raw_values, Transformation $trafo)
33  {
34  $this->trafo = $trafo;
35  $this->raw_values = $raw_values;
36  }
37 
41  public function offsetExists($offset): bool
42  {
43  return isset($this->raw_values[$offset]);
44  }
45 
49  public function offsetGet($offset)
50  {
51  if (!$this->offsetExists($offset)) {
52  return null;
53  }
54 
55  return is_array($this->raw_values[$offset])
56  ? array_map($this->getApplicator(), $this->raw_values[$offset])
57  : $this->getApplicator()($this->raw_values[$offset]);
58  }
59 
60  private function getApplicator(): Closure
61  {
62  return function ($value) {
63  if (is_array($value)) {
64  foreach ($value as $k => $v) {
65  $value[$k] = $this->getApplicator()($v);
66  }
67  return $value;
68  }
69  return $this->trafo->transform($value);
70  };
71  }
72 
76  public function offsetSet($offset, $value): void
77  {
78  $this->raw_values[$offset] = $value;
79  }
80 
84  public function offsetUnset($offset): void
85  {
86  if ($this->offsetExists($offset)) {
87  unset($this->raw_values[$offset]);
88  }
89  }
90 
94  public function count(): int
95  {
96  return count($this->raw_values);
97  }
98 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ByTrying.php:21
__construct(array $raw_values, Transformation $trafo)
A transformation is a function from one datatype to another.