Passed
Push — master ( 8cf9c5...df1ad3 )
by Guillermo A.
02:14
created

AbstractModel::toArray()   A

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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
namespace Guillermoandrae\Models;
4
5
use Guillermoandrae\Common\DumpableTrait;
6
use Guillermoandrae\Common\AbstractAggregate;
7
8
abstract class AbstractModel implements ModelInterface
9
{
10
    use DumpableTrait;
11 1
12
    /**
13 1
     * @var array
14
     */
15
    protected $data = [];
16
17
    /**
18
     * Undocumented function
19
     *
20
     * @param [type] $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
21
     */
22
    public function __construct($data = null)
23
    {
24
        if (is_array($data)) {
25
            foreach ($data as $key => $value) {
26
                $this->data[$key] = $value;
27
            }
28
        }
29
    }
30
31
    final public function offsetExists($offset)
32
    {
33
        return isset($this->data[$offset]);
34
    }
35
36
    final public function offsetGet($offset)
37
    {
38
        return $this->offsetExists($offset) ? $this->data[$offset] : null;
39
    }
40
41
    final public function offsetSet($offset, $value)
42
    {
43
        if (is_null($offset)) {
44
            $this->data[] = $value;
45
        } else {
46
            $this->data[$offset] = $value;
47
        }
48
    }
49
50
    final public function offsetUnset($offset)
51
    {
52
        unset($this->data[$offset]);
53
    }
54
55
    final public function toJson(): string
56
    {
57
        return json_encode($this->toArray());
58
    }
59
60
    public function toArray(): array
61
    {
62
        return $this->data;
63
    }
64
}
65