/** * @param array $cli * * @return string */ function cli_serialize($cli) { debug_assert(array_key_exists('param', $cli) && array_key_exists('flag', $cli) && array_key_exists('input', $cli)); $ret = $cli['input'][0]; foreach ($cli['param'] as $name => $val) { $ret .= ' --' . $name . '=' . escapeshellarg($val); } foreach ($cli['flag'] as $name => $val) { $ret .= ' -' . $name . ' ' . escapeshellarg($val) . ' '; } $cnt = count($cli['input']); for ($i = 1; $i < $cnt; $i++) { $ret .= ' ' . $cli['input'][$i]; } return $ret; }
/** * @param string $lang */ function locale($lang) { global $config; $lang = str_replace('..', '', $lang); $lang = str_replace('/', '', $lang); if (file_exists($file = '../locale/' . $lang . '.php')) { require_once $file; } elseif (file_exists($file = '../locale/en_US.php')) { require_once $file; } else { $locale = array(); } if (debug_assert(isset($locale), "locale {$lang} isn't defined")) { $config->lang = $lang; $config->locale = object($locale); } }
/** * @param string $command * @param array|null &$output * @param int|null &$retval * @return string */ function proc_exec($command, &$output = array(), &$retval = null) { $descriptors = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w")); $res = proc_open($command, $descriptors, $pipes); fclose($pipes[0]); $stdout = stream_get_contents($pipes[1]); fclose($pipes[1]); $stderr = stream_get_contents($pipes[2]); fclose($pipes[2]); $output = explode(PHP_EOL, $stdout); $retval = proc_close($res); if (0 !== $retval) { logger_log("Process returned error." . PHP_EOL . " * Cli: " . $command . PHP_EOL . " * Return value: " . $retval . PHP_EOL . " * Stderr: " . PHP_EOL . str_indent($stderr, 1) . PHP_EOL . " * Stdout: " . PHP_EOL . str_indent($stdout, 1) . PHP_EOL); debug_assert(false); // FIXME: should be enforce but i wont risk it now } $ol = count($output); return $ol > 0 ? $output[$ol - 1] : ''; }
<?php require __DIR__ . '/../debug.php'; require __DIR__ . '/../arr.php'; 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] ) );
<?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 array $array * @param callable $iterator * * @return array */ function array_filter_key($array, $iterator) { if (debug_assert(is_array($array) && is_callable($iterator), 'Invalid parameters')) { $mapped = array_map_val($array, $iterator); $filtered = array_filter($mapped, function ($val) { return true === $val; }); return array_intersect_key($array, $filtered); } else { return array(); } }
<?php // Copyright IF Research Sp. z o.o. 2013. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'loader.php'; loader_include_dir_recursive(__DIR__); debug_assert(false, 'NO ERROR');
/** * @param string $string * @return string */ function html_from_string($string) { if (debug_assert(is_string($string), var_dump_human_compact($string))) { $ret = htmlspecialchars($string, htmlspecialchars_flags()); } else { $ret = ''; } 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; }); }
<?php if (class_exists('Zend_Log')) { debug_assert(function () { foreach (array(LOG_EMERG => Zend_Log::EMERG, LOG_ALERT => Zend_Log::ALERT, LOG_CRIT => Zend_Log::CRIT, LOG_ERR => Zend_Log::ERR, LOG_WARNING => Zend_Log::WARN, LOG_NOTICE => Zend_Log::NOTICE, LOG_INFO => Zend_Log::INFO, LOG_DEBUG => Zend_Log::DEBUG) as $k => $v) { if ($k != $v) { return false; } } return true; }, 'One of log levels differ. Update the library.'); }
/** * @param int $level * @return string */ function log_level_str($level) { $map = array(LOG_EMERG => 'EMERG', LOG_ALERT => 'ALERT', LOG_CRIT => 'CRIT', LOG_ERR => 'ERR', LOG_WARNING => 'WARNING', LOG_NOTICE => 'NOTICE', LOG_INFO => 'INFO', LOG_DEBUG => 'DEBUG'); if (debug_assert(array_key_exists($level, $map), 'Unknown log level')) { $level = $map[$level]; } return $level; }
/** * @param callable $object * @param string|callable $what * @return callable */ function object_get_dg($object, $what) { if (is_string($what)) { $what = return_dg($what); } if (debug_assert_type($object, 'callable') && debug_assert($what, 'callable')) { return function () use($object, $what) { return object_get($object(), $what()); }; } else { return function () { return null; }; } }
/** * @param array $pairs * @return array */ function http_accept_sort($pairs) { usort($pairs, function ($a, $b) { $firstA = -1; $firstB = 1; $undefined = 0; if (1 == count($a)) { return $firstA; } if (1 == count($b)) { return $firstB; } debug_assert(count($a) == 2 && count($b) == 2); if ($a[1] == $b[1]) { return $undefined; } if ($a[1] < $b[1]) { return $firstB; } if ($a[1] > $b[1]) { return $firstA; } debug_enforce(false); return $undefined; }); return $pairs; }
/** * FIXME: use cursors * @param $page * @param $rowCount * * @return $this */ public function setLimitPage($page, $rowCount) { debug_assert(false); return $this; }
/** * Return row object for current id * * @return Zend_Db_Table_Row|null * @deprecated */ public function getRow() { debug_assert(false, 'Function getRow is scheduled for deletion, replace with Model::getById( $id )'); $key = $this->getPrimaryKey(); if (null !== $key) { $ret = static::getById($key); } else { $ret = null; } return $ret; }
/** * TODO: move to external class / trait * @param $type * @return string */ private function _getFormType($type) { $formType = 'text'; switch ($type) { case "boolean": case "bool": $formType = 'checkbox'; break; case "array": $formType = 'multiCheckbox'; break; case "date": $formType = 'date'; break; case "datetime": $formType = 'datetime'; break; case "time": $formType = 'time'; break; case "uint": case "int": $formType = 'int'; break; case "float": $formType = 'float'; break; case "email": $formType = 'email'; break; case "host": $formType = 'host'; break; case "text": $formType = 'text'; break; case "currency": $formType = 'currency'; break; case "country": $formType = 'country'; break; case "hidden": $formType = 'hidden'; break; default: debug_assert(false !== array_search($type, self::$types), "Unknown Form Type `{$type}`"); break; } return $formType; }
/** * @static * @param array $conditions * @param array|callable|null $constructor * * @return static */ public static function getBy($conditions, $constructor = null) { $ret = self::getListBy($conditions, $constructor); $count = count($ret); if ($count === 0) { return static::find(); } else { debug_assert($count === 1, 'getBy expects single or no result, but `' . $count . '` resulted.'); return array_shift($ret); } }
/** * @param mixed $subject * @param callable $predicate * @param callable|null $on_fail * @return mixed */ function ensure($subject, $predicate, $on_fail = null) { if (null === $on_fail) { $on_fail = function ($subject) { debug_assert(false, "Could not assure about variable: " . var_dump_human_compact($subject)); return $subject; }; } if (call_user_func($predicate, $subject)) { $ret = $subject; } else { $ret = call_user_func($on_fail, $subject); } return $ret; }
/** * @param resource $resource * @param string $delimiter * @param string $enclosure * @param string $escape * @return array */ function res_to_csv_assoc($resource, $delimiter = ',', $enclosure = '"', $escape = '\\') { $ret = []; $header = fgetcsv($resource, 0, $delimiter, $enclosure, $escape); if (debug_assert($header !== false)) { while (true) { $row = fgetcsv($resource, 0, $delimiter, $enclosure, $escape); if ($row === false) { break; } else { $ret[] = array_map_key($row, function ($value, $key) use($header) { return $header[$key]; }); } } } return $ret; }