Completed
Push — master ( cc8b1f...bbfee2 )
by Malte
02:26
created

Attribute::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 8
rs 10
cc 3
nc 3
nop 2
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 serialized attribute
60
     *
61
     * @return array
62
     */
63
    public function __serialize(){
64
        return $this->values;
65
    }
66
67
    /**
68
     * Add one or more values to the attribute
69
     * @param array|mixed $value
70
     * @param boolean $strict
71
     *
72
     * @return Attribute
73
     */
74
    public function add($value, $strict = false) {
75
        if (is_array($value)) {
76
            return $this->merge($value, $strict);
77
        }elseif ($value !== null) {
78
            $this->attach($value, $strict);
79
        }
80
81
        return $this;
82
    }
83
84
    /**
85
     * Merge a given array of values with the current values array
86
     * @param array $values
87
     * @param boolean $strict
88
     *
89
     * @return Attribute
90
     */
91
    public function merge($values, $strict = false) {
92
        if (is_array($values)) {
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
93
            foreach ($values as $value) {
94
                $this->attach($value, $strict);
95
            }
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * Check if the attribute contains the given value
103
     * @param mixed $value
104
     *
105
     * @return bool
106
     */
107
    public function contains($value) {
108
        foreach ($this->values as $v) {
109
            if ($v === $value) {
110
                return true;
111
            }
112
        }
113
        return false;
114
    }
115
116
    /**
117
     * Attach a given value to the current value array
118
     * @param $value
119
     * @param bool $strict
120
     */
121
    public function attach($value, $strict = false) {
122
        if ($strict === true) {
123
            if ($this->contains($value) === false) {
124
                $this->values[] = $value;
125
            }
126
        }else{
127
            $this->values[] = $value;
128
        }
129
    }
130
131
    /**
132
     * Set the attribute name
133
     * @param $name
134
     *
135
     * @return Attribute
136
     */
137
    public function setName($name){
138
        $this->name = $name;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Get the attribute name
145
     *
146
     * @return string
147
     */
148
    public function getName(){
149
        return $this->name;
150
    }
151
152
    /**
153
     * Get all values
154
     *
155
     * @return array
156
     */
157
    public function get(){
158
        return $this->values;
159
    }
160
161
    /**
162
     * Alias method for self::get()
163
     *
164
     * @return array
165
     */
166
    public function all(){
167
        return $this->get();
168
    }
169
170
    /**
171
     * Get the first value if possible
172
     *
173
     * @return mixed|null
174
     */
175
    public function first(){
176
        if (count($this->values) > 0) {
177
            return $this->values[0];
178
        }
179
        return null;
180
    }
181
182
    /**
183
     * Get the last value if possible
184
     *
185
     * @return mixed|null
186
     */
187
    public function last(){
188
        $cnt = count($this->values);
189
        if ($cnt > 0) {
190
            return $this->values[$cnt - 1];
191
        }
192
        return null;
193
    }
194
}