public function serializeContent() { return array_map_val($this->recordColumnsGet(), function ($row) { if ($row instanceof Lib_Model) { return $row->serialize(); } return $row; }); }
/** * @param ... $getters * @return callable */ function tuple_max_dg($getters) { $predicates = func_get_args(); return function () use($predicates) { $args2 = func_get_args(); return call_user_func_array('tuple_max', array_map_val($predicates, function ($arg, $n) use($args2) { if (is_callable($arg)) { return call_user_func_array($arg, $args2); } elseif (is_null($arg)) { return $args2[$n]; } else { return $arg; } })); }; }
/** * @param \Exception $exception * @param string $eol * @return string */ function exception_str($exception, $eol = null) { if ($eol === null) { $eol = "\n"; } $ret = ''; $prefix = "Uncaught"; if (debug_assert($exception instanceof \Exception)) { do { $class = get_class($exception); $msg = $exception->getMessage(); $file = $exception->getFile(); $line = $exception->getLine(); $trace = backtrace_string(0, $exception->getTrace()); $ret .= "{$prefix} {$class} in {$file}:{$line}" . $eol . str_indent("Message:" . $eol . implode($eol, array_map_val(explode($eol, $msg), function ($val, $key) { return str_indent($val, 1); })), 1) . $eol; if (!empty($trace)) { $ret .= str_indent("Backtrace:" . $eol . str_indent($trace, 1), 1) . $eol; } $prefix = "Caused by"; $exception = $exception->getPrevious(); } while ($exception !== null); } return $ret; }
/** * @param array $commands * @param callable $onerror ($command,$code,$stderr) * @return array */ function parallel_exec($commands, $onerror = null) { $spec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')); $errors = array(); $ret = array(); foreach (array_chunk($commands, hw_core_get(), true) as $chunk) { $processes = array(); foreach ($chunk as $key => $command) { $key = to_hash($key); $process = proc_open($command, $spec, $pipes); if (is_resource($process)) { fclose($pipes[0]); $processes[$key] = array('cmd' => $command, 'res' => $process, 'out' => $pipes[1], 'err' => $pipes[2]); } else { $processes[$key] = $command; } } while (!empty($processes)) { foreach ($processes as $key => $v) { $resource = $v['res']; $cmd = $v['cmd']; if (is_resource($resource)) { $status = proc_get_status($resource); if (!$status['running']) { $code = $status['exitcode']; $out = stream_get_contents($v['out']); fclose($v['out']); $err = stream_get_contents($v['err']); fclose($v['err']); proc_close($resource); unset($processes[$key]); if (0 !== $code) { $errors[$key] = array('error' => "Error code '{$code}'", 'cmd' => $cmd, 'err' => $err); } else { $ret[$key] = $out; } } } else { unset($processes[$key]); $errors[$key] = array('error' => "Could not open process", 'cmd' => $cmd); } } } } if (null !== $onerror) { foreach ($errors as $error) { $onerror($error['cmd'], $error['error'], array_key_exists('err', $error) ? $error['err'] : null); } } else { $format_errors = function ($errors) { $errors = array_map_val($errors, function ($error) { global $cli_format_error; return $cli_format_error($error['cmd'], $error['error'], array_key_exists('err', $error) ? $error['err'] : null); }); return implode(PHP_EOL . '===' . PHP_EOL, $errors); }; debug_enforce(empty($errors), $format_errors($errors)); } return $ret; }
/** * @param callable|string $charlist * @param callable|string|null $string * * @return callable */ function trim_dg($charlist = null, $string = null) { $getters = []; if (null === $string) { $string = tuple_get(0); } elseif (is_string($string)) { $string = return_dg($string); } else { debug_enforce_type($string, 'callable'); } $getters[] = $string; if (null !== $charlist) { if (is_string($charlist)) { $charlist = return_dg($charlist); } else { debug_enforce_type($charlist, 'callable'); } $getters[] = $charlist; } return function () use($getters) { $args = func_get_args(); return call_user_func_array('trim', array_map_val($getters, function ($getter) use($args) { return call_user_func_array($getter, $args); })); }; }
/** * @param $array * @param $callable * @return array */ function array_sort($array, $callable) { $mapped = array_map_val($array, $callable); asort($mapped); $ret = array_map_val($mapped, function ($sortKey, $key) use($array) { return $array[$key]; }); return $ret; }
/** * Returns result keys (aliases). * @return array */ public function getColumnAliases() { return array_map_val($this->getColumns(), function () { $column = func_get_arg(0); $alias = $column[2] === null ? $column[1] : $column[2]; return $alias; }); }
/** * @param object $config * @param callable $callable * @return object */ function object_map_val($config, $callable) { return (object) array_map_val((array) $config, function () use($callable) { return call_user_func_array($callable, func_get_args()); }); }
/** * @param array $packet * @return string */ function http_assemble($packet) { debug_enforce(array_key_exists('Host', $packet) && is_string($packet['Host']), 'Invalid Host Header'); $method = array_key_exists('Method', $packet) ? array_get_unset($packet, 'Method') : 'GET'; $version = array_key_exists('HTTPv', $packet) ? array_get_unset($packet, 'HTTPv') : 'HTTP/1.1'; $resource = array_key_exists('Path', $packet) ? array_get_unset($packet, 'Path') : '/'; $content = array_get_unset($packet, 'Content'); if (is_array($content)) { list($content, $packet) = mime_multipart_encode($content, $packet); } $packet = array_merge(array('Connection' => 'close', 'Content-Length' => (string) strlen($content)), $packet); $ret = "{$method} {$resource} {$version}\r\n"; $ret .= implode('', array_map_val($packet, 'mime_header_entry_map')) . "\r\n" . $content; return $ret; }
/** * @todo: please do not use $q for now * @param null $q * @param bool $collection do not index by id * * @return array */ public function load($q = null, $collection = false) { $alias = $this->getAlias(); $array = $this->loadArray($q, $collection); $t = $this; $ret = array_map_val($array[$alias], function () use($t) { $row = func_get_arg(0); $obj = $t->modelFactory($row); $obj->changedColumnsReset(); return $obj; }); return $ret; }
/** * @param $pattern * @param int $flags * @return array */ function glob_match($pattern, $flags = 0) { $regex = str_glob2regexp($pattern, $flags & GLOB_BRACE); return array_map_val(glob($pattern, $flags), function ($path) use($regex, $pattern) { debug_assert(preg_match($regex, $path, $matches), "Cannot match '{$regex}' on '{$path}' ( generated by '{$pattern}' )"); return $matches; }); }
/** * @param string $term * @param array $eqCol * @param array $likeCol * @param array $additionalSelectCol * @return $this */ public function buildSearchCondition($term, array $eqCol = array(), array $likeCol = array(), array $additionalSelectCol = array()) { $collate = 'utf8_general_ci'; $mappedTerm = str_map($term, function ($char) { return ctype_alnum($char) ? $char : ' '; }); $db = $this->getDb(); $firstOne = true; $where = array_chain(explode(' ', $mappedTerm), array_filter_key_dg(not_dg(empty_dg())), array_map_val_dg(function ($termPart) use($db, $likeCol, $eqCol, $collate, &$firstOne) { if ($firstOne) { $t1 = $db->quote("{$termPart}%", 'string'); $firstOne = false; } else { $t1 = $db->quote("%{$termPart}%", 'string'); } $t2 = $db->quote($termPart, 'string'); $whereLike = array_map_val($likeCol, function ($column) use($collate, $termPart, $t1) { return "{$column} COLLATE {$collate} LIKE {$t1}"; }); $whereEq = array_map_val($eqCol, function ($column) use($termPart, $t2) { return "{$column} = {$t2}"; }); $where = array_merge($whereLike, $whereEq); return '( ' . implode(' OR ', $where) . ' )'; })); $this->setColumns(array_merge($eqCol, $likeCol, $additionalSelectCol))->setWhere(implode(" AND ", $where)); return $this; }