Lines 100.00% 5 / 5
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
MapOperator 100.00% 5 / 5 100.00% 2 / 2 2 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 attach 100.00% 4 / 4 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Support\Stream;
6
7use Closure;
8use Stewart\Contracts\Subscription;
9
10/**
11 * @internal
12 * @template TIn
13 * @template TOut
14 * @implements StreamSource<TOut>
15 */
16final readonly class MapOperator implements StreamSource
17{
18    /**
19     * @param StreamSource<TIn> $source
20     * @param Closure(TIn): TOut $mapper
21     */
22    public function __construct(
23        private StreamSource $source,
24        private Closure $mapper,
25    ) {}
26
27    public function attach(SubscriptionScope $scope, Closure $downstream): Subscription
28    {
29        $mapper = $this->mapper;
30
31        return $this->source->attach($scope, static function (mixed $event) use ($mapper, $downstream): void {
32            $downstream($mapper($event));
33        });
34    }
35}