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
|
|
|
* Get all of the attributes. |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
4 |
|
public function getAttributes() |
22
|
|
|
{ |
23
|
4 |
|
return $this->attributes; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Determine if the given attribute value exists. |
28
|
|
|
* |
29
|
|
|
* @param string $key |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
4 |
|
public function has($key) |
33
|
|
|
{ |
34
|
4 |
|
return Arr::has($this->attributes, $key); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the specified attribute. |
39
|
|
|
* |
40
|
|
|
* @param string $key |
41
|
|
|
* @param mixed $default |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
4 |
|
public function get($key, $default = null) |
45
|
|
|
{ |
46
|
4 |
|
return Arr::get($this->attributes, $key, $default); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set the given attributes. |
51
|
|
|
* |
52
|
|
|
* @param string|array $key |
53
|
|
|
* @param mixed $value |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
4 |
|
public function set($key, $value = null) |
57
|
|
|
{ |
58
|
4 |
|
$data = is_array($key) ? $key : [$key => $value]; |
59
|
|
|
|
60
|
4 |
|
foreach ($data as $key => $value) { |
61
|
4 |
|
Arr::set($this->attributes, $key, $value); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
4 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Remove the given attributes. |
69
|
|
|
* |
70
|
|
|
* @param string|array $keys |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
4 |
|
public function remove($keys) |
74
|
|
|
{ |
75
|
4 |
|
$keys = is_array($keys) ? $keys : func_get_args(); |
76
|
|
|
|
77
|
4 |
|
Arr::forget($this->attributes, $keys); |
78
|
|
|
|
79
|
4 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Remove all attributes. |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
4 |
|
public function flush() |
88
|
|
|
{ |
89
|
4 |
|
$this->attributes = []; |
90
|
|
|
|
91
|
4 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Convert the instance to an array. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
4 |
|
public function toArray() |
100
|
|
|
{ |
101
|
4 |
|
return $this->getAttributes(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Convert the object into something JSON serializable. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
4 |
|
public function jsonSerialize() |
110
|
|
|
{ |
111
|
4 |
|
return $this->toArray(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Convert the instance to JSON. |
116
|
|
|
* |
117
|
|
|
* @param int $options |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
4 |
|
public function toJson($options = 0) |
121
|
|
|
{ |
122
|
4 |
|
return json_encode($this->jsonSerialize(), $options); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Determine if the given offset exists. |
127
|
|
|
* |
128
|
|
|
* @param string $offset |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
4 |
|
public function offsetExists($offset) |
132
|
|
|
{ |
133
|
4 |
|
return $this->has($offset); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the value for a given offset. |
138
|
|
|
* |
139
|
|
|
* @param string $offset |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
4 |
|
public function offsetGet($offset) |
143
|
|
|
{ |
144
|
4 |
|
return $this->get($offset); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the value at the given offset. |
149
|
|
|
* |
150
|
|
|
* @param string $offset |
151
|
|
|
* @param mixed $value |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
4 |
|
public function offsetSet($offset, $value) |
155
|
|
|
{ |
156
|
4 |
|
$this->set($offset, $value); |
157
|
4 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Unset the value at the given offset. |
161
|
|
|
* |
162
|
|
|
* @param string $offset |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
4 |
|
public function offsetUnset($offset) |
166
|
|
|
{ |
167
|
4 |
|
$this->remove($offset); |
168
|
4 |
|
} |
169
|
|
|
} |
170
|
|
|
|