Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Token;
3
 
4
use Aws\Token\TokenInterface;
5
 
6
/**
7
 * Basic implementation of the AWS Token interface that allows callers to
8
 * pass in an AWS token in the constructor.
9
 */
10
class Token implements TokenInterface, \Serializable
11
{
12
    protected $token;
13
    protected $expires;
14
 
15
    /**
16
     * Constructs a new basic token object, with the specified AWS
17
     * token
18
     *
19
     * @param string $token   Security token to use
20
     * @param int    $expires UNIX timestamp for when the token expires
21
     */
22
    public function __construct($token, $expires = null)
23
    {
24
        $this->token = $token;
25
        $this->expires = $expires;
26
    }
27
 
28
    /**
29
     * Sets the state of a token object
30
     *
31
     * @param array $state   array containing 'token' and 'expires'
32
     */
33
    public static function __set_state(array $state)
34
    {
35
        return new self(
36
            $state['token'],
37
            $state['expires']
38
        );
39
    }
40
 
41
    /**
42
     * @return string
43
     */
44
    public function getToken()
45
    {
46
        return $this->token;
47
    }
48
 
49
    /**
50
     * @return int
51
     */
52
    public function getExpiration()
53
    {
54
        return $this->expires;
55
    }
56
 
57
    /**
58
     * @return bool
59
     */
60
    public function isExpired()
61
    {
62
        return $this->expires !== null && time() >= $this->expires;
63
    }
64
 
65
    /**
66
     * @return array
67
     */
68
    public function toArray()
69
    {
70
        return [
71
            'token'   => $this->token,
72
            'expires' => $this->expires
73
        ];
74
    }
75
 
76
    /**
77
     * @return string
78
     */
79
    public function serialize()
80
    {
81
        return json_encode($this->__serialize());
82
    }
83
 
84
    /**
85
     * Sets the state of the object from serialized json data
86
     */
87
    public function unserialize($serialized)
88
    {
89
        $data = json_decode($serialized, true);
90
 
91
        $this->__unserialize($data);
92
    }
93
 
94
    /**
95
     * @return array
96
     */
97
    public function __serialize()
98
    {
99
        return $this->toArray();
100
    }
101
 
102
    /**
103
     *  Sets the state of this object from an array
104
     */
105
    public function __unserialize($data)
106
    {
107
        $this->token = $data['token'];
108
        $this->expires = $data['expires'];
109
    }
110
}