Completed
Branch develop (8ddc4a)
by Florent
02:46
created

JWKSet::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

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