HasAttributes::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the docodeit/wechat.
5
 *
6
 * (c) docodeit <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace JinWeChat\Kernel\Traits;
13
14
use Illuminate\Support\Arr;
15
use Illuminate\Support\Str;
16
use JinWeChat\Kernel\Exceptions\InvalidArgumentException;
17
18
/**
19
 * Trait Attributes.
20
 */
21
trait HasAttributes
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $attributes = [];
27
28
    /**
29
     * @var bool
30
     */
31
    protected $snakeable = true;
32
33
    /**
34
     * Set Attributes.
35
     *
36
     * @param array $attributes
37
     *
38
     * @return $this
39
     */
40
    public function setAttributes(array $attributes = [])
41
    {
42
        $this->attributes = $attributes;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Set attribute.
49
     *
50
     * @param string $attribute
51
     * @param string $value
52
     *
53
     * @return $this
54
     */
55
    public function setAttribute($attribute, $value)
56
    {
57
        Arr::set($this->attributes, $attribute, $value);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Get attribute.
64
     *
65
     * @param string $attribute
66
     * @param mixed  $default
67
     *
68
     * @return mixed
69
     */
70
    public function getAttribute($attribute, $default = null)
71
    {
72
        return Arr::get($this->attributes, $attribute, $default);
73
    }
74
75
    /**
76
     * @param string $attribute
77
     *
78
     * @return bool
79
     */
80
    public function isRequired($attribute)
81
    {
82
        return in_array($attribute, $this->getRequired(), true);
83
    }
84
85
    /**
86
     * @return array|mixed
87
     */
88
    public function getRequired()
89
    {
90
        return property_exists($this, 'required') ? $this->required : [];
0 ignored issues
show
Bug Best Practice introduced by
The property required does not exist on JinWeChat\Kernel\Traits\HasAttributes. Since you implemented __get, consider adding a @property annotation.
Loading history...
91
    }
92
93
    /**
94
     * Set attribute.
95
     *
96
     * @param string $attribute
97
     * @param mixed  $value
98
     *
99
     * @throws \JinWeChat\Kernel\Exceptions\InvalidArgumentException
100
     *
101
     * @return $this
102
     */
103
    public function with($attribute, $value)
104
    {
105
        $this->snakeable && $attribute = Str::snake($attribute);
106
107
        $this->setAttribute($attribute, $value);
108
109
        return $this;
110
    }
111
112
    /**
113
     * Override parent set() method.
114
     *
115
     * @param string $attribute
116
     * @param mixed  $value
117
     *
118
     * @return $this
119
     */
120
    public function set($attribute, $value)
121
    {
122
        $this->setAttribute($attribute, $value);
123
124
        return $this;
125
    }
126
127
    /**
128
     * Override parent get() method.
129
     *
130
     * @param string $attribute
131
     * @param mixed  $default
132
     *
133
     * @return mixed
134
     */
135
    public function get($attribute, $default = null)
136
    {
137
        return $this->getAttribute($attribute, $default);
138
    }
139
140
    /**
141
     * @param string $key
142
     *
143
     * @return bool
144
     */
145
    public function has(string $key)
146
    {
147
        return Arr::has($this->attributes, $key);
148
    }
149
150
    /**
151
     * @param array $attributes
152
     *
153
     * @return $this
154
     */
155
    public function merge(array $attributes)
156
    {
157
        $this->attributes = array_merge($this->attributes, $attributes);
158
159
        return $this;
160
    }
161
162
    /**
163
     * @param array|string $keys
164
     *
165
     * @return array
166
     */
167
    public function only($keys)
168
    {
169
        return Arr::only($this->attributes, $keys);
170
    }
171
172
    /**
173
     * Return all items.
174
     *
175
     * @return array
176
     */
177
    public function all()
178
    {
179
        $this->checkRequiredAttributes();
180
181
        return $this->attributes;
182
    }
183
184
    /**
185
     * Magic call.
186
     *
187
     * @param string $method
188
     * @param array  $args
189
     *
190
     * @return $this
191
     */
192
    public function __call($method, $args)
193
    {
194
        if (0 === stripos($method, 'with')) {
195
            return $this->with(substr($method, 4), array_shift($args));
196
        }
197
198
        throw new \BadMethodCallException(sprintf('Method "%s" does not exists.', $method));
199
    }
200
201
    /**
202
     * Magic get.
203
     *
204
     * @param string $property
205
     *
206
     * @return mixed
207
     */
208
    public function __get($property)
209
    {
210
        return $this->get($property);
211
    }
212
213
    /**
214
     * Magic set.
215
     *
216
     * @param string $property
217
     * @param mixed  $value
218
     *
219
     * @return $this
220
     */
221
    public function __set($property, $value)
222
    {
223
        return $this->with($property, $value);
224
    }
225
226
    /**
227
     * Whether or not an data exists by key.
228
     *
229
     * @param string $key
230
     *
231
     * @return bool
232
     */
233
    public function __isset($key)
234
    {
235
        return isset($this->attributes[$key]);
236
    }
237
238
    /**
239
     * Check required attributes.
240
     *
241
     * @throws \JinWeChat\Kernel\Exceptions\InvalidArgumentException
242
     */
243
    protected function checkRequiredAttributes()
244
    {
245
        foreach ($this->getRequired() as $attribute) {
246
            if (is_null($this->get($attribute))) {
247
                throw new InvalidArgumentException(sprintf('"%s" cannot be empty.', $attribute));
248
            }
249
        }
250
    }
251
}
252