Download the PHP package anarchitecture/pipe without Composer
On this page you can find all versions of the php package anarchitecture/pipe. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anarchitecture/pipe
More information about anarchitecture/pipe
Files in anarchitecture/pipe
Package pipe
Short Description Unary callable helpers to leverage PHP 8.5 pipes
License MIT
Informations about the package pipe
anarchitecture/pipe
anarchitecture/pipe is a small PHP library that provides functions returning unary callables,
designed to be used with the PHP 8.5 pipe operator (|>).
The goal is to make it easy to leverage existing functions in pipe expressions without having to write inline closures.
Requirements
- PHP 8.5+ (pipe operator support)
Installation
Quick Start
What a helper returns
Each helper provides a unary callable:
Helpers (by category)
Arrays
p\array_all(callable $callback)p\array_any(callable $callback)p\array_chunk(int $length, bool $preserve_keys = false)p\array_dissoc(string|int ...$keys)— returns a copy of the array without the given key(s)p\array_filter(callable $callback)— filters an array, callback takes array values as single argumentp\array_flatten(array $arrays)— flattens one level (array of arrays to single array). String-key clashes: later arrays overwrite earlier; numeric keys are reindexedp\array_map(callable $mapper)— maps each value to a new value (like \array_map); preserves keys for single-array mappingp\array_map_recursive(callable $mapper)— recursively maps leaf values in nested arrays; preserves keys at every levelp\array_map_recursive_with_path(callable $mapper)— recursively maps leaf values in nested arrays; mapper is called as$mapper($value, $path)where$pathis the list of keys from root to leaf (preserves keys)p\array_nth(int $i)— nth element ornull(supports negative indexes:-1is last,-2is second-last, etc.)p\array_reduce(callable $reducer, mixed $initial = null)— reduces to a single value; reducer is called asreducer($carry, $value)(no key); empty input returns $initial (ornullif omitted)p\array_reduce_until(callable $reducer, callable $until, mixed $initial = null)— reduces left-to-right until$until($carry, $value, $key) === true; returns[$carry, $key, $value]or[$carry, null, null]if never triggeredp\array_slice(int $offset, ?int $length = null, bool $preserve_keys = false)p\array_transpose()— transpose a 2D array (matrix), preserving row/column keys (pads missing values withnull)p\array_sum(callable $callback)— map each element over $callback then sum numeric resultsp\array_unique(int $flags = SORT_STRING)p\sort(int $flags = SORT_REGULAR)p\rsort(int $flags = SORT_REGULAR)p\uasort(callable $comparator)— sorts values and preserves keys (like PHP\uasort)p\usort(callable $comparator)— sorts values and reindexes keys (like PHP\usort)
Strings / regex
p\explode(string $separator, int $limit = PHP_INT_MAX)p\implode(string $separator = "")p\str_replace(string|array $search, string|array $replace)p\str_starts_with(string $prefix)p\preg_match(string $pattern, int $flags = 0, int $offset = 0)— returns the$matchesarray (empty array when no match)p\preg_match_all(string $pattern, int $flags = 0, int $offset = 0)— returns the$matchesarray (empty array when no match)p\preg_replace(string|array $pattern, string|array $replacement, int $limit = -1)
Iterables (Generators-friendly)
p\collect(iterable $iterable)— terminal: collect any iterable into an array (preserves keys)p\iterable_all(?callable $callback = null)— returnstrueif all items match (or no item is!== truewhen callback isnull); short-circuitsp\iterable_any(?callable $callback = null)— returnstrueif any item matches (or is=== truewhen callback isnull); short-circuitsp\iterable_chunk(int $size, bool $preserve_keys = false)— lazily chunks an iterable into arrays of up to$sizeitems; final chunk may be smaller; when$preserve_keysistruepreserves input keys within each chunkp\iterable_filter(callable $callback)— yields matching items for which$callbackreturnstruep\iterable_first(iterable $iterable)— returns first item ornull(consumes one element)p\iterable_flatten($preserve_keys = true)— lazily flattens an iterable of iterables - preserving keys is optional, beware of key clashesp\iterable_map(callable $callback)— yields items mapped over$callback. Preserves keys.p\iterable_nth(int $n)— returns the nth item (0-based); consumes up to n+1 items; returnsnullif out of rangep\iterable_reduce(callable $callback, $initial = null)— reduces an iterable to a single valuep\iterable_reduce_until(callable $reducer, callable $until, mixed $initial = null)— reduces an iterable; returns[$carry, $key, $value]when triggered, or[$carry, null, null]if never triggered (short-circuits)p\iterable_scan(callable $callback, mixed $initial = null)— lazily yields the intermediate state after each iteration, like iterable_reduce() but streaming every step instead of only returning the final state.p\iterable_string(int $size = 1)— lazily iterate over a string as bytes ($size = 1) or byte-chunks ($size > 1).p\iterable_take(int $count)— yields first$countitemsp\iterable_ticker(int $start = 0)— infinite counter generatorp\iterable_window(int $size, bool $circular = false)– sliding windows over iterables (optionally circular)p\iterable_zip(iterable ...$right)— lazily zips the left iterable with one or more right iterables; yields tuples and stops at the shortest (preserves left keys)p\iterate(callable $callback, bool $include_seed = true)— infinite sequence by repeated application (yields seed first by default)
Control flow
p\if_else(callable $predicate, callable $then, callable $else)— applies$then($value)when$predicate($value) === true, otherwise$else($value)p\unless(callable $predicate, callable $callback)— applies$callbackonly when$predicate($value) !== true(otherwise returns the input unchanged)p\when(callable $predicate, callable $callback)— applies$callbackonly when$predicate($value) === true(otherwise returns the input unchanged)
Predicates / functional
p\equals(mixed $value)— returns true ifitem === $valuep\value(mixed $value)— constant function returns$valuep\not(callable $callable)— boolean invert the$callable's return value
Misc
p\apply(callable $callback)— applies an array of arguments to a callable (numeric keys => positional, string keys => named; mixed keys rejected)p\increment(int|float $by = 1)p\tap(callable $callback)— “tap” helper: calls$callback($value)for side effects and returns$valueunchangedp\zip_map(?callable $callback)— zip semantics over multiple arrays
Semantics (intentional differences)
A few helpers differ from their underlying built-ins to make pipelines pleasant:
p\apply($callback)rejects arrays with mixed numeric and string keys (to avoid PHP’s “positional after named” edge cases).p\array_dissoc()removes one or more keys and returns the modified array (missing keys are ignored).p\preg_match()andp\preg_match_all()return the$matchesarray (like the third arg of\preg_match()), not the match count; no match =>[].p\sort(),p\rsort(),p\usort(),p\uasort()return the sorted array (native functions returntrue/false).p\usort()reindexes keys (it returns a list). Usep\uasort()when you need to preserve key associations.p\zip_map($callback)([])returns[](avoids callingarray_map()with no arrays).- For debugging,
p\tap(\var_dump(...))is the direct replacement for the removedp\var_dump().Examples
Apply (spread arguments)
Conditional transform
If / else branching
Working with iterables (lazy pipelines)
Zip-map (multiple arrays)
Array dissoc (remove keys)
Tap (debug/log without breaking the pipeline)
Array transpose (matrix)
For numeric matrices it behaves like a regular transpose:
For string keyed matrices, it pads missing cells with null:
Iterable zip
Sliding windows (iterables)
Philosophy
- functions return unary callables
- explicitly designed for pipe expressions
- thin wrappers around existing PHP functions
- small, predictable, and composable
- no magic, just functions
Status
Experimental — based on PHP 8.5 pipes.
License
MIT