Completed
Push — master ( 44981b...553dae )
by wen
03:07
created

HasAttributesTrait::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 $this
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 $this;
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
     * Get all of the current attributes on the class.
44
     *
45
     * @return array
46
     */
47
    public function getAttributes()
48
    {
49
        return $this->attributes;
50
    }
51
52
    /**
53
     * Determine if the given key exists in the attributes.
54
     *
55
     * @param  string|int $key
56
     *
57
     * @return bool
58
     */
59
    public function existsAttribute($key)
60
    {
61
        return Arr::exists($this->attributes, $key);
62
    }
63
64
    /**
65
     * Check if an key or keys exist in the attributes using "dot" notation.
66
     *
67
     * @param  string|array $keys
68
     *
69
     * @return bool
70
     */
71
    public function hasAttribute($keys)
72
    {
73
        return Arr::has($this->attributes, $keys);
74
    }
75
76
    /**
77
     * Convert the Object instance to an array.
78
     *
79
     * @return array
80
     */
81
    public function toArray()
82
    {
83
        return $this->getAttributes();
84
    }
85
86
    /**
87
     * Convert the object into something JSON serializable.
88
     *
89
     * @return array
90
     */
91
    public function jsonSerialize()
92
    {
93
        return $this->toArray();
94
    }
95
96
    /**
97
     * Convert the Object instance to JSON.
98
     *
99
     * @param  int  $options
100
     * @return string
101
     */
102
    public function toJson($options = 0)
103
    {
104
        return json_encode($this->jsonSerialize(), $options);
105
    }
106
}
107