Completed
Branch master (a17b64)
by Rémi
15:50
created

MagicCasting::attributesToArray()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 5
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
namespace Analogue\ORM;
4
5
use Carbon\Carbon;
6
use ProxyManager\Proxy\ProxyInterface;
7
use Illuminate\Contracts\Support\Arrayable;
8
9
trait MagicCasting
10
{
11
12
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
13
     * Determine if the given attribute exists.
14
     *
15
     * @param  mixed $offset
16
     * @return bool
17
     */
18
    public function offsetExists($offset)
19
    {
20
        return isset($this->$offset);
21
    }
22
23
    /**
24
     * Get the value for a given offset.
25
     *
26
     * @param  mixed $offset
27
     * @return mixed
28
     */
29
    public function offsetGet($offset)
30
    {
31
        return $this->$offset;
32
    }
33
34
    /**
35
     * Set the value for a given offset.
36
     *
37
     * @param  mixed $offset
38
     * @param  mixed $value
39
     * @return void
40
     */
41
    public function offsetSet($offset, $value)
42
    {
43
        $this->$offset = $value;
44
    }
45
46
    /**
47
     * Unset the value for a given offset.
48
     *
49
     * @param  mixed $offset
50
     * @return void
51
     */
52
    public function offsetUnset($offset)
53
    {
54
        unset($this->$offset);
55
    }
56
57
    /**
58
     * Convert the object into something JSON serializable.
59
     *
60
     * @return array
61
     */
62
    public function jsonSerialize()
63
    {
64
        return $this->toArray();
65
    }
66
67
    /**
68
     * Convert the entity instance to JSON.
69
     *
70
     * @param  int $options
71
     * @return string
72
     */
73
    public function toJson($options = 0)
74
    {
75
        return json_encode($this->toArray(), $options);
76
    }
77
78
    /**
79
     * Convert Mappable object to array;
80
     *
81
     * @return array
82
     */
83
    public function toArray()
84
    {
85
        return $this->attributesToArray($this->attributes);
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
86
    }
87
88
    /**
89
     * Transform the Object to array/json,
90
     *
91
     * @param  array $sourceAttributes
92
     * @return array
93
     */
94
    protected function attributesToArray(array $sourceAttributes)
95
    {
96
        $attributes = [];
97
98
        foreach ($sourceAttributes as $key => $attribute) {
99
            // If the attribute is a proxy, and hasn't be loaded, we discard
100
            // it from the returned set.
101
            if ($attribute instanceof ProxyInterface && !$attribute->isProxyInitialized()) {
102
                continue;
103
            }
104
105
            if ($attribute instanceof Carbon) {
106
                $attributes[$key] = $attribute->__toString();
107
                continue;
108
            }
109
110
            if ($attribute instanceof Arrayable) {
111
                $attributes[$key] = $attribute->toArray();
112
            } else {
113
                $attributes[$key] = $attribute;
114
            }
115
        }
116
        return $attributes;
117
    }
118
}
119