1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Lyal\Checkr\Traits; |
5
|
|
|
|
6
|
|
|
use Lyal\Checkr\Exceptions\InvalidAttributeException; |
7
|
|
|
|
8
|
|
|
trait HasAttributes |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The entity's attributes. |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $attributes = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Results to not send with API requests |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
protected $hidden = []; |
23
|
|
|
|
24
|
|
|
public $checkFields = true; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param $key |
29
|
|
|
* @return \Illuminate\Support\Collection|mixed|null |
30
|
|
|
* @throws InvalidAttributeException |
31
|
|
|
*/ |
32
|
|
|
public function __get($key) |
33
|
|
|
{ |
34
|
|
|
if ($this->checkField($key) && array_key_exists($key, $this->attributes)) { |
35
|
|
|
return $this->attributes[$key] ?? null; |
36
|
|
|
} |
37
|
|
|
throw new InvalidAttributeException(get_class($this), $key); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set a value in our container, checking to make sure it's |
42
|
|
|
* a valid attribute |
43
|
|
|
* |
44
|
|
|
* @param $key |
45
|
|
|
* @param $value |
46
|
|
|
* @throws InvalidAttributeException |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function __set($key, $value) |
51
|
|
|
{ |
52
|
|
|
if (!$this->checkField($key)) { |
53
|
|
|
throw new InvalidAttributeException(get_class($this), $key); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->attributes[$key] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Check if a value is set in our container; |
61
|
|
|
* |
62
|
|
|
* @param $key |
63
|
|
|
* @throws InvalidAttributeException |
64
|
|
|
* |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
|
|
|
68
|
|
|
public function __isset($key) |
69
|
|
|
{ |
70
|
|
|
if (array_key_exists($key, $this->attributes)) { |
71
|
|
|
return null !== $this->attributes[$key]; |
72
|
|
|
} |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks if a field is valid for an entity - check always |
78
|
|
|
* returns true if $this->checkFields = false |
79
|
|
|
* |
80
|
|
|
* @param $field |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
|
84
|
|
|
public function checkField($field) : bool |
85
|
|
|
{ |
86
|
|
|
if ($field === 'include') { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($this->checkFields) { |
91
|
|
|
return |
92
|
|
|
in_array(str_singular($field) . '_id', $this->getFields(), false) |
|
|
|
|
93
|
|
|
|| in_array(str_singular($field) . '_ids', $this->getFields(), false) |
94
|
|
|
|| in_array($field, $this->getFields(), false) |
95
|
|
|
|| in_array($field, $this->getHidden(), false); |
96
|
|
|
} |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param bool $sanitized Remove hidden fields if true |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
|
105
|
|
|
public function getAttributes($sanitized = true) : array |
106
|
|
|
{ |
107
|
|
|
$container = $this->attributes; |
108
|
|
|
if ($sanitized) { |
109
|
|
|
foreach ($this->getHidden() as $key) { |
110
|
|
|
unset($container[$key]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
return $container; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets the attribute values to the contents of the given array |
118
|
|
|
* |
119
|
|
|
* Note: this is not API-safe, as it ignores the $fields array |
120
|
|
|
* |
121
|
|
|
* @param array $values |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
protected function setAttributes(array $values) : array |
125
|
|
|
{ |
126
|
|
|
return $this->attributes = $values; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Getter for our attribute container |
132
|
|
|
* |
133
|
|
|
* @param $key |
134
|
|
|
* @return mixed|null |
135
|
|
|
*/ |
136
|
|
|
|
137
|
|
|
protected function getAttribute($key) |
138
|
|
|
{ |
139
|
|
|
if (isset($this->attributes[$key])) { |
140
|
|
|
return $this->attributes[$key]; |
141
|
|
|
} |
142
|
|
|
return null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Setter for our attribute container |
147
|
|
|
* |
148
|
|
|
* @param $key |
149
|
|
|
* @param |
150
|
|
|
* @return mixed|null |
151
|
|
|
*/ |
|
|
|
|
152
|
|
|
|
153
|
|
|
protected function setAttribute($key, $value) |
154
|
|
|
{ |
155
|
|
|
return $this->attributes[$key] = $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the hidden attributes array that should not be sent in requests |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
|
164
|
|
|
protected function getHidden() |
165
|
|
|
{ |
166
|
|
|
return $this->hidden; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set a hidden attributes array |
171
|
|
|
* |
172
|
|
|
* @param $key |
173
|
|
|
* @param $name |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
|
|
|
177
|
|
|
protected function setHidden($hidden) |
178
|
|
|
{ |
179
|
|
|
return $this->hidden = $hidden; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |