forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyValueAccess.php
83 lines (70 loc) · 2.06 KB
/
KeyValueAccess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/
declare(strict_types=1);
namespace ILIAS\Refinery;
use Closure;
use ArrayAccess;
use Countable;
class KeyValueAccess implements ArrayAccess, Countable
{
private array $raw_values;
private Transformation $trafo;
public function __construct(array $raw_values, Transformation $trafo)
{
$this->trafo = $trafo;
$this->raw_values = $raw_values;
}
public function offsetExists(mixed $offset): bool
{
return isset($this->raw_values[$offset]);
}
public function offsetGet(mixed $offset): mixed
{
if (!$this->offsetExists($offset)) {
return null;
}
return is_array($this->raw_values[$offset])
? array_map($this->getApplicator(), $this->raw_values[$offset])
: $this->getApplicator()($this->raw_values[$offset]);
}
private function getApplicator(): Closure
{
return function ($value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $this->getApplicator()($v);
}
return $value;
}
return $this->trafo->transform($value);
};
}
public function offsetSet(mixed $offset, mixed $value): void
{
$this->raw_values[$offset] = $value;
}
public function offsetUnset(mixed $offset): void
{
if ($this->offsetExists($offset)) {
unset($this->raw_values[$offset]);
}
}
public function count(): int
{
return count($this->raw_values);
}
}