aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/UserDataAdapter.php
blob: 52771450dc5cb193c176dad68407d2368bbb059f (plain)
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
84
85
86
87
<?php
/**
 * Adapter to fake user_data property in UserManagement
 *
 * @author noack
 * @license GPL2
 */
class UserDataAdapter implements ArrayAccess, Countable, IteratorAggregate
{
    private $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * @param string $offset
     * @return string
     */
    public function adaptOffset($offset)
    {
        $adapted = trim(mb_strstr($offset, '.'), '.');
        return $adapted ?: $offset;
    }

    /**
     * ArrayAccess: Check whether the given offset exists.
     */
    public function offsetExists($offset): bool
    {
        return $this->user->offsetExists($this->adaptOffset($offset));
    }

    /**
     * ArrayAccess: Get the value at the given offset.
     */
    public function offsetGet($offset): mixed
    {
        return $this->user->offsetGet($this->adaptOffset($offset));
    }

    /**
     * ArrayAccess: Set the value at the given offset.
     */
    public function offsetSet($offset, $value): void
    {
        $this->user->offsetSet($this->adaptOffset($offset), $value);
    }

    /**
     * ArrayAccess: unset the value at the given offset.
     */
    public function offsetUnset($offset): void
    {
        $this->user->offsetUnset($this->adaptOffset($offset));
    }

    /**
     * @see Countable::count()
     */
    public function count(): int
    {
        return $this->user->count();
    }

    /**
     * @see IteratorAggregate::getIterator()
     */
    public function getIterator(): Traversable
    {
        return $this->user->getIterator();
    }

    /**
      * @param array $data
      * @param bool $reset
      */
    public function setData($data, $reset = false)
    {
        $adapted_data = [];
        foreach ($data as $k => $v) {
            $adapted_data[$this->adaptOffset($k)] = $v;
        }
        $this->user->setData($adapted_data, $reset);
    }
}