'apple', 200 => 'banana']; * $opposite = mapkv($data, function($k, $v){ return [-1 * $k => strtoupper($v)]; }); * * This would return [-100 => 'APPLE', -200 => 'BANANA'] * * By convention, mapping functions should return an 1-row array "[newKey => newValue]". * * Some unconventional forms are also defined: * - Return empty array ==> Skip/omit the row * - Return multiple items ==> Add all items to the result * - Return an unkeyed (numeric) array ==> Discard original keys. Items are appended numerically (`$arr[] = $value`). * * @param array $array * Values to iterate over * @param callable $func * Callback function. * function(scalar $key, mixed $value): array * @return array * The filtered array. */ public function mapkv($array, $func) { $r = []; foreach ($array as $k => $v) { foreach ($func($k, $v) as $out_k => $out_v) { if (isset($r[$out_k])) { $r[] = $out_v; } else { $r[$out_k] = $out_v; } } } return $r; } /** * Map file-names. * * @param string $matchPat * Ex: 'src/*.json' * @param string $outPat * Ex: 'dest/#1.json' * @param bool $flip * The orientation of the result map. * If false, returned as "original => filtered". * If true, returned as "filtered => original". * @return array * List of files and the corresponding names. */ public function globMap($matchPat, $outPat, $flip = FALSE) { $inFiles = glob($matchPat); $regex = ';' . preg_quote($matchPat, ';') . ';'; $regex = str_replace(preg_quote('*', ';'), '(.*)', $regex); $replacement = preg_replace(';#(\d+);', '\\' . '\\\1', $outPat); $outFiles = preg_replace($regex, $replacement, $inFiles); return $flip ? array_combine($outFiles, $inFiles) : array_combine($inFiles, $outFiles); } public function chdir($directory) { if (!\chdir($directory)) { throw new IOException("Failed to change directory ($directory)"); } } /** * @param string|string[] $pats * List of glob patterns. * @param null|int $flags * @return array * List of matching files. */ public function glob($pats, $flags = NULL) { $r = []; $pats = (array) $pats; foreach ($pats as $pat) { $r = array_unique(array_merge($r, (array) \glob($pat, $flags))); } sort($r); return $r; } /** * Read a set of files and concatenate the results * * @param string|string[] $srcs * Files to read. These may be globs. * @param string $newLine * Whether to ensure that joined files have a newline separator. * Ex: 'raw' (as-is), 'auto' (add if missing) * @return string * The result of joining the files. */ public function cat($srcs, $newLine = 'auto') { $buf = ''; foreach (glob($srcs) as $file) { if (!is_readable($file)) { throw new \RuntimeException("Cannot read $file"); } $buf .= file_get_contents($file); switch ($newLine) { case 'auto': if (substr($buf, -1) !== "\n") { $buf .= "\n"; } break; case 'raw': // Don't break; } } return $buf; } } __halt_compiler();----SIGNATURE:----b0N7nMzVtkdG1HnwOcSxhlz7r0auRhI1vMmFW45jDy4j/BTCRz0GGb3OkoPhQvc0Dpt9h4YqFxuhwDYCR6QS99GYw21iAbHFFJpyMcs8RRx+HFMqVRg7MYKuzsUTsGrsOb1Z3X791f+UbfPLRDKs9Jl81wB665QrGXShr5EppeJl6NoX0WguNXdjYwkp+c9k4/S3PgGlHGIELBcPe5XKwoIT+ZJzNo6e73lD5Y2nuCjhhWDfA9wRaA15zTscXPrXvuKuAdepO6cXlKc2oc9rArTFdyM77y2tHqD3SQWauMrC5W0YaCwTOqg3D71Wo5bI8BF0AVYpLBQG+evnNiCdkT0hKAkop1fqptNsBPzjd61I44hoa1L9sc7zCE1RzvD6QnojMbzdo4DF/6r8ZaeMoCH1hNwhbQESBZBcoy0kdl8mKwDjtC/Gx5f3xT9pFkdBb1wxSm7db6wTCgQPintDOBf5BQJEiWEraerLgv480sq3K424Y77m73ZginK90vjpazrjPlS7mVT25DQKR1UnGFfln9pOZ1goMpwi6wGAERCkf8ptq9+vWCBKmMg51OLCfP9mUCz7MMAtJB6h5iqtvBtL9NWUB1XWAjLeM/bXOhfUXhxTLRPwZq4/6qAkV5GE8DH1l8YgZ5vzOPjhvBggzZVAd31ViNh50XoTE1Zyj5g=----ATTACHMENT:----ODE0NjkxMTY5MTkxNjI4NSA5NjI3MjY2OTA1MTIxNTA5IDkxNjUzNjY2MTgwNTU5NzU=