add support for multiple arguments and mutually exclusive params
authorMichael Wallner <mike@php.net>
Fri, 2 Mar 2018 09:54:52 +0000 (10:54 +0100)
committerMichael Wallner <mike@php.net>
Fri, 2 Mar 2018 09:54:52 +0000 (10:54 +0100)
gen-matrix.php

index 90e0011e555457661d1d5c9b1adca8a4ec203c62..e7ded6795d4c4d35e9ac09050c2980a9d21e333f 100644 (file)
@@ -1,26 +1,52 @@
 <?php
 
-return function ($array) {
-       foreach ($array as $key => $values) {
-               if (is_numeric($key) && is_string($values)) {
-                       $key = $values;
-                       $values = ["no","yes"];
-               }
-               if (empty($apc)) {
-                       // seed
-                       foreach ($values as $val) {
-                               $apc[] = "$key=$val";
+return function() {
+       $process = function($apc, $key, $values = ["no", "yes"]) {
+
+               return $apc;
+       };
+
+       foreach (func_get_args() as $array) {
+               $apc = [];
+               foreach ($array as $key => $values) {
+                       if (is_numeric($key) && is_string($values)) {
+                               // switch on yes/no
+                               $key = $values;
+                               $values = ["no", "yes"];
+                       } else if (is_numeric($key) && is_array($values)) {
+                               // mutually enasbled options
+                               $vpc = [];
+                               foreach ($values as $yes) {
+                                       $mpc = "$yes=yes ";
+                                       foreach ($values as $no) {
+                                               if ($yes === $no) {
+                                                       continue;
+                                               }
+                                               $mpc .= "$no=no ";
+                                       }
+                                       $vpc[] = $mpc;
+                               }
+                               $key = null;
+                               $values = $vpc;
                        }
-               } else {
-                       // combine
-                       $cpc = $apc;
-                       $apc = [];
-                       foreach ($values as $val) {
-                               foreach ($cpc as $e) {
-                                       $apc[] = "$e $key=$val";
+
+                       if (empty($apc)) {
+                               // seed
+                               foreach ((array) $values as $val) {
+                                       $apc[] = strlen($key) ? "$key=$val" : $val;
+                               }
+                       } else {
+                               // combine
+                               $cpc = $apc;
+                               $apc = [];
+                               foreach ((array) $values as $val) {
+                                       foreach ($cpc as $e) {
+                                               $apc[] = strlen($key) ? "$e $key=$val" : "$e $val";
+                                       }
                                }
                        }
                }
+               $xpc[] = $apc;
        }
-       return $apc;
+       return $xpc;
 };