Completed
Push — master ( 2b6d49...c13cc5 )
by Florent
02:37
created

JWK::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
use Base64Url\Base64Url;
14
15
/**
16
 * Class JWK.
17
 */
18
final class JWK implements JWKInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $values = [];
24
25
    /**
26
     * JWK constructor.
27
     *
28
     * @param array $values
29
     */
30
    public function __construct(array $values = [])
31
    {
32
        if (!array_key_exists('kty', $values)) {
33
            throw new \InvalidArgumentException('The parameter "kty" is mandatory.');
34
        }
35
        $this->values = $values;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function jsonSerialize()
42
    {
43
        return $this->getAll();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function get($key)
50
    {
51
        if ($this->has($key)) {
52
            return $this->values[$key];
53
        }
54
        throw new \InvalidArgumentException(sprintf('The value identified by "%s" does not exist.', $key));
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function has($key)
61
    {
62
        return array_key_exists($key, $this->getAll());
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getKeys()
69
    {
70
        return array_keys($this->values);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getAll()
77
    {
78
        return $this->values;
79
    }
80
81
    public function thumbprint($hash_algorithm)
82
    {
83
        if (false === in_array($hash_algorithm, hash_algos())) {
84
            throw new \InvalidArgumentException(sprintf('Hash algorithm "%s" is not supported', $hash_algorithm));
85
        }
86
87
        $values = array_intersect_key($this->getAll(), array_flip(['kty', 'n', 'e', 'crv', 'x', 'y', 'k']));
88
        ksort($values);
89
        $input = json_encode($values);
90
91
        return Base64Url::encode(hash($hash_algorithm, $input, true));
92
    }
93
}
94