ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
KeyValueAccess.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Refinery;
22
23use Closure;
24use ArrayAccess;
25use Countable;
26
27class KeyValueAccess implements ArrayAccess, Countable
28{
29 private array $raw_values;
31
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}
__construct(array $raw_values, Transformation $trafo)
offsetSet(mixed $offset, mixed $value)
A transformation is a function from one datatype to another.