Completed
Push — master ( efbd93...cbfd0d )
by Carlos
03:31
created

AttributeTrait::toJSON()   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 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/socialite.
5
 *
6
 * (c) overtrue <[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
/**
13
 * AttributeTrait.php.
14
 *
15
 * This file is part of the socialite.
16
 *
17
 * (c) overtrue <[email protected]>
18
 *
19
 * This source file is subject to the MIT license that is bundled
20
 * with this source code in the file LICENSE.
21
 */
22
23
namespace Overtrue\Socialite;
24
25
/**
26
 * Trait AttributeTrait.
27
 */
28
trait AttributeTrait
29
{
30
    /**
31
     * @var array
32
     */
33
    protected $attributes;
34
35
    /**
36
     * Return the attributes.
37
     *
38
     * @return array
39
     */
40
    public function getAttributes()
41
    {
42
        return $this->attributes;
43
    }
44
45
    /**
46
     * Return the extra attribute.
47
     *
48
     * @param string $name
49
     * @param string $default
50
     *
51
     * @return mixed
52
     */
53
    public function getAttribute($name, $default = null)
54
    {
55
        return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
56
    }
57
58
    /**
59
     * Set extra attributes.
60
     *
61
     * @param string $name
62
     * @param mixed  $value
63
     *
64
     * @return $this
65
     */
66
    public function setAttribute($name, $value)
67
    {
68
        $this->attributes[$name] = $value;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Map the given array onto the user's properties.
75
     *
76
     * @param array $attributes
77
     *
78
     * @return $this
79
     */
80
    public function merge(array $attributes)
81
    {
82
        $this->attributes = array_merge($this->attributes, $attributes);
83
84
        return $this;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function offsetExists($offset)
91
    {
92
        return array_key_exists($offset, $this->attributes);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function offsetGet($offset)
99
    {
100
        return $this->getAttribute($offset);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function offsetSet($offset, $value)
107
    {
108
        $this->setAttribute($offset, $value);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function offsetUnset($offset)
115
    {
116
        unset($this->attributes[$offset]);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function __get($property)
123
    {
124
        return $this->getAttribute($property);
125
    }
126
127
    /**
128
     * Return array.
129
     *
130
     * @return array
131
     */
132
    public function toArray()
133
    {
134
        return $this->getAttributes();
135
    }
136
137
    /**
138
     * Return JSON.
139
     *
140
     * @return string
141
     */
142
    public function toJSON()
143
    {
144
        return json_encode($this->getAttributes(), JSON_UNESCAPED_UNICODE);
145
    }
146
}
147