GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

JWK::fromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK;
6
7
use Sop\JWX\JWK\Parameter\JWKParameter;
8
use Sop\JWX\JWK\Parameter\KeyIDParameter;
9
10
/**
11
 * Class to represent JWK structure.
12
 *
13
 * @see https://tools.ietf.org/html/rfc7517#section-4
14
 */
15
class JWK implements \Countable, \IteratorAggregate
16
{
17
    use TypedJWK;
18
19
    /**
20
     * Parameters.
21
     *
22
     * @var JWKParameter[]
23
     */
24
    protected $_parameters;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param JWKParameter ...$params
30
     */
31 153
    public function __construct(JWKParameter ...$params)
32
    {
33 153
        $this->_parameters = [];
34 153
        foreach ($params as $param) {
35 142
            $this->_parameters[$param->name()] = $param;
36
        }
37 153
    }
38
39
    /**
40
     * Initialize from an array representing a JSON object.
41
     */
42 35
    public static function fromArray(array $members): self
43
    {
44 35
        $params = [];
45 35
        foreach ($members as $name => $value) {
46 33
            $params[] = JWKParameter::fromNameAndValue($name, $value);
47
        }
48 35
        return new static(...$params);
49
    }
50
51
    /**
52
     * Initialize from a JSON string.
53
     *
54
     * @throws \UnexpectedValueException
55
     */
56 11
    public static function fromJSON(string $json): self
57
    {
58 11
        $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING);
59 11
        if (!is_array($members)) {
60 1
            throw new \UnexpectedValueException('Invalid JSON.');
61
        }
62 10
        return static::fromArray($members);
63
    }
64
65
    /**
66
     * Initialize from another JWK.
67
     *
68
     * Allows casting to subclass by late static binding.
69
     */
70 58
    public static function fromJWK(JWK $jwk): self
71
    {
72 58
        return new static(...array_values($jwk->_parameters));
73
    }
74
75
    /**
76
     * Get self with parameters added.
77
     *
78
     * @param JWKParameter ...$params
79
     */
80 10
    public function withParameters(JWKParameter ...$params): self
81
    {
82 10
        $obj = clone $this;
83 10
        foreach ($params as $param) {
84 10
            $obj->_parameters[$param->name()] = $param;
85
        }
86 10
        return $obj;
87
    }
88
89
    /**
90
     * Get all parameters.
91
     *
92
     * @return JWKParameter[]
93
     */
94 3
    public function parameters(): array
95
    {
96 3
        return array_values($this->_parameters);
97
    }
98
99
    /**
100
     * Get self with given key ID added to parameters.
101
     *
102
     * @param string $id Key ID as a string
103
     */
104 4
    public function withKeyID(string $id): self
105
    {
106 4
        return $this->withParameters(new KeyIDParameter($id));
107
    }
108
109
    /**
110
     * Whether parameters are present.
111
     *
112
     * Returns false if any of the given parameters is not set.
113
     *
114
     * @param string ...$names Parameter names
115
     */
116 233
    public function has(string ...$names): bool
117
    {
118 233
        foreach ($names as $name) {
119 233
            if (!isset($this->_parameters[$name])) {
120 233
                return false;
121
            }
122
        }
123 221
        return true;
124
    }
125
126
    /**
127
     * Get a parameter.
128
     *
129
     * @param string $name Parameter name
130
     *
131
     * @throws \LogicException
132
     */
133 196
    public function get(string $name): JWKParameter
134
    {
135 196
        if (!$this->has($name)) {
136 1
            throw new \LogicException("Parameter {$name} doesn't exists.");
137
        }
138 195
        return $this->_parameters[$name];
139
    }
140
141
    /**
142
     * Convert to array.
143
     *
144
     * @return array Parameter values keyed by parameter names
145
     */
146 5
    public function toArray(): array
147
    {
148 5
        $a = [];
149 5
        foreach ($this->_parameters as $param) {
150 2
            $a[$param->name()] = $param->value();
151
        }
152 5
        return $a;
153
    }
154
155
    /**
156
     * Convert to JSON.
157
     */
158 2
    public function toJSON(): string
159
    {
160 2
        $data = $this->toArray();
161 2
        if (empty($data)) {
162 1
            return '';
163
        }
164 1
        return json_encode((object) $data, JSON_UNESCAPED_SLASHES);
165
    }
166
167
    /**
168
     * Get the number of parameters.
169
     *
170
     * @see \Countable::count()
171
     */
172 1
    public function count(): int
173
    {
174 1
        return count($this->_parameters);
175
    }
176
177
    /**
178
     * Get iterator for the parameters.
179
     *
180
     * @see \IteratorAggregate::getIterator()
181
     */
182 1
    public function getIterator(): \ArrayIterator
183
    {
184 1
        return new \ArrayIterator($this->_parameters);
185
    }
186
}
187