summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/Token/Token.php
blob: 6d2c566982ca6ad668b3e08808f076dd530d76ac (plain)
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
109
110
<?php
namespace Aws\Token;

use Aws\Token\TokenInterface;

/**
 * Basic implementation of the AWS Token interface that allows callers to
 * pass in an AWS token in the constructor.
 */
class Token implements TokenInterface, \Serializable
{
    protected $token;
    protected $expires;

    /**
     * Constructs a new basic token object, with the specified AWS
     * token
     *
     * @param string $token   Security token to use
     * @param int    $expires UNIX timestamp for when the token expires
     */
    public function __construct($token, $expires = null)
    {
        $this->token = $token;
        $this->expires = $expires;
    }

    /**
     * Sets the state of a token object
     *
     * @param array $state   array containing 'token' and 'expires'
     */
    public static function __set_state(array $state)
    {
        return new self(
            $state['token'],
            $state['expires']
        );
    }

    /**
     * @return string
     */
    public function getToken()
    {
        return $this->token;
    }

    /**
     * @return int
     */
    public function getExpiration()
    {
        return $this->expires;
    }

    /**
     * @return bool
     */
    public function isExpired()
    {
        return $this->expires !== null && time() >= $this->expires;
    }

    /**
     * @return array
     */
    public function toArray()
    {
        return [
            'token'   => $this->token,
            'expires' => $this->expires
        ];
    }

    /**
     * @return string
     */
    public function serialize()
    {
        return json_encode($this->__serialize());
    }

    /**
     * Sets the state of the object from serialized json data
     */
    public function unserialize($serialized)
    {
        $data = json_decode($serialized, true);

        $this->__unserialize($data);
    }

    /**
     * @return array
     */
    public function __serialize()
    {
        return $this->toArray();
    }

    /**
     *  Sets the state of this object from an array
     */
    public function __unserialize($data)
    {
        $this->token = $data['token'];
        $this->expires = $data['expires'];
    }
}