-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathToken.php
108 lines (84 loc) · 1.52 KB
/
Token.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Milo\Github\OAuth;
use Milo\Github;
/**
* OAuth token envelope.
*
* @author Miloslav Hůla (https://github.com/milo)
*/
class Token extends Github\Sanity
{
/** @var string */
private $value;
/** @var string */
private $type;
/** @var string[] */
private $scopes;
/**
* @param string
* @param string
* @param string[]
*/
public function __construct($value, $type = '', array $scopes = [])
{
$this->value = $value;
$this->type = $type;
$this->scopes = $scopes;
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @return string[]
*/
public function getScopes()
{
return $this->scopes;
}
/**
* @see https://developer.github.com/v3/oauth/#scopes
*
* @param string
* @return bool
*/
public function hasScope($scope)
{
if (in_array($scope, $this->scopes, TRUE)) {
return TRUE;
}
static $superiors = [
'user:email' => 'user',
'user:follow' => 'user',
'notifications' => 'repo',
];
if (array_key_exists($scope, $superiors) && in_array($superiors[$scope], $this->scopes, TRUE)) {
return TRUE;
}
return FALSE;
}
/** @internal */
public function toArray()
{
return [
'value' => $this->value,
'type' => $this->type,
'scopes' => $this->scopes,
];
}
/** @internal */
public static function createFromArray(array $data)
{
return new static($data['value'], $data['type'], $data['scopes']);
}
}