overflow()

Monitors the number of items yielded by the stream and raises an exception when the allowed limit is exceeded.

class RunOpenCode\Component\Dataset\Operator\Overflow
__construct(iterable<TKey, TValue> $source, positive-int $capacity, \Throwable|(callable(StreamOverflowException=): \Throwable)|null $throw = null)
Parameters:
  • $sourceiterable<TKey, TValue> Stream source to iterate over.

  • $capacitypositive-int Maximum number of items to iterate over.

  • $throw\Throwable|(callable(StreamOverflowException=): \Throwable)|null Exception to throw if stream yielded more items then capacity allows. If null is provided, StreamOverflowException is thrown. Otherwise, provided exception will be thrown, or callable invoked to create exception to throw.

getIterator()
Returns:

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

Use cases

  • When number of yielded items is expected not to exceed given capacity.

Example

Execute query which should yield number of items not more than expected count (per example, business logic allows for user to have up to 10 bank accounts).

1<?php
2
3$accounts = $database->execute('SELECT * FROM accounts WHERE accounts.user_id = :user');
4
5new Stream($accounts)
6    ->overflow(10)
7    ->collect(ArrayCollector::class);