Resource::__unserialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace kalanis\Restful;
4
5
6
use ArrayAccess;
7
use ArrayIterator;
8
use Exception;
9
use IteratorAggregate;
10
use Nette;
11
use Nette\MemberAccessException;
12
use Nette\Utils\Json;
13
use Serializable;
14
15
16
/**
17
 * REST resource
18
 * @package kalanis\Restful
19
 *
20
 * @property string $contentType Allowed result content type
21
 * @property array<string|int, mixed> $data
22
 * @template TK of string|int
23
 * @template TVal of mixed
24
 * @implements ArrayAccess<TK, TVal>
25
 * @implements IteratorAggregate<ArrayIterator<TK, TVal>>
26
 */
27 1
class Resource implements ArrayAccess, Serializable, IteratorAggregate, IResource
28
{
29
    use Nette\SmartObject {
30
        Nette\SmartObject::__get as SO__get;
31
        Nette\SmartObject::__set as SO__set;
32
        Nette\SmartObject::__isset as SO__isset;
33
        Nette\SmartObject::__unset as SO__unset;
34
    }
35
36
    /**
37
     * @param array<string|int, mixed> $data
38
     */
39 1
    public function __construct(
40
        private array $data = [],
41
    )
42
    {
43 1
    }
44
45
    /**
46
     * get info if the resource has some data set or is empty
47
     */
48
    public function hasData(): bool
49
    {
50
        return !empty($this->data);
51
    }
52
53
    /******************** Serializable ********************/
54
55
    public function __serialize(): array
56
    {
57
        return $this->data;
58
    }
59
60
    /**
61
     * @param array<string|int, mixed> $data
62
     * @return void
63
     */
64
    public function __unserialize(array $data): void
65
    {
66
        $this->data = $data;
67
    }
68
69
    /**
70
     * Serialize result set
71
     */
72
    public function serialize(): string
73
    {
74
        return Json::encode($this->data);
75
    }
76
77
    /**
78
     * Unserialize Resource
79
     */
80
    public function unserialize(string $data): void
81
    {
82
        $this->data = (array) Json::decode($data, true);
83
    }
84
85
    public function offsetExists(mixed $offset): bool
86
    {
87
        return isset($this->data[$offset]);
88
    }
89
90
    /******************** ArrayAccess interface ********************/
91
92
    public function offsetGet(mixed $offset): mixed
93
    {
94
        return $this->data[strval($offset)];
95
    }
96
97
    public function offsetSet(mixed $offset, mixed $value): void
98
    {
99 1
        if (is_null($offset)) {
100 1
            $offset = count($this->data);
101
        }
102 1
        $this->data[strval($offset)] = $value;
103 1
    }
104
105
    public function offsetUnset(mixed $offset): void
106
    {
107
        unset($this->data[strval($offset)]);
108
    }
109
110
    /**
111
     * Get resource data iterator
112
     * @return ArrayIterator<string|int, mixed>
113
     */
114
    public function getIterator(): ArrayIterator
115
    {
116
        return new ArrayIterator($this->getData());
117
    }
118
119
    /******************** Iterator aggregate interface ********************/
120
121
    /**
122
     * Get result set data
123
     * @return array<string|int, mixed>
124
     */
125
    public function getData(): array
126
    {
127 1
        return $this->data;
128
    }
129
130
    /******************** Magic methods ********************/
131
132
    /**
133
     * Magic getter from $this->data
134
     * @param string $name
135
     *
136
     * @throws Exception|MemberAccessException
137
     * @return mixed
138
     */
139
    public function &__get(string $name)
140
    {
141
        try {
142 1
            return $this->SO__get($name);
143 1
        } catch (MemberAccessException $e) {
144 1
            if (isset($this->data[$name])) {
145 1
                return $this->data[$name];
146
            }
147
            throw $e;
148
        }
149
150
    }
151
152
    /**
153
     * Magic setter to $this->data
154
     */
155
    public function __set(string $name, mixed $value)
156
    {
157
        try {
158 1
            $this->SO__set($name, $value);
159 1
        } catch (MemberAccessException) {
160 1
            $this->data[$name] = $value;
161
        }
162 1
    }
163
164
    /**
165
     * Magic isset to $this->data
166
     * @param string $name
167
     * @return bool
168
     */
169
    public function __isset(string $name)
170
    {
171
        return !$this->SO__isset($name) ? isset($this->data[$name]) : true;
172
    }
173
174
    /**
175
     * Magic unset from $this->data
176
     * @param string $name
177
     * @throws Exception|MemberAccessException
178
     */
179
    public function __unset(string $name)
180
    {
181
        try {
182
            $this->SO__unset($name);
183
        } catch (MemberAccessException $e) {
184
            if (isset($this->data[$name])) {
185
                unset($this->data[$name]);
186
                return;
187
            }
188
            throw $e;
189
        }
190
    }
191
}
192