Query/src/common.php

190 lines
3.8 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
2012-10-31 16:21:15 -04:00
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
2012-10-31 16:21:15 -04:00
*
2016-10-12 22:12:25 -04:00
* PHP version 7
2016-09-07 13:17:17 -04:00
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2016-10-12 22:12:25 -04:00
* @copyright 2012 - 2016 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
2012-10-31 16:21:15 -04:00
*/
2016-10-12 22:12:25 -04:00
use Query\{
ConnectionManager,
QueryBuilderInterface
};
2012-10-31 16:21:15 -04:00
// --------------------------------------------------------------------------
/**
* Global functions that don't really fit anywhere else
2012-10-31 16:21:15 -04:00
*/
2012-10-31 16:21:15 -04:00
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
*/
2016-10-12 22:12:25 -04:00
function mb_trim(string $string): string
2012-10-31 16:21:15 -04:00
{
return preg_replace('/(^\s+)|(\s+$)/u', '', $string);
2012-10-31 16:21:15 -04:00
}
}
// --------------------------------------------------------------------------
if ( ! function_exists('db_filter'))
2012-10-31 16:21:15 -04:00
{
/**
* Filter out db rows into one array
*
* @param array $array
* @param mixed $index
* @return array
*/
2016-10-12 22:12:25 -04:00
function db_filter(array $array, $index): array
2012-10-31 16:21:15 -04:00
{
2016-10-13 21:55:23 -04:00
$newArray = [];
2012-10-31 16:21:15 -04:00
foreach($array as $a)
{
2016-10-13 21:55:23 -04:00
$newArray[] = $a[$index];
}
2016-10-13 21:55:23 -04:00
return $newArray;
}
}
2016-10-12 22:12:25 -04:00
if ( ! function_exists('to_camel_case'))
{
/**
* Create a camelCase string from snake_case
*
2016-10-13 21:55:23 -04:00
* @param string $snakeCase
2016-10-12 22:12:25 -04:00
* @return string
*/
2016-10-13 21:55:23 -04:00
function to_camel_case(string $snakeCase): string
2016-10-12 22:12:25 -04:00
{
2016-10-13 21:55:23 -04:00
$pieces = explode('_', $snakeCase);
$numPieces = count($pieces);
2016-10-12 22:12:25 -04:00
$pieces[0] = mb_strtolower($pieces[0]);
for($i = 1; $i < $numPieces; $i++)
2016-10-12 22:12:25 -04:00
{
$pieces[$i] = ucfirst(mb_strtolower($pieces[$i]));
}
return implode('', $pieces);
}
}
// --------------------------------------------------------------------------
if ( ! function_exists('array_zipper'))
{
/**
* Zip a set of arrays together on common keys
*
2016-10-13 21:55:23 -04:00
* The $zipperInput array is an array of arrays indexed by their place in the output
* array.
*
2016-10-13 21:55:23 -04:00
* @param array $zipperInput
* @return array
*/
2016-10-13 21:55:23 -04:00
function array_zipper(array $zipperInput): array
{
$output = [];
2016-10-13 21:55:23 -04:00
foreach($zipperInput as $appendKey => $values)
{
foreach($values as $index => $value)
{
if ( ! isset($output[$index]))
{
$output[$index] = [];
}
2016-10-13 21:55:23 -04:00
$output[$index][$appendKey] = $value;
}
}
return $output;
}
}
// --------------------------------------------------------------------------
if ( ! function_exists('regex_in_array'))
{
/**
* Determine whether a value in the passed array matches the pattern
* passed
*
* @param array $array
* @param string $pattern
* @return bool
*/
2016-10-12 22:12:25 -04:00
function regex_in_array(array $array, string $pattern): bool
{
2015-11-11 09:25:21 -05:00
if (empty($array))
{
return FALSE;
}
foreach($array as $item)
{
2015-11-10 11:10:15 -05:00
if (is_scalar($item) && preg_match($pattern, $item))
{
2015-11-10 11:10:15 -05:00
return TRUE;
}
}
return FALSE;
}
}
// --------------------------------------------------------------------------
if ( ! function_exists('Query'))
{
/**
* 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 QueryBuilderInterface|null
*/
function Query($params = ''): ?QueryBuilderInterface
2014-03-31 12:58:43 -04:00
{
2016-10-13 21:55:23 -04:00
$manager = ConnectionManager::getInstance();
if ($params === NULL)
{
return NULL;
}
// If you are getting a previously created connection
if (is_scalar($params))
{
2016-10-13 21:55:23 -04:00
return $manager->getConnection($params);
}
$paramsObject = (object) $params;
// Otherwise, return a new connection
return $manager->connect($paramsObject);
2014-03-31 12:58:43 -04:00
}
}
// End of common.php