Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
TakeIterator
90.91% covered (success)
90.91%
10 / 11
83.33% covered (warning)
83.33%
5 / 6
8.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 rewind
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 current
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 next
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 valid
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Collection;
6
7use Iterator;
8
9/**
10 * take イテレータ。
11 *
12 * @template TKey of array-key
13 * @template TValue
14 * @implements Iterator<TKey,TValue>
15 */
16class TakeIterator implements Iterator
17{
18    #region variable
19
20    private int $position = 0;
21
22    #endregion
23
24    /**
25     * 生成
26     *
27     * @param Iterator $iterator
28     * @param int $count
29     * @phpstan-param non-negative-int $count
30     */
31    public function __construct(
32        private Iterator $iterator,
33        private readonly int $count
34    ) {
35    }
36
37    #region Iterator
38
39    public function rewind(): void
40    {
41        $this->position = 0;
42        $this->iterator->rewind();
43    }
44
45
46    public function key(): mixed
47    {
48        return $this->iterator->key();
49    }
50
51    public function current(): mixed
52    {
53        return $this->iterator->current();
54    }
55
56    public function next(): void
57    {
58        $this->position += 1;
59        if ($this->valid()) {
60            $this->iterator->next();
61        }
62    }
63
64    public function valid(): bool
65    {
66        if ($this->position < $this->count) {
67            return $this->iterator->valid();
68        }
69
70        return false;
71    }
72
73    #endregion
74}