ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
KeyValueAccess.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
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 
38  public function offsetExists(mixed $offset): bool
39  {
40  return isset($this->raw_values[$offset]);
41  }
42 
43  public function offsetGet(mixed $offset): mixed
44  {
45  if (!$this->offsetExists($offset)) {
46  return null;
47  }
48 
49  return is_array($this->raw_values[$offset])
50  ? array_map($this->getApplicator(), $this->raw_values[$offset])
51  : $this->getApplicator()($this->raw_values[$offset]);
52  }
53 
54  private function getApplicator(): Closure
55  {
56  return function ($value) {
57  if (is_array($value)) {
58  foreach ($value as $k => $v) {
59  $value[$k] = $this->getApplicator()($v);
60  }
61  return $value;
62  }
63  return $this->trafo->transform($value);
64  };
65  }
66 
67  public function offsetSet(mixed $offset, mixed $value): void
68  {
69  $this->raw_values[$offset] = $value;
70  }
71 
72  public function offsetUnset(mixed $offset): void
73  {
74  if ($this->offsetExists($offset)) {
75  unset($this->raw_values[$offset]);
76  }
77  }
78 
79  public function count(): int
80  {
81  return count($this->raw_values);
82  }
83 }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
offsetSet(mixed $offset, mixed $value)
__construct(array $raw_values, Transformation $trafo)
A transformation is a function from one datatype to another.