1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\DataObjects\Behaviors\WithAttributes; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait HasAttributesTrait |
7
|
|
|
* @package ByTIC\DataObjects\Behaviors\WithAttributes |
8
|
|
|
*/ |
9
|
|
|
trait HasAttributesTrait |
10
|
|
|
{ |
11
|
|
|
protected $attributes = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return mixed |
15
|
|
|
*/ |
16
|
18 |
|
public function getAttributes() |
17
|
|
|
{ |
18
|
18 |
|
return $this->attributes; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param $key |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
3 |
|
public function hasAttribute($key): bool |
26
|
|
|
{ |
27
|
3 |
|
foreach ((array)$key as $prop) { |
28
|
3 |
|
if ($this->getAttribute($prop) === null) { |
29
|
2 |
|
return false; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $key |
38
|
|
|
* @param null $default |
|
|
|
|
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
10 |
|
public function getPropertyRaw(string $key, $default = null) |
42
|
|
|
{ |
43
|
10 |
|
if (property_exists($this, $key)) { |
44
|
3 |
|
return $this->{$key}; |
45
|
|
|
} |
46
|
7 |
|
return $this->getAttribute($key, $default); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get an attribute from the model. |
51
|
|
|
* |
52
|
|
|
* @param string $key |
53
|
|
|
* @return mixed|void |
54
|
|
|
*/ |
55
|
11 |
|
public function getAttribute(string $key, $default = null) |
56
|
|
|
{ |
57
|
11 |
|
if (empty($key)) { |
58
|
6 |
|
throw new \InvalidArgumentException("Please provide a key argument"); |
59
|
|
|
} |
60
|
10 |
|
if (!isset($this->attributes[$key])) { |
61
|
|
|
return $default; |
62
|
|
|
} |
63
|
|
|
return $this->attributes[$key]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $key |
68
|
14 |
|
* @param $value |
69
|
|
|
* @return static |
70
|
14 |
|
*/ |
71
|
8 |
|
public function setPropertyValue($key, $value) |
72
|
|
|
{ |
73
|
14 |
|
if (method_exists($this, 'transformInboundValue')) { |
74
|
14 |
|
$value = $this->transformInboundValue($key, $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (property_exists($this, $key)) { |
78
|
|
|
$this->{$key} = $value; |
79
|
|
|
} |
80
|
|
|
$this->setAttribute($key, $value); |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
15 |
|
|
84
|
|
|
/** |
85
|
15 |
|
* @param $key |
86
|
15 |
|
* @param $value |
87
|
|
|
* @return $this |
88
|
|
|
* @noinspection PhpMissingReturnTypeInspection |
89
|
|
|
*/ |
90
|
|
|
public function setAttribute($key, $value) |
91
|
|
|
{ |
92
|
1 |
|
$this->attributes[$key] = $value; |
93
|
|
|
return $this; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
1 |
|
* @param $prop |
98
|
1 |
|
*/ |
99
|
|
|
protected function unsetProperty($prop) |
100
|
|
|
{ |
101
|
|
|
if (property_exists($this, $prop)) { |
102
|
|
|
unset($this->{$prop}); |
103
|
1 |
|
} |
104
|
|
|
$this->unsetAttribute($prop); |
105
|
1 |
|
} |
106
|
1 |
|
|
107
|
|
|
/** |
108
|
1 |
|
* @param $key |
109
|
|
|
*/ |
110
|
|
|
public function unsetAttribute($key) |
111
|
|
|
{ |
112
|
|
|
if ($this->hasAttribute($key)) { |
113
|
|
|
unset($this->attributes[$key]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|