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

GenericEntity   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 46
rs 10
c 1
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 7 2
A offsetUnset() 0 7 2
A getId() 0 3 1
A offsetGet() 0 3 1
A offsetExists() 0 3 1
A __construct() 0 7 1
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