1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Attributes; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
trait HasAttributesTrait |
8
|
|
|
{ |
9
|
|
|
protected $attributes = []; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Set a given log value. |
13
|
|
|
* |
14
|
|
|
* @param array|string $key |
15
|
|
|
* @param mixed $value |
16
|
|
|
* |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
public function setAttribute($key, $value = null) |
20
|
|
|
{ |
21
|
|
|
$keys = is_array($key) ? $key : [$key => $value]; |
22
|
|
|
|
23
|
|
|
foreach ($keys as $key => $value) { |
24
|
|
|
Arr::set($this->attributes, $key, $value); |
25
|
|
|
} |
26
|
|
|
return true; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the specified log value. |
31
|
|
|
* |
32
|
|
|
* @param null|string $key |
33
|
|
|
* @param mixed $default |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function getAttribute($key = null, $default = null) |
38
|
|
|
{ |
39
|
|
|
return Arr::get($this->attributes, $key, $default); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine if the given key exists in the attributes. |
44
|
|
|
* |
45
|
|
|
* @param string|int $key |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function existsAttribute($key) |
50
|
|
|
{ |
51
|
|
|
return Arr::exists($this->attributes, $key); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if an key or keys exist in the attributes using "dot" notation. |
56
|
|
|
* |
57
|
|
|
* @param string|array $keys |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function hasAttribute($keys) |
62
|
|
|
{ |
63
|
|
|
return Arr::has($this->attributes, $keys); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Convert the Object instance to an array. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function toArray() |
72
|
|
|
{ |
73
|
|
|
return $this->attributes; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Convert the object into something JSON serializable. |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function jsonSerialize() |
82
|
|
|
{ |
83
|
|
|
return $this->toArray(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Convert the Object instance to JSON. |
88
|
|
|
* |
89
|
|
|
* @param int $options |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function toJson($options = 0) |
93
|
|
|
{ |
94
|
|
|
return json_encode($this->jsonSerialize(), $options); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|