1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 Dirk Groenen |
4
|
|
|
* |
5
|
|
|
* (c) Dirk Groenen <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DirkGroenen\Pinterest\Models; |
12
|
|
|
|
13
|
|
|
use DirkGroenen\Pinterest\Exceptions\PinterestException; |
14
|
|
|
use DirkGroenen\Pinterest\Pinterest\Transport\Response; |
15
|
|
|
use DirkGroenen\Pinterest\Pinterest; |
16
|
|
|
|
17
|
|
|
class Model implements \JsonSerializable { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The model's attributes |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $attributes = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The available object keys |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $fillable = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Instance of the master Pinterest class |
35
|
|
|
* |
36
|
|
|
* @var Pinterest |
37
|
|
|
*/ |
38
|
|
|
protected $master; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new model instance |
42
|
|
|
* |
43
|
|
|
* @param Pinterest $master |
44
|
|
|
* @param mixed $modeldata |
45
|
|
|
*/ |
46
|
31 |
|
public function __construct(Pinterest $master, $modeldata = null) |
47
|
|
|
{ |
48
|
31 |
|
$this->master = $master; |
49
|
|
|
|
50
|
|
|
// Fill the model |
51
|
31 |
|
if (is_array($modeldata)) { |
52
|
20 |
|
$this->fill($modeldata); |
53
|
|
|
} |
54
|
11 |
|
else if ($modeldata instanceof \DirkGroenen\Pinterest\Transport\Response) { |
55
|
11 |
|
$this->fill($modeldata->data); |
56
|
|
|
} |
57
|
31 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the model's attribute |
61
|
|
|
* |
62
|
|
|
* @access public |
63
|
|
|
* @param string $key |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
19 |
|
public function __get($key) |
67
|
|
|
{ |
68
|
19 |
|
return isset($this->attributes[$key]) ? $this->attributes[$key] : null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the model's attribute |
73
|
|
|
* |
74
|
|
|
* @access public |
75
|
|
|
* @param string $key |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @throws Exceptions\PinterestException |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function __set($key, $value) |
81
|
|
|
{ |
82
|
|
|
if ($this->isFillable($key)) { |
83
|
|
|
$this->attributes[$key] = $value; |
84
|
|
|
} else { |
85
|
|
|
throw new PinterestException(sprintf("%s is not a fillable attribute.", $key)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check if the model's attribute is set |
91
|
|
|
* |
92
|
|
|
* @param $key |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
1 |
|
public function __isset($key) |
96
|
|
|
{ |
97
|
1 |
|
return array_key_exists($key, $this->attributes); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Fill the attributes |
102
|
|
|
* |
103
|
|
|
* @access private |
104
|
|
|
* @param array $attributes |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
31 |
|
private function fill(array $attributes) |
108
|
|
|
{ |
109
|
31 |
|
foreach ($attributes as $key => $value) { |
110
|
31 |
|
if ($this->isFillable($key)) { |
111
|
31 |
|
$this->attributes[$key] = $value; |
112
|
|
|
} |
113
|
|
|
} |
114
|
31 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if the key is fillable |
118
|
|
|
* |
119
|
|
|
* @access public |
120
|
|
|
* @param string $key |
121
|
|
|
* @return boolean |
122
|
|
|
*/ |
123
|
31 |
|
public function isFillable($key) |
124
|
|
|
{ |
125
|
31 |
|
return in_array($key, $this->fillable); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Convert the model instance to an array |
130
|
|
|
* |
131
|
|
|
* @access public |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
4 |
|
public function toArray() |
135
|
|
|
{ |
136
|
4 |
|
$array = array(); |
137
|
|
|
|
138
|
4 |
|
foreach ($this->fillable as $key) { |
139
|
4 |
|
$array[$key] = $this->{$key}; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
return $array; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Convert the model instance to JSON |
147
|
|
|
* |
148
|
|
|
* @access public |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
1 |
|
public function toJson() |
152
|
|
|
{ |
153
|
1 |
|
return json_encode($this->toArray(), true); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Convert the object into something JSON serializable. |
158
|
|
|
* |
159
|
|
|
* @access public |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function jsonSerialize() |
163
|
|
|
{ |
164
|
|
|
return $this->toArray(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Convert the model to its string representation |
169
|
|
|
* |
170
|
|
|
* @access public |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function __toString() |
174
|
|
|
{ |
175
|
|
|
return $this->toJson(); |
176
|
|
|
} |
177
|
|
|
} |