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
TakeWhileIterator
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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 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;
8use PeServer\Core\Throws\ArgumentException;
9use PeServer\Core\Throws\CallbackTypeError;
10
11/**
12 * takeWhile イテレータ。
13 *
14 * @template TKey of array-key
15 * @template TValue
16 * @implements Iterator<TKey,TValue>
17 */
18class TakeWhileIterator implements Iterator
19{
20    #region variable
21
22    private int $position = 0;
23
24    #endregion
25
26    /**
27     * 生成
28     *
29     * @param Iterator $iterator
30     * @param mixed $callback
31     * @phpstan-param callable(TValue,TKey):(bool) $callback
32     */
33    public function __construct(
34        private Iterator $iterator,
35        private mixed $callback
36    ) {
37        if (!is_callable($callback)) { //@phpstan-ignore-line phpstan-param callable
38            throw new CallbackTypeError('$callback');
39        }
40    }
41
42    #region Iterator
43
44    public function rewind(): void
45    {
46        $this->position = 0;
47        $this->iterator->rewind();
48    }
49
50    public function key(): mixed
51    {
52        return $this->iterator->key();
53    }
54
55    public function current(): mixed
56    {
57        return $this->iterator->current();
58    }
59
60    public function next(): void
61    {
62        $this->position += 1;
63        $this->iterator->next();
64    }
65
66    public function valid(): bool
67    {
68        if (!$this->iterator->valid()) {
69            return false;
70        }
71
72        return call_user_func($this->callback, $this->iterator->current(), $this->iterator->key());
73    }
74
75    #endregion
76}