Completed
Push — master ( d0ace3...8281d7 )
by Florent
02:33
created

JWT::withoutProtectedHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Object;
13
14
/**
15
 * Class JWT.
16
 */
17
abstract class JWT implements JWTInterface
18
{
19
    /**
20
     * @var string|null
21
     */
22
    private $input = null;
23
24
    /**
25
     * @var array
26
     */
27
    private $protected_headers = [];
28
29
    /**
30
     * @var array
31
     */
32
    private $unprotected_headers = [];
33
34
    /**
35
     * @var mixed|null
36
     */
37
    private $payload = null;
38
39
    /**
40
     * JWT constructor.
41
     *
42
     * @param mixed|null $input
43
     * @param array      $protected_headers
44
     * @param array      $unprotected_headers
45
     * @param null       $payload
46
     */
47
    public function __construct($input, array $protected_headers = [], array $unprotected_headers = [], $payload = null)
48
    {
49
        $this->input = $input;
50
        $this->payload = $payload;
51
        $this->protected_headers = $protected_headers;
52
        $this->unprotected_headers = $unprotected_headers;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getInput()
59
    {
60
        return $this->input;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getProtectedHeaders()
67
    {
68
        return $this->protected_headers;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getUnprotectedHeaders()
75
    {
76
        return $this->unprotected_headers;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getPayload()
83
    {
84
        return $this->payload;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getProtectedHeader($key)
91
    {
92
        if ($this->hasProtectedHeader($key)) {
93
            return $this->protected_headers[$key];
94
        }
95
        throw new \InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key));
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function hasProtectedHeader($key)
102
    {
103
        return array_key_exists($key, $this->protected_headers);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getUnprotectedHeader($key)
110
    {
111
        if ($this->hasUnprotectedHeader($key)) {
112
            return $this->unprotected_headers[$key];
113
        }
114
        throw new \InvalidArgumentException(sprintf('The unprotected header "%s" does not exist', $key));
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function hasUnprotectedHeader($key)
121
    {
122
        return array_key_exists($key, $this->unprotected_headers);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getHeaders()
129
    {
130
        return array_merge($this->getProtectedHeaders(), $this->getUnprotectedHeaders());
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getHeader($key)
137
    {
138
        if ($this->hasProtectedHeader($key)) {
139
            return $this->getProtectedHeader($key);
140
        } elseif ($this->hasUnprotectedHeader($key)) {
141
            return $this->getUnprotectedHeader($key);
142
        }
143
        throw new \InvalidArgumentException(sprintf('The protected or unprotected headers do not contain header "%s"', $key));
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function hasHeader($key)
150
    {
151
        return $this->hasProtectedHeader($key) || $this->hasUnprotectedHeader($key);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function getHeaderOrClaim($key)
158
    {
159
        if ($this->hasHeader($key)) {
160
            return $this->getHeader($key);
161
        } elseif ($this->hasClaim($key)) {
162
            return $this->getClaim($key);
163
        }
164
        throw new \InvalidArgumentException(sprintf('The header or claim do not contain value with key "%s"', $key));
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function hasHeaderOrClaim($key)
171
    {
172
        return $this->hasHeader($key) || $this->hasClaim($key);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function getClaim($key)
179
    {
180
        if ($this->hasClaim($key)) {
181
            return $this->payload[$key];
182
        }
183
        throw new \InvalidArgumentException(sprintf('The payload does not contain claim "%s"', $key));
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getClaims()
190
    {
191
        if (is_array($this->payload)) {
192
            return $this->payload;
193
        }
194
        throw new \InvalidArgumentException('The payload does not contain claims');
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function hasClaim($key)
201
    {
202
        return $this->hasClaims() && array_key_exists($key, $this->payload);
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function hasClaims()
209
    {
210
        return is_array($this->payload);
211
    }
212
}
213