diff --git a/src/Util/Functional.php b/src/Util/Functional.php new file mode 100644 index 0000000000..eb5297663e --- /dev/null +++ b/src/Util/Functional.php @@ -0,0 +1,67 @@ +. +// }}} + +/** + * 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; + +abstract class Functional +{ + /** + * TODO replace with \Functional\cartesian_product when it gets merged upstream + */ + public static function cartesianProduct(string|array $separator, ...$collections) + { + $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; + } +}