ILIAS  release_8 Revision v8.24
KeyValueAccess.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
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
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}
offsetSet($offset, $value)
@inheritDoc
__construct(array $raw_values, Transformation $trafo)
offsetExists($offset)
@inheritDoc
offsetUnset($offset)
@inheritDoc
offsetGet($offset)
@inheritDoc
A transformation is a function from one datatype to another.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ByTrying.php:21