Completed
Push — master ( cb70dd...e19938 )
by Florent
02:50
created

JWT::getHeaderOrClaim()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
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 9
rs 9.6667
cc 3
eloc 6
nc 3
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
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 string $input
43
     */
44
    public function __construct($input = null)
45
    {
46
        $this->input = $input;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getInput()
53
    {
54
        return $this->input;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getProtectedHeaders()
61
    {
62
        return $this->protected_headers;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getUnprotectedHeaders()
69
    {
70
        return $this->unprotected_headers;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getPayload()
77
    {
78
        return $this->payload;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function withProtectedHeaders(array $values)
85
    {
86
        $jwt = clone $this;
87
        $jwt->protected_headers = $values;
88
89
        return $jwt;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function withUnprotectedHeaders(array $values)
96
    {
97
        $jwt = clone $this;
98
        $jwt->unprotected_headers = $values;
99
100
        return $jwt;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function withProtectedHeader($key, $value)
107
    {
108
        $jwt = clone $this;
109
        $jwt->protected_headers[$key] = $value;
110
111
        return $jwt;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function withUnprotectedHeader($key, $value)
118
    {
119
        $jwt = clone $this;
120
        $jwt->unprotected_headers[$key] = $value;
121
122
        return $jwt;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function withPayload($payload)
129
    {
130
        $jwt = clone $this;
131
        $jwt->payload = $payload;
132
133
        return $jwt;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function getProtectedHeader($key)
140
    {
141
        if ($this->hasProtectedHeader($key)) {
142
            return $this->protected_headers[$key];
143
        }
144
        throw new \InvalidArgumentException(sprintf('The protected header "%" does not exist', $key));
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function hasProtectedHeader($key)
151
    {
152
        return array_key_exists($key, $this->protected_headers);
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function withoutProtectedHeader($key)
159
    {
160
        if (!$this->hasProtectedHeader($key)) {
161
            return $this;
162
        }
163
        $jwt = clone $this;
164
        unset($jwt->protected_headers[$key]);
165
166
        return $jwt;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getUnprotectedHeader($key)
173
    {
174
        if ($this->hasUnprotectedHeader($key)) {
175
            return $this->unprotected_headers[$key];
176
        }
177
        throw new \InvalidArgumentException(sprintf('The unprotected header "%" does not exist', $key));
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function hasUnprotectedHeader($key)
184
    {
185
        return array_key_exists($key, $this->unprotected_headers);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function withoutUnprotectedHeader($key)
192
    {
193
        if (!$this->hasUnprotectedHeader($key)) {
194
            return $this;
195
        }
196
        $jwt = clone $this;
197
        unset($jwt->unprotected_headers[$key]);
198
199
        return $jwt;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function getHeaders()
206
    {
207
        return array_merge($this->protected_headers, $this->unprotected_headers);
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function getHeader($key)
214
    {
215
        if ($this->hasProtectedHeader($key)) {
216
            return $this->getProtectedHeader($key);
217
        } elseif ($this->hasUnprotectedHeader($key)) {
218
            return $this->getUnprotectedHeader($key);
219
        }
220
        throw new \InvalidArgumentException(sprintf('The protected or unprotected headers do not contain header "%"', $key));
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function hasHeader($key)
227
    {
228
        return $this->hasProtectedHeader($key) || $this->hasUnprotectedHeader($key);
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234
    public function getHeaderOrClaim($key)
235
    {
236
        if ($this->hasHeader($key)) {
237
            return $this->getHeader($key);
238
        } elseif ($this->hasClaim($key)) {
239
            return $this->getClaim($key);
240
        }
241
        throw new \InvalidArgumentException(sprintf('The header or claim do not contain value with key "%"', $key));
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247
    public function hasHeaderOrClaim($key)
248
    {
249
        return $this->hasHeader($key) || $this->hasClaim($key);
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function getClaim($key)
256
    {
257
        if ($this->hasClaim($key)) {
258
            return $this->payload[$key];
259
        }
260
        throw new \InvalidArgumentException(sprintf('The payload does not contain claim "%"', $key));
261
    }
262
263
    /**
264
     * {@inheritdoc}
265
     */
266
    public function getClaims()
267
    {
268
        if (is_array($this->payload)) {
269
            return $this->payload;
270
        }
271
        throw new \InvalidArgumentException('The payload does not contain claims');
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function hasClaim($key)
278
    {
279
        return $this->hasClaims() && array_key_exists($key, $this->payload);
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285
    public function hasClaims()
286
    {
287
        return is_array($this->payload);
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293
    public function withClaim($key, $value)
294
    {
295
        $jwt = clone $this;
296
        if (!is_array($jwt->payload)) {
297
            $jwt->payload = [];
298
        }
299
        $jwt->payload[$key] = $value;
300
301
        return $jwt;
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307
    public function withoutClaim($key)
308
    {
309
        if (!$this->hasClaim($key)) {
310
            return $this;
311
        }
312
        $jwt = clone $this;
313
        unset($jwt->payload[$key]);
314
315
        return $jwt;
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321
    public function withClaims(array $claims)
322
    {
323
        $jwt = clone $this;
324
        $jwt->payload = $claims;
325
326
        return $jwt;
327
    }
328
329
    /*public function __clone()
330
    {
331
        $this->input = null;
332
    }*/
333
}
334