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 Illuminate\Support\Str; |
16
|
|
|
use Illuminate\Support\Facades\File; |
17
|
|
|
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException; |
18
|
|
|
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException; |
19
|
|
|
use Webklex\PHPIMAP\Support\Masks\AttachmentMask; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Attribute |
23
|
|
|
* |
24
|
|
|
* @package Webklex\PHPIMAP |
25
|
|
|
*/ |
26
|
|
|
class Attribute { |
27
|
|
|
|
28
|
|
|
/** @var string $name */ |
29
|
|
|
protected $name; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Value holder |
33
|
|
|
* |
34
|
|
|
* @var array $values |
35
|
|
|
*/ |
36
|
|
|
protected $values = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Attribute constructor. |
40
|
|
|
* @param string $name |
41
|
|
|
* @param array|mixed $value |
42
|
|
|
*/ |
43
|
|
|
public function __construct($name, $value = null) { |
44
|
|
|
$this->setName($name); |
45
|
|
|
$this->add($value); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the stringified attribute |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function __toString() { |
55
|
|
|
return implode(", ", $this->values); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return the stringified attribute |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function toString(){ |
64
|
|
|
return $this->__toString(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return the serialized attribute |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function __serialize(){ |
73
|
|
|
return $this->values; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Convert instance to array |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function toArray(){ |
82
|
|
|
return $this->__serialize(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add one or more values to the attribute |
87
|
|
|
* @param array|mixed $value |
88
|
|
|
* @param boolean $strict |
89
|
|
|
* |
90
|
|
|
* @return Attribute |
91
|
|
|
*/ |
92
|
|
|
public function add($value, $strict = false) { |
93
|
|
|
if (is_array($value)) { |
94
|
|
|
return $this->merge($value, $strict); |
95
|
|
|
}elseif ($value !== null) { |
96
|
|
|
$this->attach($value, $strict); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Merge a given array of values with the current values array |
104
|
|
|
* @param array $values |
105
|
|
|
* @param boolean $strict |
106
|
|
|
* |
107
|
|
|
* @return Attribute |
108
|
|
|
*/ |
109
|
|
|
public function merge($values, $strict = false) { |
110
|
|
|
if (is_array($values)) { |
|
|
|
|
111
|
|
|
foreach ($values as $value) { |
112
|
|
|
$this->attach($value, $strict); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Check if the attribute contains the given value |
121
|
|
|
* @param mixed $value |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function contains($value) { |
126
|
|
|
foreach ($this->values as $v) { |
127
|
|
|
if ($v === $value) { |
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Attach a given value to the current value array |
136
|
|
|
* @param $value |
137
|
|
|
* @param bool $strict |
138
|
|
|
*/ |
139
|
|
|
public function attach($value, $strict = false) { |
140
|
|
|
if ($strict === true) { |
141
|
|
|
if ($this->contains($value) === false) { |
142
|
|
|
$this->values[] = $value; |
143
|
|
|
} |
144
|
|
|
}else{ |
145
|
|
|
$this->values[] = $value; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set the attribute name |
151
|
|
|
* @param $name |
152
|
|
|
* |
153
|
|
|
* @return Attribute |
154
|
|
|
*/ |
155
|
|
|
public function setName($name){ |
156
|
|
|
$this->name = $name; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the attribute name |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function getName(){ |
167
|
|
|
return $this->name; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get all values |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function get(){ |
176
|
|
|
return $this->values; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Alias method for self::get() |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
public function all(){ |
185
|
|
|
return $this->get(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the first value if possible |
190
|
|
|
* |
191
|
|
|
* @return mixed|null |
192
|
|
|
*/ |
193
|
|
|
public function first(){ |
194
|
|
|
if (count($this->values) > 0) { |
195
|
|
|
return $this->values[0]; |
196
|
|
|
} |
197
|
|
|
return null; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the last value if possible |
202
|
|
|
* |
203
|
|
|
* @return mixed|null |
204
|
|
|
*/ |
205
|
|
|
public function last(){ |
206
|
|
|
$cnt = count($this->values); |
207
|
|
|
if ($cnt > 0) { |
208
|
|
|
return $this->values[$cnt - 1]; |
209
|
|
|
} |
210
|
|
|
return null; |
211
|
|
|
} |
212
|
|
|
} |