/** * @param callable|null $getter * * @return callable */ function boolval_dg($getter = null) { if ($getter === null) { $getter = tuple_get(); } return function () use($getter) { return boolval(call_user_func_array($getter, func_get_args())); }; }
function parallel_each_dg($callable, $array = null) { if ($array === null) { $array = tuple_get(0); } elseif (!is_callable($array)) { $array = return_dg($array); } return function () use($callable, $array) { $args = func_get_args(); parallel_each(call_user_func_array($array, $args), $callable); }; }
/** * @param callable $filter * @param callable|array|null $list * @return callable */ function list_filter_key_dg($filter, $list = null) { if (null === $list) { $list = tuple_get(); } elseif (is_array_list($list)) { $list = return_dg($list); } else { debug_assert_type($list, 'callable'); } debug_assert_type($filter, 'callable'); return function () use($list, $filter) { $args = func_get_args(); $list = call_user_func_array($list, $args); return list_filter_key($list, $filter); }; }
/** * @param ... $values * @return mixed */ function tuple_max($values) { $args = array_filter_key(func_get_args(), not_dg(eq_dg(tuple_get(), return_dg(null)))); $argsCount = count($args); if ($argsCount < 1) { $ret = null; } elseif ($argsCount === 1) { $ret = array_shift($args); } else { $ret = call_user_func_array('max', $args); } return $ret; }
function debug_enforce_type_dg($type, $var = null) { if (is_string($type)) { $type = return_dg($type); } else { debug_enforce_type($type, 'callable'); } if (null === $var) { $var = tuple_get(); } else { debug_enforce_type($var, 'callable'); } return function () use($type, $var) { $args = func_get_args(); return debug_enforce_type(call_user_func_array($var, $args), call_user_func_array($type, $args)); }; }
/** * WARNING: proper string/expression escaping is missing * @param array|Zend_Db_Expr|string $expr * @param string|null $alias * @param string|null $separator * @param bool|null $distinct * @param null|array|int|string|Zend_Db_Expr $order * @return $this */ public function groupConcatSet($expr, $alias = null, $separator = null, $distinct = null, $order = null) { debug_assert(is_string($expr) || is_array($expr) || is_object($expr) && $expr instanceof Zend_Db_Expr, "Invalid expression value `" . var_dump_human_compact($expr) . "`"); debug_assert(is_null($alias) || is_string($alias), "Invalid alias value `" . var_dump_human_compact($alias) . "`"); debug_assert(is_null($separator) || is_string($separator), "Invalid separator value `" . var_dump_human_compact($separator) . "`"); debug_assert(is_null($distinct) || is_bool($distinct), "Invalid distinct value `" . var_dump_human_compact($distinct) . "`"); debug_assert(is_null($order) || is_array($order) || is_int($order) || is_string($order) || is_object($order) && $order instanceof Zend_Db_Expr, "Invalid order value `" . var_dump_human_compact($order) . "`"); $convertToString = function ($expr) { if (is_object($expr)) { $expr = strval($expr); } else { if (array_key_exists($expr, $this->schemaColumnsGet())) { $expr = str_wrap($this->getAlias(), '`') . '.' . str_wrap($expr, '`'); } elseif (is_string($expr)) { $expr = str_wrap($expr, '\''); } } return $expr; }; if (is_array($expr)) { $expr = array_chain($expr, array_map_val_dg(tuple_get(0, $convertToString)), array_implode_dg(',')); } else { $expr = $convertToString(strval($expr)); } if ($distinct !== null) { $expr = "DISTINCT {$expr}"; } if ($order !== null) { if (is_array($order)) { $order = array_chain($expr, array_map_val_dg(tuple_get(0, $convertToString)), array_implode_dg(',')); } else { $order = $convertToString(strval($order)); } $expr = "{$expr} ORDER BY {$order}"; } if ($separator !== null) { $expr = "{$expr} SEPARATOR '{$separator}'"; } $expr = "GROUP_CONCAT({$expr})"; if ($alias !== null) { $expr = [$alias => $expr]; } return $this->setColumns($expr, $this->getAlias()); }
<?php require __DIR__ . '/../file_system.php'; require __DIR__ . '/../cli.php'; require __DIR__ . '/../debug.php'; require __DIR__ . '/../lst.php'; require __DIR__ . '/../string.php'; require __DIR__ . '/../bool.php'; debug_assert(str_filter('0A--B', not_dg(and_dg(eq_dg(tuple_get(), return_dg('-')), eq_dg(tuple_carry(), tuple_get())))) === '0A-B'); debug_assert(eq_dg(str_filter_dg(not_dg(and_dg(eq_dg(tuple_get(), return_dg('-')), eq_dg(tuple_carry(), tuple_get()))), '0A--B'), return_dg('0A-B'))); debug_assert(eq_dg(str_filter_dg(not_dg(and_dg(eq_dg(tuple_get(), return_dg('-')), eq_dg(tuple_carry(), tuple_get()))), return_dg('0A--B')), return_dg('0A-B'))); debug_assert(call_chain(return_dg('0A--B'), str_filter_dg(not_dg(and_dg(eq_dg(tuple_get(), return_dg('-')), eq_dg(tuple_carry(), tuple_get())))), eq_dg(tuple_get(0), return_dg('0A-B'))));
/** * @param string|callable|null $separator * @param array|callable|null * @return callable */ function array_implode_dg($separator = null, $array = null) { if (null === $separator) { $separator = return_dg(PHP_EOL); } elseif (!is_callable($separator)) { $separator = return_dg($separator); } if (is_null($array)) { $array = tuple_get(0); } elseif (is_array($array)) { $array = return_dg($array); } else { debug_enforce_type($array, 'callable'); } return function () use($separator, $array) { $args = func_get_args(); return implode(call_user_func_array($separator, $args), call_user_func_array($array, $args)); }; }
<?php require __DIR__ . '/../../debug.php'; require __DIR__ . '/../../arr.php'; debug_assert(array_eq(call_user_func(array_dg(tuple_get(1), tuple_get(0)), 1, 2), [2, 1])); debug_assert(array_eq(array_first(range(1, 5), 3), [1, 2, 3])); debug_assert(array_eq(array_initial(range(1, 5), 3), [1, 2])); //TODO: array_to debug_assert(array_eq(array_last(range(1, 5), 3), [3, 4, 5])); //debug_assert( array_eq( array_to( range(1,5), 3 ), [1,2,3] ) ); //debug_assert( array_eq( array_from( range(1,5), 3 ), [3,4,5] ) ); // TODO: array_rest //debug_assert( array_eq( array_before( range(1,5), 3 ), [1,2] ) ); //debug_assert( array_eq( array_after( range(1,5), 3 ), [4,5] ) );
/** * @param string|callable|null $char * @param string|callable|null $placeholder * @return string */ function char_accent_map_dg($char = null, $placeholder = null) { if (null === $char) { $char = tuple_get(0); } elseif (is_char($char)) { $char = return_dg($char); } else { debug_enforce_type($char, 'callable'); } if (is_null($placeholder)) { $placeholder = return_dg('-'); } elseif (is_string($placeholder)) { $placeholder = return_dg($placeholder); } else { debug_enforce_type($placeholder, 'callable'); } $map = char_accent_map_element(); return function () use($map, $char, $placeholder) { $args = func_get_args(); $c = call_user_func_array($char, $args); $ret = array_key_exists($c, $map) ? $map[$c] : call_user_func_array($placeholder, $args); return $ret; }; }
/** * @param string|callable|null $class * @param object|callable|null $object * * @return callable */ function instanceof_dg($class = null, $object = null) { if (is_null($object)) { $object = tuple_get(0); } elseif (is_object($object)) { $object = return_dg($object); } else { debug_enforce_type($object, 'callable'); } if (is_null($class)) { $class = tuple_get(1); } elseif (is_string($class)) { $class = return_dg($class); } else { debug_enforce_type($class, 'callable'); } return function () use($object, $class) { $args = func_get_args(); $object = call_user_func_array($object, $args); $class = call_user_func_array($class, $args); return $object instanceof $class; }; }
/** * @param string|array $content * @param string $basename * @param string|finfo|null $mime * @todo http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt */ function http_response_file_download($content, $basename, $mime = null) { if (is_array($content)) { list($content, $length) = $content; } else { debug_enforce_type($content, 'string'); $length = strlen($content); } if (null === $mime && class_exists('finfo')) { $mime = new finfo(); } if (is_object($mime) && $mime instanceof finfo) { $mime = $mime->buffer($content, FILEINFO_MIME); } $headers = ['Pragma: public', 'Expires: -1', 'Cache-Control: public, must-revalidate, post-check=0, pre-check=0', "Content-Disposition: attachment; filename=\"{$basename}\"", "Content-Type: {$mime}", "Content-Length: {$length}"]; http_output_compression(false); array_each($headers, header_dg()); $download = function () use($content) { print $content; ob_flush(); flush(); }; call_safe(max_execution_time_set_dg(0), $download, max_execution_time_set_dg(tuple_get(0))); }
function is_file_dg($path = null) { if (null === $path) { $path = tuple_get(0); } elseif (!is_callable($path)) { $path = return_dg($path); } return function () use($path) { $args = func_get_args(); return is_file(call_user_func_array($path, $args)); }; }