Skip to content

Commit 00b2cbd

Browse files
committed
docs: update documentation
1 parent add4fe2 commit 00b2cbd

File tree

1 file changed

+40
-53
lines changed

1 file changed

+40
-53
lines changed

README.md

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,22 @@ Iterable functions
88

99
Provides additional functions to work with [iterable](https://wiki.php.net/rfc/iterable) variables (even on PHP5.3+).
1010

11-
is_iterable()
12-
-------------
13-
To check wether or not a PHP variable can be looped over in a `foreach` statement, PHP provides an `is_iterable()` function.
14-
15-
**But this function only works on PHP7.1+**.
16-
17-
This library ships a polyfill of this function for previous PHP versions.
18-
19-
Usage:
20-
```php
21-
var_dump(is_iterable(array('foo', 'bar'))); // true
22-
var_dump(is_iterable(new DirectoryIterator(__DIR__))); // true
23-
var_dump(is_iterable('foobar')); // false
24-
```
25-
2611
iterable_to_array()
2712
-------------------
2813

2914
PHP offers an `iterator_to_array()` function to export any iterator into an array.
3015

3116
**But when you want to transform an `iterable` to an array, the `iterable` itself can already be an array.**
3217

33-
When using `iterator_to_array()` with an array, PHP5 triggers a E_RECOVERABLE_ERROR while PHP7 throws a `TypeError`.
18+
When using `iterator_to_array()` with an iterable, that happens to be an array, PHP will throw a `TypeError`.
3419

3520
If you need an iterable-agnostic function, try our `iterable_to_array()`:
3621

3722
```php
38-
var_dump(iterable_to_array(new ArrayIterator(array('foo', 'bar')))); // ['foo', 'bar']
39-
var_dump(iterable_to_array(array('foo', 'bar'))); // ['foo', 'bar']
23+
use function BenTools\IterableFunctions\iterable_to_array;
24+
25+
var_dump(iterable_to_array(new \ArrayIterator(['foo', 'bar']))); // ['foo', 'bar']
26+
var_dump(iterable_to_array(['foo', 'bar'])); // ['foo', 'bar']
4027
```
4128

4229
iterable_to_traversable()
@@ -49,8 +36,10 @@ If your variable is an array, the function converts it to an `ArrayIterator`.
4936

5037
Usage:
5138
```php
52-
var_dump(iterable_to_traversable(array('foo', 'bar'))); // ArrayIterator(array('foo', 'bar'))
53-
var_dump(iterable_to_traversable(new ArrayIterator(array('foo', 'bar')))); // ArrayIterator(array('foo', 'bar'))
39+
use function BenTools\IterableFunctions\iterable_to_traversable;
40+
41+
var_dump(iterable_to_traversable(['foo', 'bar'])); // \ArrayIterator(['foo', 'bar'])
42+
var_dump(iterable_to_traversable(new \ArrayIterator(['foo', 'bar']))); // \ArrayIterator(['foo', 'bar'])
5443
```
5544

5645

@@ -60,6 +49,8 @@ iterable_map()
6049
Works like an `array_map` with an `array` or a `Traversable`.
6150

6251
```php
52+
use function BenTools\IterableFunctions\iterable_map;
53+
6354
$generator = function () {
6455
yield 'foo';
6556
yield 'bar';
@@ -76,6 +67,8 @@ iterable_reduce()
7667
Works like an `reduce` with an `iterable`.
7768

7869
```php
70+
use function BenTools\IterableFunctions\iterable_reduce;
71+
7972
$generator = function () {
8073
yield 1;
8174
yield 2;
@@ -96,6 +89,8 @@ iterable_filter()
9689
Works like an `array_filter` with an `array` or a `Traversable`.
9790

9891
```php
92+
use function BenTools\IterableFunctions\iterable_filter;
93+
9994
$generator = function () {
10095
yield 0;
10196
yield 1;
@@ -108,6 +103,8 @@ foreach (iterable_filter($generator()) as $item) {
108103

109104
Of course you can define your own filter:
110105
```php
106+
use function BenTools\IterableFunctions\iterable_filter;
107+
111108
$generator = function () {
112109
yield 'foo';
113110
yield 'bar';
@@ -123,54 +120,43 @@ foreach (iterable_filter($generator(), $filter) as $item) {
123120
}
124121
```
125122

123+
Iterable fluent interface
124+
=========================
126125

127-
Iterable factory
128-
================
129-
130-
When you have an `iterable` type-hint somewhere, and don't know in advance wether you'll pass an `array` or a `Traversable`, just call the magic `iterable()` factory:
126+
The `iterable` function allows you to wrap an iterable and apply some common operations.
131127

128+
With an array input:
132129
```php
133-
interface SomeInterface
134-
{
135-
/**
136-
* Return an iterable list of items
137-
*
138-
* @return iterable
139-
*/
140-
public function getItems(): iterable;
141-
}
142-
143-
class MyService implements SomeInterface
144-
{
145-
/**
146-
* @inheritdoc
147-
*/
148-
public function getItems(): iterable
149-
{
150-
return iterable($this->someOtherService->findAll()):
151-
}
130+
use function BenTools\IterableFunctions\iterable;
131+
$data = [
132+
'banana',
133+
'pineapple',
134+
'rock',
135+
];
152136

153-
}
137+
$iterable = iterable($data)->filter(fn($eatable) => 'rock' !== $eatable)->map('strtoupper'); // Traversable of ['banana', 'pineapple']
154138
```
155139

156-
It even accepts a `null` value (then converting it to an `EmptyIterator`).
157-
158-
You may add a `filter` callable and a `map` callable to make your life easier:
159-
140+
With a traversable input:
160141
```php
142+
use function BenTools\IterableFunctions\iterable;
161143
$data = [
162144
'banana',
163145
'pineapple',
164-
'potato',
146+
'rock',
165147
];
166148

167-
$isFruit = function ($eatable) {
168-
return 'potato' !== $eatable;
169-
};
149+
$data = fn() => yield from $data;
170150

171-
var_dump(iterator_to_array(iterable($data)->filter($isFruit)->map('strtoupper'))); // ['banana', 'pineapple']
151+
$iterable = iterable($data())->filter(fn($eatable) => 'rock' !== $eatable)->map('strtoupper'); // Traversable of ['banana', 'pineapple']
172152
```
173153

154+
Array output:
155+
```php
156+
$iterable->asArray(); // array ['banana', 'pineapple']
157+
```
158+
159+
174160
Installation
175161
============
176162

@@ -186,6 +172,7 @@ require_once '/path/to/this/library/src/iterable-functions.php';
186172

187173
Unit tests
188174
==========
175+
189176
```
190177
./vendor/bin/phpunit
191178
```

0 commit comments

Comments
 (0)