Completed
Push — master ( 1a59c2...c73113 )
by Florent
02:55
created

JWKSet::countKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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