Completed
Push — master ( 7c0e89...5c9864 )
by Wang
13:40
created

AccessToken::jsonSerialize()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 8
nop 0
1
<?php
2
3
4
namespace Oakhope\OAuth2\Client\Token\MiniProgram;
5
6
class AccessToken extends \League\OAuth2\Client\Token\AccessToken
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $sessionKey;
12
13
    /**
14
     * @var string
15
     */
16
    protected $openId;
17
18
    /**
19
     * @var string
20
     */
21
    protected $unionId;
22
23
    /**
24
     * @var array
25
     */
26
    protected $values = [];
27
28
    /**
29
     * Constructs an access token.
30
     *
31
     * @param array $options An array of options returned by the service provider
32
     *     in the access token request. The `access_token` option is required.
33
     * @throws \InvalidArgumentException if `access_token` is not provided in `$options`.
34
     */
35
    public function __construct(array $options = [])
36
    {
37
        $options['access_token'] = 'session_key';
38
39
        parent::__construct($options);
40
41
        if (empty($options['session_key'])) {
42
            throw new \InvalidArgumentException('Required option not passed: "session_key"');
43
        }
44
45
        $this->sessionKey = $options['session_key'];
46
47
        if (!empty($options['openid'])) {
48
            $this->openId = $options['openid'];
49
        }
50
51
        if (!empty($options['unionid'])) {
52
            $this->unionId = $options['unionid'];
53
        }
54
55
        // Capture any additional values that might exist in the token but are
56
        // not part of the standard response. Vendors will sometimes pass
57
        // additional user data this way.
58
        $this->values = array_diff_key($options, array_flip([
59
            'session_key',
60
            'openid',
61
            'unionid'
62
        ]));
63
    }
64
65
    /**
66
     * Returns the session key string of this instance.
67
     *
68
     * @return string
69
     */
70
    public function getSessionKey()
71
    {
72
        return $this->sessionKey;
73
    }
74
75
    /**
76
     * Returns the resource owner identifier, if defined.
77
     *
78
     * @return string|null
79
     */
80
    public function getOpenId()
81
    {
82
        return $this->openId;
83
    }
84
85
    /**
86
     * Returns the resource owner identifier, if defined.
87
     *
88
     * @return string|null
89
     */
90
    public function getUnionId()
91
    {
92
        return $this->unionId;
93
    }
94
95
    /**
96
     * Returns additional vendor values stored in the token.
97
     *
98
     * @return array
99
     */
100
    public function getValues()
101
    {
102
        return $this->values;
103
    }
104
105
    /**
106
     * Returns the token key.
107
     *
108
     * @return string
109
     */
110
    public function __toString()
111
    {
112
        return (string) $this->getSessionKey();
113
    }
114
115
    /**
116
     * Returns an array of parameters to serialize when this is serialized with
117
     * json_encode().
118
     *
119
     * @return array
120
     */
121
    public function jsonSerialize()
122
    {
123
        $parameters = $this->values;
124
125
        if ($this->sessionKey) {
126
            $parameters['sessionKey'] = $this->sessionKey;
127
        }
128
129
        if ($this->openId) {
130
            $parameters['openid'] = $this->openId;
131
        }
132
133
        if ($this->unionId) {
134
            $parameters['unionid'] = $this->unionId;
135
        }
136
137
        return $parameters;
138
    }
139
}
140