1212-------
1313
1414``` php
15- require_once __DIR__ . '/vendor/autoload.php';
16-
1715use function BenTools\CartesianProduct\combinations;
1816
1917$data = [
@@ -103,15 +101,12 @@ Array
103101)
104102```
105103
106-
107104Combinations count
108105------------------
109106
110- You can simply count how many combinations your data produce:
107+ You can simply count how many combinations your data produce (this will not generate any combination) :
111108
112109``` php
113- require_once __DIR__ . '/vendor/autoload.php';
114-
115110use function BenTools\CartesianProduct\combinations;
116111
117112$data = [
@@ -132,11 +127,54 @@ $data = [
132127var_dump(count(combinations($data))); // 2 * 3 * 2 = 12
133128```
134129
130+ Filtering combinations
131+ ----------------------
132+
133+ You can filter combinations using the ` filter ` method. This is useful if you want to skip some combinations based on certain criteria:
134+
135+ ``` php
136+ use function BenTools\CartesianProduct\combinations;
137+
138+ $data = [
139+ 'hair' => [
140+ 'blond',
141+ 'black'
142+ ],
143+ 'eyes' => [
144+ 'blue',
145+ 'green',
146+ ]
147+ ];
148+
149+ foreach (combinations($data)->filter(fn (array $combination) => 'green' !== $combination['eyes']) as $combination) {
150+ printf('Hair: %s - Eyes: %s' . PHP_EOL, $combination['hair'], $combination['eyes']);
151+ }
152+ ```
153+
154+ Map output
155+ ----------
156+
157+ You can use the ` each ` method to transform each combination into a different format:
158+
159+ ``` php
160+ use App\Entity\Book;
161+
162+ use function BenTools\CartesianProduct\combinations;
163+
164+ $books = [
165+ 'author' => ['Isaac Asimov', 'Arthur C. Clarke'],
166+ 'genre' => ['Science Fiction', 'Fantasy'],
167+ ]
168+
169+ foreach (combinations($books)->each(fn (array $combination) => Book::fromArray($combination)) as $book) {
170+ assert($book instanceof Book);
171+ }
172+ ```
135173
136174Installation
137175------------
138176
139- PHP 7.4 + is required.
177+ PHP 8.2 + is required.
140178```
141179composer require bentools/cartesian-product
142180```
@@ -146,7 +184,6 @@ Performance test
146184The following example was executed on my Core i7 personnal computer with 8GB RAM.
147185
148186``` php
149- require_once __DIR__ . '/vendor/autoload.php';
150187use function BenTools\CartesianProduct\combinations;
151188
152189$data = array_fill(0, 10, array_fill(0, 5, 'foo'));
@@ -172,7 +209,7 @@ Output:
172209Unit tests
173210----------
174211```
175- ./vendor/bin/phpunit
212+ ./vendor/bin/pest
176213```
177214
178215
0 commit comments