This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Model.php
154 lines (120 loc) · 3.19 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Destiny;
use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use JsonSerializable;
/**
* Class Model.
*/
abstract class Model implements JsonSerializable, Arrayable, ArrayAccess
{
protected $properties = [];
protected $appends = [];
protected $cached = [];
public function __construct(array $properties)
{
$this->properties = $properties;
}
public function jsonSerialize()
{
return $this->toArray();
}
public function __toString()
{
return json_encode($this->toArray());
}
public function __get($key)
{
return $this->getProperty($key);
}
public function __set($key, $value)
{
$mutator = 's'.$key;
if (is_callable([$this, $mutator])) {
$this->$mutator($value);
} else {
$this->setProperty($key, $value);
}
}
public function __isset($key)
{
return isset($this->properties[$key]) || isset($this->cached[$key]);
}
public function __unset($key)
{
unset($this->properties[$key]);
}
public function offsetExists($offset)
{
return $this->__isset($offset);
}
public function offsetGet($offset)
{
return $this->__get($offset);
}
public function offsetSet($offset, $value)
{
$this->__set($offset, $value);
}
public function offsetUnset($offset)
{
$this->__unset($offset);
}
public function getProperties()
{
$properties = [];
foreach ($this->properties as $key => $value) {
$properties[$key] = $this->$key;
}
foreach ($this->appends as $key) {
$properties[$key] = $this->$key;
}
return $properties;
}
protected function getProperty($key)
{
if (isset($this->cached[$key])) {
return $this->cached[$key];
}
$value = (isset($this->properties[$key]) ? $this->properties[$key] : null);
$mutator = 'g'.ucfirst($key);
if (is_callable([$this, $mutator])) {
$this->cached[$key] = $value = $this->$mutator($value);
}
return $value;
}
protected function getNonMutatedProperty($key)
{
if (isset($this->cached[$key])) {
return $this->cached[$key];
}
$value = (isset($this->properties[$key]) ? $this->properties[$key] : null);
return $value;
}
protected function setProperty($key, $value)
{
$this->properties[$key] = $value;
}
public function setCachedProperty($key, $value)
{
return $this->cached[$key] = $value;
}
protected function newCollection(array $items = [])
{
return new Collection($items);
}
public function toArray()
{
$array = [];
$properties = array_merge(array_keys($this->properties), $this->appends);
foreach ($properties as $key) {
$property = $this->getProperty($key);
if ($property instanceof Arrayable) {
$property = $property->toArray();
}
$array[$key] = $property;
}
return $array;
}
}