1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* File: Attribute.php |
4
|
|
|
* Category: - |
5
|
|
|
* Author: M. Goldenbaum |
6
|
|
|
* Created: 01.01.21 20:17 |
7
|
|
|
* Updated: - |
8
|
|
|
* |
9
|
|
|
* Description: |
10
|
|
|
* - |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Webklex\PHPIMAP; |
14
|
|
|
|
15
|
|
|
use ArrayAccess; |
16
|
|
|
use Carbon\Carbon; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Attribute |
20
|
|
|
* |
21
|
|
|
* @package Webklex\PHPIMAP |
22
|
|
|
*/ |
23
|
|
|
class Attribute implements ArrayAccess { |
24
|
|
|
|
25
|
|
|
/** @var string $name */ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Value holder |
30
|
|
|
* |
31
|
|
|
* @var array $values |
32
|
|
|
*/ |
33
|
|
|
protected $values = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Attribute constructor. |
37
|
|
|
* @param string $name |
38
|
|
|
* @param array|mixed $value |
39
|
|
|
*/ |
40
|
|
|
public function __construct($name, $value = null) { |
41
|
|
|
$this->setName($name); |
42
|
|
|
$this->add($value); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return the stringified attribute |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function __toString() { |
52
|
|
|
return implode(", ", $this->values); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return the stringified attribute |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function toString(){ |
61
|
|
|
return $this->__toString(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Convert instance to array |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function toArray(){ |
70
|
|
|
return $this->values; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Convert first value to a date object |
75
|
|
|
* |
76
|
|
|
* @return Carbon|null |
77
|
|
|
*/ |
78
|
|
|
public function toDate(){ |
79
|
|
|
$date = $this->first(); |
80
|
|
|
if ($date instanceof Carbon) return $date; |
81
|
|
|
|
82
|
|
|
return Carbon::parse($date); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Determine if a value exists at an offset. |
87
|
|
|
* |
88
|
|
|
* @param mixed $key |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function offsetExists($key) { |
92
|
|
|
return array_key_exists($key, $this->values); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a value at a given offset. |
97
|
|
|
* |
98
|
|
|
* @param mixed $key |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function offsetGet($key) { |
102
|
|
|
return $this->values[$key]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the value at a given offset. |
107
|
|
|
* |
108
|
|
|
* @param mixed $key |
109
|
|
|
* @param mixed $value |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function offsetSet($key, $value) { |
113
|
|
|
if (is_null($key)) { |
114
|
|
|
$this->values[] = $value; |
115
|
|
|
} else { |
116
|
|
|
$this->values[$key] = $value; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Unset the value at a given offset. |
122
|
|
|
* |
123
|
|
|
* @param string $key |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function offsetUnset($key) { |
127
|
|
|
unset($this->values[$key]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Add one or more values to the attribute |
132
|
|
|
* @param array|mixed $value |
133
|
|
|
* @param boolean $strict |
134
|
|
|
* |
135
|
|
|
* @return Attribute |
136
|
|
|
*/ |
137
|
|
|
public function add($value, $strict = false) { |
138
|
|
|
if (is_array($value)) { |
139
|
|
|
return $this->merge($value, $strict); |
140
|
|
|
}elseif ($value !== null) { |
141
|
|
|
$this->attach($value, $strict); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Merge a given array of values with the current values array |
149
|
|
|
* @param array $values |
150
|
|
|
* @param boolean $strict |
151
|
|
|
* |
152
|
|
|
* @return Attribute |
153
|
|
|
*/ |
154
|
|
|
public function merge($values, $strict = false) { |
155
|
|
|
if (is_array($values)) { |
|
|
|
|
156
|
|
|
foreach ($values as $value) { |
157
|
|
|
$this->attach($value, $strict); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check if the attribute contains the given value |
166
|
|
|
* @param mixed $value |
167
|
|
|
* |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
public function contains($value) { |
171
|
|
|
foreach ($this->values as $v) { |
172
|
|
|
if ($v === $value) { |
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Attach a given value to the current value array |
181
|
|
|
* @param $value |
182
|
|
|
* @param bool $strict |
183
|
|
|
*/ |
184
|
|
|
public function attach($value, $strict = false) { |
185
|
|
|
if ($strict === true) { |
186
|
|
|
if ($this->contains($value) === false) { |
187
|
|
|
$this->values[] = $value; |
188
|
|
|
} |
189
|
|
|
}else{ |
190
|
|
|
$this->values[] = $value; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Set the attribute name |
196
|
|
|
* @param $name |
197
|
|
|
* |
198
|
|
|
* @return Attribute |
199
|
|
|
*/ |
200
|
|
|
public function setName($name){ |
201
|
|
|
$this->name = $name; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the attribute name |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function getName(){ |
212
|
|
|
return $this->name; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get all values |
217
|
|
|
* |
218
|
|
|
* @return array |
219
|
|
|
*/ |
220
|
|
|
public function get(){ |
221
|
|
|
return $this->values; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Alias method for self::get() |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
|
|
public function all(){ |
230
|
|
|
return $this->get(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get the first value if possible |
235
|
|
|
* |
236
|
|
|
* @return mixed|null |
237
|
|
|
*/ |
238
|
|
|
public function first(){ |
239
|
|
|
if ($this->offsetExists(0)) { |
240
|
|
|
return $this->values[0]; |
241
|
|
|
} |
242
|
|
|
return null; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get the last value if possible |
247
|
|
|
* |
248
|
|
|
* @return mixed|null |
249
|
|
|
*/ |
250
|
|
|
public function last(){ |
251
|
|
|
if (($cnt = $this->count()) > 0) { |
252
|
|
|
return $this->values[$cnt - 1]; |
253
|
|
|
} |
254
|
|
|
return null; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get the number of values |
259
|
|
|
* |
260
|
|
|
* @return int |
261
|
|
|
*/ |
262
|
|
|
public function count(){ |
263
|
|
|
return count($this->values); |
264
|
|
|
} |
265
|
|
|
} |