Completed
Push — master ( 8feb15...b9cea8 )
by Florent
02:52
created

JWKSet::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 1
f 0
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;
13
14
/**
15
 * Class JWKSet.
16
 */
17
final class JWKSet implements JWKSetInterface
18
{
19
    /**
20
     * @var int
21
     */
22
    private $position = 0;
23
24
    /**
25
     * @var array
26
     */
27
    protected $keys = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getKey($index)
33
    {
34
        if (!isset($this->keys[$index])) {
35
            throw new \InvalidArgumentException('Undefined index.');
36
        }
37
        return $this->keys[$index];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getKeys()
44
    {
45
        return $this->keys;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function addKey(JWKInterface $key)
52
    {
53
        $keyset = clone $this;
54
        $keyset->keys[] = $key;
55
56
        return $keyset;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function removeKey($key)
63
    {
64
        if (isset($this->keys[$key])) {
65
            $keyset = clone $this;
66
            unset($keyset->keys[$key]);
67
68
            return $keyset;
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function jsonSerialize()
78
    {
79
        return ['keys' => array_values($this->getKeys())];
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function count($mode = COUNT_NORMAL)
86
    {
87
        return count($this->getKeys(), $mode);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function current()
94
    {
95
        return isset($this->keys[$this->position])?$this->keys[$this->position]:null;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function key()
102
    {
103
        return $this->position;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function next()
110
    {
111
        $this->position++;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function rewind()
118
    {
119
        $this->position = 0;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function valid()
126
    {
127
        return $this->current() instanceof JWKInterface;
128
    }
129
}
130