Completed
Push — master ( 71757c...fc1061 )
by wen
06:47 queued 05:06
created

HasAttributesTrait::setAttribute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 2
1
<?php
2
3
namespace Sco\Attributes;
4
5
use Illuminate\Support\Arr;
6
7
trait HasAttributesTrait
8
{
9
    protected $attributes = [];
10
11
    /**
12
     * Set a given log value.
13
     *
14
     * @param array|string $key
15
     * @param mixed        $value
16
     *
17
     * @return bool
18
     */
19
    public function setAttribute($key, $value = null)
20
    {
21
        $keys = is_array($key) ? $key : [$key => $value];
22
23
        foreach ($keys as $key => $value) {
24
            Arr::set($this->attributes, $key, $value);
25
        }
26
        return true;
27
    }
28
29
    /**
30
     * Get the specified log value.
31
     *
32
     * @param null|string $key
33
     * @param mixed       $default
34
     *
35
     * @return mixed
36
     */
37
    public function getAttribute($key = null, $default = null)
38
    {
39
        return Arr::get($this->attributes, $key, $default);
40
    }
41
42
    /**
43
     * @param  string|int $key
44
     *
45
     * @return bool
46
     */
47
    public function existsAttribute($key)
48
    {
49
        return Arr::exists($this->attributes, $key);
50
    }
51
52
    /**
53
     * @param  string|array $keys
54
     *
55
     * @return bool
56
     */
57
    public function hasAttribute($keys)
58
    {
59
        return Arr::has($this->attributes, $keys);
60
    }
61
}
62