/** * @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); }; }
function universal_to_string($thing, $indent = 0) { if (is_array($thing)) { if (is_array_list($thing)) { return list_to_string($thing, $indent); } return dictionary_to_string($thing, $indent); } if ($thing === null) { return 'null'; } if ($thing === false) { return 'false'; } if ($thing === true) { return 'true'; } if ($thing === 0) { return '0'; } if (is_numeric($thing)) { return '' . $thing; } if (is_string($thing)) { return '"' . str_replace(array("\n", "\r", "\t", "\""), array("\\n", "\\r", "\\t", "\\\""), $thing) . '"'; } return '<UNDEFINED TO STRING>' . $thing . '</UNDEFINED TO STRING>'; }
/** * @param string $string * @param string|array|callable $allow * @return string * @throws Exception */ function str_filter($string, $allow) { debug_enforce_type($string, 'string'); if (is_array_list($allow)) { debug_enforce(array_all($allow, tuple_get(0, 'is_string')), "Unhandled value for allow parameter: " . var_dump_human_compact($allow)); $allow = implode('', $allow); } if (is_string($allow)) { $allow = str_contains_dg(tuple_get(0), $allow); } $ret = ''; $len = strlen($string); for ($i = 0; $i < $len; $i++) { $char = $string[$i]; if ($allow($char)) { $ret .= $char; } } return $ret; }
/** * @param mixed $form * @param string $prefix * @return array */ function form_flatten($form, $prefix = '') { $ret = array(); if (is_object($form)) { $form = get_object_vars($form); } if (is_array($form)) { if (is_array_assoc($form)) { foreach ($form as $k1 => $v1) { $ret = array_merge($ret, form_flatten($v1, empty($prefix) ? $k1 : $prefix . "[{$k1}]")); } } elseif (is_array_list($form)) { foreach ($form as $k1 => $v1) { $ret = array_merge($ret, form_flatten($v1, empty($prefix) ? $k1 : $prefix . "[]")); } } else { debug_assert(false, "Mixed array cannot be encoded by multipart_form_data_encode."); } } else { $ret[] = array('name' => $prefix, 'value' => $form); } return $ret; }