function chain($baseItem, $items=array()) { if(!empty($items) && !empty($baseItem)) { return chain($baseItem->{f_first($items)}, f_rest($items)); //D::show('$baseItem->' . join('->', $items) . ';'); //return eval('$baseItem->' . join('->', $items) . ';') ?: null; } else { return $baseItem; } }
public static function __callStatic($tagName, $values=array()) { if(isset($values[0]) && is_array($values[0])) { //D::log($values[0], '0 values'); $attributes = ' ' . join(' ', f_keyMap( function($v, $k) { if(!empty($v)) { return $k . '="' . join(', ', (array)$v) . '"'; } }, $values[0] )); $childern = f_rest($values); } else { $attributes = ''; $childern =& $values; } if(empty($childern) && $tagName != 'script') { return '<' . $tagName . $attributes . '/>'; } else { return '<' . $tagName . $attributes . '>' . join((array)$childern) . '</' . $tagName . '>'; } }
function f_qsort($in) { if(!empty($in)) { return array_merge( f_qsort( f_filter( function($a) use($in) { return $a < f_first($in); }, f_rest($in) ) ), array( f_first($in) ), f_qsort( f_filter( function($a) use($in) { return $a >= f_first($in); }, f_rest($in) ) ) ); } else { return array(); } }
function regexArray($regexs) { $matches = array(); D::log($_SERVER['QUERY_STRING']); foreach ($regexs as $regex => $func) { preg_match_all($regex, $_SERVER['QUERY_STRING'], $matches); if (f_first($matches)) { return f_push(array($func), f_map('f_first', f_rest($matches))); } } return false; }
static function _buildWhere($group, $groupOperator='AND', $escape=true) { //"Bitch I'll pick the world up and I'ma drop it on your f*ckin' head" - Lil Wayne. $keys = array_keys($group); if(is_int(f_last($keys)) && is_string(f_last($group))) { $operator = f_last($group); $group = f_chop($group); } else { $operator = '='; } if(is_int(f_first($keys)) && is_string(f_first($group))) { $groupOperator = f_first($group); $group = f_rest($group); } $builtArray = f_keyMap( function($value, $key) use($groupOperator, $operator, $escape) { if(is_int($key) && is_array($value)) { $bWhere = Query::_buildWhere($value, $groupOperator, $escape); if(!empty($bWhere)) { return '(' . "\n\t" . $bWhere . "\n" .')'; } else { return null; } } if(is_string($key)) { static $escapeFunc = array('Query' , 'nullEscape'); if(!$escape) { $escapeFunc = 'nothing'; } else { $escapeFunc = f_callable($escapeFunc); } if(is_array($value)) { $key = $escapeFunc($key, ''); if(f_first(array_keys($value)) !== 0) { return join(' ' . $groupOperator . ' ', f_keyMap(function($v, $k) use($key, $escapeFunc) { return $key . ' BETWEEN ' . $escapeFunc($k) . ' AND ' . $escapeFunc($v); }, $value)); } return $key . ' IN (' . join(', ', array_map($escapeFunc, $value)) . ')'; } else { $value = call_user_func($escapeFunc, $value); if($value === 'null') { if($operator == '=') { $operator = 'IS'; } else { $operator = 'IS NOT'; } } return Query::escape($key) . ' ' . $operator . ' ' . $value; } } }, $group ); if(!empty($builtArray)) { return join(' ' . $groupOperator . ' ', array_filter($builtArray)); } }
private function regexArray($regexs) { $matches = array(); foreach($regexs as $regex => $func) { preg_match_all($regex, $this->request, $matches); if(f_first($matches)) { D::log($regex, 'regex'); return f_push( array($func), array_map('f_first', f_rest($matches)) ); } } return false; }
static function _buildWhere($group, $groupOperator='AND', $escape=true) { //"Bitch I'll pick the world up and I'ma drop it on your f*ckin' head" - Lil Wayne. $keys = array_keys($group); if(is_int(f_last($keys)) && is_string(f_last($group))) { $operator = f_last($group); $group = f_chop($group); } else { $operator = '='; } if(is_int(f_first($keys)) && is_string(f_first($group))) { $groupOperator = f_first($group); $group = f_rest($group); } $builtArray = f_keyMap( function($value, $key) use($groupOperator, $operator, $escape) { if(is_int($key) && is_array($value)) { //Group? @todo double check to make sure OR is working $bWhere = Query::_buildWhere($value, $groupOperator, $escape); if(!empty($bWhere)) { return '(' . "\n" . $bWhere . ')'; } else { return null; } } if(is_string($key)) { static $escapeFunc = 'Query::nullEscape'; if(!$escape) { $escapeFunc = 'nothing'; } //column if(is_array($value)) { //IN or group return Query::escape($key) . ' IN (' . join(', ', array_map($escapeFunc, $value)) . ')'; } else { $value = call_user_func($escapeFunc, $value); if($value === 'null') { if($operator == '=') { $operator = 'IS'; } else { $operator = 'IS NOT'; } } return Query::escape($key) . ' ' . $operator . ' ' . $value; } } }, $group ); // D::log($builtArray, 'built array'); if(!empty($builtArray)) { return join(' ' . $groupOperator . ' ', array_filter($builtArray)); } }