2012-10-31 16:21:15 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
|
|
|
* @package Query
|
2014-04-01 14:54:38 -04:00
|
|
|
* @subpackage Core
|
2012-10-31 16:21:15 -04:00
|
|
|
* @author Timothy J. Warren
|
2014-01-02 12:36:50 -05:00
|
|
|
* @copyright Copyright (c) 2012 - 2014
|
2012-10-31 16:21:15 -04:00
|
|
|
* @link https://github.com/aviat4ion/Query
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2014-03-31 12:47:07 -04:00
|
|
|
* Global functions that don't really fit anywhere else
|
2012-10-31 16:21:15 -04:00
|
|
|
*/
|
2012-11-08 14:28:49 -05:00
|
|
|
|
2012-10-31 16:21:15 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if ( ! function_exists('do_include'))
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bulk directory loading workaround for use
|
|
|
|
* with array_map and glob
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function do_include($path)
|
|
|
|
{
|
|
|
|
require_once($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if ( ! function_exists('mb_trim'))
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Multibyte-safe trim function
|
|
|
|
*
|
2014-02-07 16:53:01 -05:00
|
|
|
* @param string $string
|
2012-10-31 16:21:15 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function mb_trim($string)
|
|
|
|
{
|
|
|
|
return preg_replace("/(^\s+)|(\s+$)/us", "", $string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
if ( ! function_exists('db_filter'))
|
2012-10-31 16:21:15 -04:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
/**
|
|
|
|
* Filter out db rows into one array
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @param mixed $index
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function db_filter($array, $index)
|
2012-10-31 16:21:15 -04:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
$new_array = array();
|
2012-10-31 16:21:15 -04:00
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
foreach($array as $a)
|
|
|
|
{
|
|
|
|
$new_array[] = $a[$index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $new_array;
|
|
|
|
}
|
2012-10-31 16:21:15 -04:00
|
|
|
}
|
|
|
|
|
2012-11-08 20:02:31 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
if ( ! function_exists('from_camel_case'))
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a snake_case string from camelCase
|
|
|
|
*
|
|
|
|
* @see http://stackoverflow.com/questions/1993721/how-to-convert-camelcase-to-camel-case
|
|
|
|
*
|
|
|
|
* @param string $input
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function from_camel_case($input) {
|
|
|
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
|
|
|
|
$ret = $matches[0];
|
|
|
|
foreach ($ret as &$match) {
|
|
|
|
$match = strtolower($match);// == strtoupper($match) ? strtolower($match) : lcfirst($match);
|
|
|
|
}
|
|
|
|
return implode('_', $ret);
|
2014-04-24 14:50:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
if ( ! function_exists('array_zipper'))
|
2014-04-24 13:08:26 -04:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
/**
|
|
|
|
* Zip a set of arrays together on common keys
|
|
|
|
*
|
|
|
|
* The $zipper_input array is an array of arrays indexed by their place in the output
|
|
|
|
* array.
|
|
|
|
*
|
|
|
|
* @param array $zipper_input
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function array_zipper(Array $zipper_input)
|
2014-04-24 13:08:26 -04:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
$output = array();
|
|
|
|
|
|
|
|
foreach($zipper_input as $append_key => $values)
|
2014-04-24 13:08:26 -04:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
foreach($values as $index => $value)
|
2014-04-24 13:08:26 -04:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
if ( ! isset($output[$index]))
|
|
|
|
{
|
|
|
|
$output[$index] = array();
|
|
|
|
}
|
|
|
|
$output[$index][$append_key] = $value;
|
2014-04-24 13:08:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
return $output;
|
|
|
|
}
|
2014-04-24 13:08:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
if ( ! function_exists('array_pluck'))
|
2014-11-07 12:14:46 -05:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
/**
|
|
|
|
* Get an array out of an multi-dimensional array based on a common
|
|
|
|
* key
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @param string $key
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function array_pluck(Array $array, $key)
|
|
|
|
{
|
|
|
|
$output = array();
|
2014-11-07 12:14:46 -05:00
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
// No point iterating over an empty array
|
|
|
|
if (empty($array)) return $array;
|
2014-11-07 12:14:46 -05:00
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
foreach($array as $inner_array)
|
|
|
|
{
|
|
|
|
if (array_key_exists($key, $inner_array))
|
|
|
|
{
|
|
|
|
$output[] = $inner_array[$key];
|
|
|
|
}
|
|
|
|
}
|
2014-11-07 12:14:46 -05:00
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
return $output;
|
|
|
|
}
|
2014-11-07 12:14:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
if ( ! function_exists('regex_in_array'))
|
2014-11-07 12:14:46 -05:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
/**
|
|
|
|
* Determine whether a value in the passed array matches the pattern
|
|
|
|
* passed
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @param string $pattern
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function regex_in_array(Array $array, $pattern)
|
2014-11-07 12:14:46 -05:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
if (empty($array)) return FALSE;
|
|
|
|
|
|
|
|
foreach($array as $item)
|
2014-11-07 12:14:46 -05:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
if (is_scalar($item))
|
|
|
|
{
|
|
|
|
if (preg_match($pattern, $item)) return TRUE;
|
|
|
|
}
|
2014-11-07 12:14:46 -05:00
|
|
|
}
|
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-11-07 12:14:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
if ( ! function_exists('Query'))
|
2012-11-08 14:28:49 -05:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
/**
|
|
|
|
* Connection function
|
|
|
|
*
|
|
|
|
* Send an array or object as connection parameters to create a connection. If
|
|
|
|
* the array or object has an 'alias' parameter, passing that string to this
|
|
|
|
* function will return that connection. Passing no parameters returns the last
|
|
|
|
* connection created.
|
|
|
|
*
|
|
|
|
* @param string|object|array $params
|
|
|
|
* @return Query\Query_Builder|null
|
|
|
|
*/
|
|
|
|
function Query($params = '')
|
2014-03-31 12:58:43 -04:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
$cmanager = \Query\Connection_Manager::get_instance();
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2015-06-04 15:27:55 -04:00
|
|
|
// If you are getting a previously created connection
|
|
|
|
if (is_scalar($params))
|
2014-06-09 17:02:14 -04:00
|
|
|
{
|
2015-06-04 15:27:55 -04:00
|
|
|
return $cmanager->get_connection($params);
|
2014-06-09 17:02:14 -04:00
|
|
|
}
|
2015-06-04 15:27:55 -04:00
|
|
|
elseif ( ! is_scalar($params) && ! is_null($params))
|
|
|
|
{
|
2015-07-20 15:24:21 -04:00
|
|
|
$params_object = (object) $params;
|
2015-06-04 15:27:55 -04:00
|
|
|
|
|
|
|
// Otherwise, return a new connection
|
|
|
|
return $cmanager->connect($params_object);
|
|
|
|
}
|
2014-03-31 12:58:43 -04:00
|
|
|
}
|
2012-11-08 14:28:49 -05:00
|
|
|
}
|
2014-02-07 16:53:01 -05:00
|
|
|
// End of common.php
|