tap()

Provides a way to observe the stream by executing a callback for each item.

class RunOpenCode\Component\Dataset\Operator\Tap
__construct(iterable<TKey, TValue> $source, callable(TValue, TKey=): void $callback)
Parameters:
  • $sourceiterable<TKey, TValue> Stream source to iterate over.

  • $callbackcallable(TValue, TKey=): void Callable to execute for each item.

getIterator()
Returns:

\Traversable<TKey, TValue> Items from the stream source.

Use cases

  • Monitor yielded items for debugging/logging purposes.

  • Execute processing logic for each yielded item.

Example

1<?php
2
3new Stream(['a' => 1, 'b' => 2, 'c' => 3])
4    ->tap(static fn(int $value, string $key): bool => print("Key: $key, Value: $value\n"));