Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SelectIterator
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 current
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Collection;
6
7use Iterator;
8use IteratorIterator;
9use PeServer\Core\Throws\ArgumentException;
10use PeServer\Core\Throws\CallbackTypeError;
11
12/**
13 * select イテレータ。
14 *
15 * @template TKey of array-key
16 * @template TValue
17 * @template TResult
18 * @extends IteratorIterator<TKey,TResult>
19 */
20class SelectIterator extends IteratorIterator //@phpstan-ignore-line Generic
21{
22    /**
23     * 生成。
24     *
25     * @param Iterator $iterator
26     * @phpstan-param Iterator<TKey,TValue> $iterator
27     * @param mixed $callback
28     * @phpstan-param callable(TValue,TKey):(TResult) $callback
29     */
30    public function __construct(
31        Iterator $iterator,
32        private mixed $callback
33    ) {
34        if (!is_callable($callback)) { //@phpstan-ignore-line phpstan-param callable
35            throw new CallbackTypeError('$callback');
36        }
37        parent::__construct($iterator); //@phpstan-ignore-line
38    }
39
40    #region IteratorIterator
41
42    /**
43     * @phpstan-return TResult
44     */
45    public function current(): mixed
46    {
47        return call_user_func($this->callback, parent::current(), parent::key());
48    }
49
50    #endregion
51}