You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41-13Lines changed: 41 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,34 @@ The same workflow using temporary arrays and for-loops would be longer, harder t
35
35
memory multiple times. A Flow pipeline keeps the computation streaming, lets you re-use SPL iterators seamlessly and
36
36
makes experimentation as simple as inserting or removing a step.
37
37
38
+
## Flow is **not** the usual Collection-style utility library
39
+
40
+
Typical Collection-like classes use arrays underneath. But `Flow`, even though it also uses a chainable fluent interface, is both an `Iterator` and a `Traversable` (therefore with native PHP support), and works perfectly with **Generators** and **SPL iterators**, with no arrays underneath. **Each iteration step only requires one value to be in memory (it can be generated on the fly), and the data is streamed through the pipeline**.
41
+
42
+
Since Flow implements both `Iterator` and `Traversable`, you can use a Flow object anywhere you would use a `Traversable` or `Iterator`. For example, you can use `foreach` directly over a Flow object:
43
+
44
+
```php
45
+
foreach (Flow::from([1, 2, 3]) as $value) {
46
+
echo $value;
47
+
}
48
+
```
49
+
50
+
You can also pass Flow objects to any function that expects an `Iterator` or `Traversable`:
@@ -71,12 +99,12 @@ makes experimentation as simple as inserting or removing a step.
71
99
| `mapAndFilter(callable $fn, $arg = null)` | Combines mapping and filtering: return a value to keep it, or `null` to drop it.
72
100
| `flip()` | Swaps keys with their corresponding values while iterating.
73
101
| `keys()` | Replaces each value by its key and reindexes the keys sequentially.
74
-
| `reindex(int $start = 0, int $step = 1)` | Rewrites keys as a numeric sequence without materialising the data.
102
+
| `reindex(int $start = 0, int $step = 1)` | Rewrites keys as a numeric sequence without materializing the data.
75
103
| `regex(string $pattern, int $flags = 0, bool $useKeys = false)` | Replaces each item with the full set of regular expression matches.
76
104
| `regexExtract(string $pattern, int $flags = 0, bool $useKeys = false)` | Extracts the first regex match for each item.
77
-
| `regexMap(string $pattern, string $replacement, bool $useKeys = false)` | Performs regex replacements on the fly using `RegexIterator::setReplacement()`.
105
+
| `regexMap(string $pattern, string $replaceWith, bool $useKeys = false)` | Performs regex replacements on the fly using `RegexIterator::setReplacement()`.
78
106
| `regexSplit(string $pattern, int $flags = 0, bool $useKeys = false)` | Splits strings by regex and yields the resulting fragments.
79
-
| `swap(callable $fn)` | Materialises the stream, lets a callback replace the dataset, then restarts iteration.
107
+
| `swap(callable $fn)` | Materializes the stream, lets a callback replace the dataset, then restarts iteration.
80
108
| `reduce(callable $fn, $seedValue = null)` | Collapses the flow into a single value by folding with an accumulator.
81
109
82
110
### Filtering, gating and flow control
@@ -91,18 +119,18 @@ makes experimentation as simple as inserting or removing a step.
91
119
| `repeatWhile(callable $fn)` | Replays the flow until the callback tells it to stop.
92
120
| `only(int $n)` | Limits the flow to the first *n* items, regardless of key types.
93
121
| `skip(int $n = 1)` | Skips the first *n* items and continues streaming.
94
-
| `drop(int $n = 1)` | Removes the last *n* items (materialises and trims the array).
122
+
| `drop(int $n = 1)` | Removes the last *n* items (materializes and trims the array).
95
123
| `slice(int $offset = 0, int $count = -1)` | Delegates to `LimitIterator` to take a window of items.
96
124
| `noRewind()` | Wraps the iterator with `NoRewindIterator` so it cannot be rewound after the first traversal.
97
125
98
-
### Ordering, caching and materialisation
126
+
### Ordering, caching and materialization
99
127
100
128
| Method | Description |
101
129
| --- | --- |
102
-
| `sort(string $type = 'sort', int $flags = SORT_REGULAR, ?callable $fn = null)` | Materialises and delegates to the native PHP sort family, preserving keys where appropriate.
103
-
| `reverse(bool $preserveKeys = false)` | Materialises, reverses and exposes the sequence through a generator.
104
-
| `cache()` | Memoises the iterator so future traversals re-use cached values.
105
-
| `pack()` | Materialises and converts the flow into a zero-based array while retaining order.
130
+
| `sort(string $type = 'sort', int $flags = SORT_REGULAR, ?callable $fn = null)` | Materializes and delegates to the native PHP sort family, preserving keys where appropriate.
131
+
| `reverse(bool $preserveKeys = false)` | Materializes, reverses and exposes the sequence through a generator.
132
+
| `cache()` | Memoizes the iterator so future traversals re-use cached values.
133
+
| `pack()` | Materializes and converts the flow into a zero-based array while retaining order.
106
134
| `all()` | Collects the entire dataset into an array, preserving keys.
107
135
108
136
### Inspecting and manual iteration helpers
@@ -112,7 +140,7 @@ makes experimentation as simple as inserting or removing a step.
112
140
| `fetch()` | Reads and advances a single value from the flow (auto-rewinds on first call).
113
141
| `fetchKey()` | Reads and advances a single key from the flow.
114
142
| `current(): mixed`, `key(): mixed`, `next(): void`, `rewind(): void`, `valid(): bool` | Implement the native `Iterator` interface so you can loop over `Flow` directly.
115
-
| `getIterator(): Iterator` | Returns the current iterator (materialising data if needed).
143
+
| `getIterator(): Iterator` | Returns the current iterator (materializing data if needed).
116
144
| `setIterator($iterable)` | Replaces the underlying iterator; intended for advanced scenarios.
117
145
118
146
### Filesystem flows
@@ -123,7 +151,7 @@ makes experimentation as simple as inserting or removing a step.
123
151
| --- | --- |
124
152
| `FilesystemFlow::from(string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS)` | Streams directory entries with full control over SPL flags.
125
153
| `FilesystemFlow::glob(string $pattern, int $flags = 0)` | Iterates filesystem matches similar to `glob()` but lazily.
126
-
| `FilesystemFlow::recursiveFrom(string $path, int $flags = default, int $mode = FilesystemFlow::DIRECTORIES_FIRST)` | Builds a recursive traversal using `RecursiveIteratorIterator` and a configurable recursion mode.
154
+
| `FilesystemFlow::recursiveFrom(string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME \| FilesystemIterator::CURRENT_AS_FILEINFO \| FilesystemIterator::SKIP_DOTS, int $mode = RecursiveIteratorIterator::SELF_FIRST)` | Builds a recursive traversal using `RecursiveIteratorIterator` and a configurable recursion mode.
127
155
| `FilesystemFlow::recursiveGlob(string $rootDir, string $pattern, int $flags = 0)` | Performs recursive glob searches, yielding `SplFileInfo` objects or paths according to flags.
128
156
| `onlyDirectories()` | Filters to directory entries only (requires `CURRENT_AS_FILEINFO`).
129
157
| `onlyFiles()` | Filters to file entries only (requires `CURRENT_AS_FILEINFO`).
@@ -134,7 +162,7 @@ Flow ships with a set of custom iterators that integrate seamlessly with SPL:
134
162
135
163
| Iterator | Purpose |
136
164
| --- | --- |
137
-
| `CachedIterator` | *Memoises* another iterator so that subsequent traversals reuse cached values.
165
+
| `CachedIterator` | *Memoizes* another iterator so that subsequent traversals reuse cached values.
138
166
| `ConditionalIterator` | Iterates until a callback vetoes further processing.
139
167
| `FlipIterator` | Swaps keys for values (and optionally values for keys) while delegating iteration.
140
168
| `FunctionIterator` | Creates generators from callbacks so you can yield values without native generators.
@@ -162,7 +190,7 @@ The `globals.php` helpers make it effortless to adopt Flow throughout an applica
162
190
163
191
## Notes
164
192
165
-
Some operations (such as `reverse()` or `sort()`) need to materialise the stream into an array before continuing. The
193
+
Some operations (such as `reverse()` or `sort()`) need to materialize the stream into an array before continuing. The
166
194
library only buffers data when absolutely necessary and automatically returns to streaming mode afterwards.
0 commit comments