|
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
|
|
|
/** |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|