1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Agent\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
trait HasAttributes |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* All of the attributes set. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $attributes = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Determine if the given attribute value exists. |
18
|
|
|
* |
19
|
|
|
* @param string $key |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
|
|
public function has($key) |
23
|
|
|
{ |
24
|
|
|
return Arr::has($this->attributes, $key); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the specified attribute. |
29
|
|
|
* |
30
|
|
|
* @param string|null $key |
31
|
|
|
* @param mixed $default |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function get($key = null, $default = null) |
35
|
|
|
{ |
36
|
|
|
return Arr::get($this->attributes, $key, $default); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the given attributes. |
41
|
|
|
* |
42
|
|
|
* @param string|array $key |
43
|
|
|
* @param mixed $value |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function set($key, $value = null) |
47
|
|
|
{ |
48
|
|
|
$data = is_array($key) ? $key : [$key => $value]; |
49
|
|
|
|
50
|
|
|
foreach ($data as $key => $value) { |
51
|
|
|
Arr::set($this->attributes, $key, $value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Remove the given attributes. |
59
|
|
|
* |
60
|
|
|
* @param string|array $keys |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function remove($keys) |
64
|
|
|
{ |
65
|
|
|
$keys = is_array($keys) ? $keys : func_get_args(); |
66
|
|
|
|
67
|
|
|
Arr::forget($this->attributes, $keys); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Remove all attributes. |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function flush() |
78
|
|
|
{ |
79
|
|
|
$this->attributes = []; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Convert the instance to an array. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function toArray() |
90
|
|
|
{ |
91
|
|
|
return $this->attributes; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Convert the object into something JSON serializable. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function jsonSerialize() |
100
|
|
|
{ |
101
|
|
|
return $this->toArray(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Convert the instance to JSON. |
106
|
|
|
* |
107
|
|
|
* @param int $options |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function toJson($options = 0) |
111
|
|
|
{ |
112
|
|
|
return json_encode($this->jsonSerialize(), $options); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Determine if the given offset exists. |
117
|
|
|
* |
118
|
|
|
* @param string $offset |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function offsetExists($offset) |
122
|
|
|
{ |
123
|
|
|
return $this->has($offset); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the value for a given offset. |
128
|
|
|
* |
129
|
|
|
* @param string $offset |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function offsetGet($offset) |
133
|
|
|
{ |
134
|
|
|
return $this->get($offset); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the value at the given offset. |
139
|
|
|
* |
140
|
|
|
* @param string $offset |
141
|
|
|
* @param mixed $value |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function offsetSet($offset, $value) |
145
|
|
|
{ |
146
|
|
|
$this->set($offset, $value); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Unset the value at the given offset. |
151
|
|
|
* |
152
|
|
|
* @param string $offset |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function offsetUnset($offset) |
156
|
|
|
{ |
157
|
|
|
$this->remove($offset); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Dynamically check if an attribute is set. |
162
|
|
|
* |
163
|
|
|
* @param string $key |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function __isset($key) |
167
|
|
|
{ |
168
|
|
|
return $this->offsetExists($key); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Dynamically retrieve the value of an attribute. |
173
|
|
|
* |
174
|
|
|
* @param string $key |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
public function __get($key) |
178
|
|
|
{ |
179
|
|
|
return $this->offsetGet($key); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Dynamically set the value of an attribute. |
184
|
|
|
* |
185
|
|
|
* @param string $key |
186
|
|
|
* @param mixed $value |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function __set($key, $value) |
190
|
|
|
{ |
191
|
|
|
$this->offsetSet($key, $value); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Dynamically unset an attribute. |
196
|
|
|
* |
197
|
|
|
* @param string $key |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
public function __unset($key) |
201
|
|
|
{ |
202
|
|
|
$this->offsetUnset($key); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|