. // }}} /** * String formatting utilities * * @package GNUsocial * @category Util * * @author Hugo Sales * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ namespace App\Util; use Stringable; abstract class Functional { /** * TODO replace with \Functional\cartesian_product when it gets merged upstream * * @param array> $collections */ public static function cartesianProduct(array $collections, string|array $separator = ''): array { $aggregation = []; $left = array_shift($collections); while (true) { $right = array_shift($collections); foreach ($left as $l) { foreach ($right as $r) { if (\is_string($separator)) { $aggregation[] = "{$l}{$separator}{$r}"; } elseif (\is_array($separator)) { foreach ($separator as $sep) { $aggregation[] = "{$l}{$sep}{$r}"; } } } } if (empty($collections)) { break; } else { $left = $aggregation; $aggregation = []; } } return $aggregation; } }