Completed
Push — master ( 18f9b1...e2cca3 )
by Dawid
07:34
created

GenericEntity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
use Igni\Storage\Id\GenericId;
4
use Igni\Storage\Entity;
5
6
class GenericEntity implements Entity, ArrayAccess
7
{
8
    private $id;
9
    private $idKey;
10
    private $data;
11
12
    public function __construct(array $data, string $idKey = 'id')
13
    {
14
        $this->idKey = $idKey;
15
16
        $id = $data[$this->idKey] ?? null;
17
        $this->id = new GenericId($id);
18
        $this->data = $data;
19
    }
20
21
    public function getId(): \Igni\Storage\Id
22
    {
23
        return $this->id;
24
    }
25
26
    public function offsetExists($offset)
27
    {
28
        return isset($this->data[$offset]);
29
    }
30
31
    public function offsetGet($offset)
32
    {
33
        return $this->data[$offset] ?? null;
34
    }
35
36
    public function offsetSet($offset, $value)
37
    {
38
        if ($offset === $this->idKey) {
39
            return;
40
        }
41
42
        $this->data[$offset] = $value;
43
    }
44
45
    public function offsetUnset($offset)
46
    {
47
        if ($offset === $this->idKey) {
48
            return;
49
        }
50
51
        unset($this->data[$offset]);
52
    }
53
}
54