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
|
|
|
namespace Overtrue\Socialite; |
13
|
|
|
|
14
|
|
|
use ArrayAccess; |
15
|
|
|
use InvalidArgumentException; |
|
|
|
|
16
|
|
|
use JsonSerializable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AccessToken. |
20
|
|
|
*/ |
21
|
|
|
class AccessToken implements AccessTokenInterface, ArrayAccess, JsonSerializable |
22
|
|
|
{ |
23
|
|
|
use HasAttributes; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AccessToken constructor. |
27
|
|
|
* |
28
|
|
|
* @param array $attributes |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $attributes) |
31
|
|
|
{ |
32
|
|
|
if (empty($attributes['access_token'])) { |
33
|
|
|
throw new InvalidArgumentException('The key "access_token" could not be empty.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->attributes = $attributes; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return the access token string. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getToken() |
45
|
|
|
{ |
46
|
|
|
return $this->getAttribute('access_token'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the refresh token string. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getRefreshToken() |
55
|
|
|
{ |
56
|
|
|
return $this->getAttribute('refresh_token'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set refresh token into this object. |
61
|
|
|
* |
62
|
|
|
* @param string $token |
63
|
|
|
*/ |
64
|
|
|
public function setRefreshToken($token) |
65
|
|
|
{ |
66
|
|
|
$this->setAttribute('refresh_token', $token); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function __toString() |
73
|
|
|
{ |
74
|
|
|
return strval($this->getAttribute('access_token', '')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function jsonSerialize() |
81
|
|
|
{ |
82
|
|
|
return $this->getToken(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: