Passed
Push — master ( d50d64...3e68ba )
by Maike
02:16
created

Model::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace MocOrm\Support;
4
5
/**
6
 * Model
7
 */
8
abstract class Model extends \MocOrm\Model\Model implements \ArrayAccess
9
{
10
    /**
11
     * Gets model data.
12
     *
13
     * @return array
14
     */
15
    public function toArray()
16
    {
17
        return $this->getData();
18
    }
19
20
    /**
21
     * Gets model data through static method.
22
     *
23
     * @param  mixed $data
24
     * @return array
25
     */
26
    public static function toList($data)
27
    {
28
        if (!($data instanceof self)) throw new \Exception(" It's not a model.");
29
30
        $data = array_map(function ($object) { return $object->toArray(); }, $data);
0 ignored issues
show
Bug introduced by
$data of type self is incompatible with the type array expected by parameter $arr1 of array_map(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        $data = array_map(function ($object) { return $object->toArray(); }, /** @scrutinizer ignore-type */ $data);
Loading history...
31
32
        return $data;
33
    }
34
35
    /**
36
     * Get first model from query result.
37
     *
38
     * @return mixed
39
     */
40
    public function first()
41
    {
42
        return current($this->getData());
43
    }
44
45
    /**
46
     * ArrayAccess Interface.
47
     */
48
    public function offsetExists($offset)
49
    {
50
        $data = $this->getData();
51
52
        return isset($data[$offset]);
53
    }
54
55
    public function offsetGet($offset)
56
    {
57
        $data = $this->getData();
58
59
        return isset($data[$offset]) ? $data[$offset] : null;
60
    }
61
62
    public function offsetSet($offset, $value)
63
    {
64
        $data = $this->getData();
65
66
        if (is_null($offset)) {
67
            $data[] = $value;
68
        } else {
69
            $data[$offset] = $value;
70
        }
71
    }
72
73
    public function offsetUnset($offset)
74
    {
75
        $data = $this->getData();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
76
77
        unset($this->data[$offset]);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist on MocOrm\Support\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
78
    }
79
}
80