Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pipeline Laravel Package

sanmai/pipeline

sanmai/pipeline is a lightweight PHP pipeline library to process data through a chain of stages. Compose reusable, testable transformations with clear input/output flow, and plug in custom middleware-like steps for flexible processing in any app.

View on GitHub
Deep Wiki
Context7

Collection Methods

Collection methods are used to convert a pipeline into an array or to iterate over its elements.

Terminal Operations

A pipeline is lazy and does not begin processing data until a terminal operation is called. These methods consume the pipeline to produce a final result.

Understanding this is key to using the library effectively. No work is done, and no memory is used by the pipeline itself, until one of these methods is invoked.

Common terminal operations include:

  • toList() and toAssoc() - Convert to arrays
  • fold() and reduce() - Aggregate to a single value
  • count(), min(), max() - Calculate statistics
  • each() - Iterate and perform side effects
  • finalVariance() - Calculate comprehensive statistics

Array Conversion

toList()

Converts the pipeline to a numerically indexed array, discarding all keys.

Signature: toList(): array

Behavior:

  • This is a terminal operation.
  • It creates a new array with sequential numeric keys, starting from 0.

Examples:

// Convert a pipeline to a simple array
$result = take(['a' => 1, 'b' => 2, 'c' => 3])->toList();
// Result: [1, 2, 3]

toAssoc()

Converts the pipeline to an associative array, preserving the original keys.

Signature: toAssoc(): array

Behavior:

  • This is a terminal operation.
  • If there are duplicate keys, the last value will overwrite the previous ones.

Examples:

// Preserve key-value associations
$result = take(['a' => 1, 'b' => 2, 'c' => 3])
    ->map(fn($x) => $x * 2)
    ->toAssoc();
// Result: ['a' => 2, 'b' => 4, 'c' => 6]

Iteration

getIterator()

Returns an iterator for the pipeline, allowing you to use it in a foreach loop.

Signature: getIterator(): Traversable

Behavior:

  • This method is part of the IteratorAggregate interface.
  • It allows for manual control over the iteration process.

Examples:

// Using foreach
$pipeline = take(['a' => 1, 'b' => 2]);
foreach ($pipeline as $key => $value) {
    echo "$key: $value\n";
}

// Manual iteration
$iterator = take([1, 2, 3])->getIterator();
while ($iterator->valid()) {
    echo $iterator->current();
    $iterator->next();
}

each()

Eagerly iterates over all elements in the pipeline, applying a function to each.

Signature: each(callable $func): void

  • $func: The function to call for each element.

Callback Signature: function(mixed $value, mixed $key): void

Behavior:

  • This is a terminal operation.
  • It is primarily used for side effects, such as logging or database operations.

Examples:

// Print each value
take([1, 2, 3])->each(fn($x) => print("Value: $x\n"));

// Save to a database
take($users)->each(fn($user) => $user->save());
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests