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

HasAttributesTrait::hasAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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