1
|
|
|
<?php namespace Peek\Api; |
2
|
|
|
|
3
|
|
|
use JsonSerializable; |
4
|
|
|
|
5
|
|
|
class Model implements JsonSerializable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
protected $attributes = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Model constructor. |
14
|
|
|
* @param null $attributes |
15
|
|
|
*/ |
16
|
|
|
public function __construct($attributes = null) |
17
|
|
|
{ |
18
|
|
|
if (is_array($attributes)) { |
19
|
|
|
$this->fill($attributes); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Fill the attributes |
25
|
|
|
* @param array $attributes |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
private function fill(array $attributes) |
29
|
|
|
{ |
30
|
|
|
foreach ($attributes as $key => $value) { |
31
|
|
|
$this->setAttribute($key, $value); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set a given attribute on the model. |
37
|
|
|
* @param string $key |
38
|
|
|
* @param mixed $value |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function setAttribute($key, $value) |
42
|
|
|
{ |
43
|
|
|
$this->attributes[$key] = $value; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function jsonSerialize() |
52
|
|
|
{ |
53
|
|
|
return $this->toArray(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function toArray() |
60
|
|
|
{ |
61
|
|
|
return $this->attributes; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function __get($name) |
69
|
|
|
{ |
70
|
|
|
return $this->getAttribute($name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $name |
75
|
|
|
* @param mixed $value |
76
|
|
|
*/ |
77
|
|
|
public function __set($name, $value) |
78
|
|
|
{ |
79
|
|
|
$this->setAttribute($name, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get an attribute from the $attributes array. |
84
|
|
|
* @param string $key |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public function getAttribute($key) |
88
|
|
|
{ |
89
|
|
|
$value = $this->getAttributeValue($key); |
90
|
|
|
|
91
|
|
|
return $value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get an attribute from the $attributes array. |
96
|
|
|
* @param string $key |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
protected function getAttributeValue($key) |
100
|
|
|
{ |
101
|
|
|
if (array_key_exists($key, $this->attributes)) { |
102
|
|
|
return $this->attributes[$key]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $key |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function __isset($key) |
113
|
|
|
{ |
114
|
|
|
return array_key_exists($key, $this->attributes); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function __toString() |
121
|
|
|
{ |
122
|
|
|
return $this->toJson(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function toJson() |
129
|
|
|
{ |
130
|
|
|
return json_encode($this->toArray(), true); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|