2016-10-12 22:12:25 -04:00
|
|
|
<?php declare(strict_types=1);
|
2012-10-31 20:21:15 +00:00
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
2016-09-07 13:17:17 -04:00
|
|
|
* SQL Query Builder / Database Abstraction Layer
|
2012-10-31 20:21:15 +00:00
|
|
|
*
|
2016-10-12 22:12:25 -04:00
|
|
|
* PHP version 7
|
2016-09-07 13:17:17 -04:00
|
|
|
*
|
2018-02-09 16:14:40 -05:00
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2016-10-12 22:12:25 -04:00
|
|
|
* @copyright 2012 - 2016 Timothy J. Warren
|
2018-02-09 16:14:40 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Query
|
2012-10-31 20:21:15 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
use Query\{
|
|
|
|
ConnectionManager,
|
|
|
|
QueryBuilderInterface
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Global functions that don't really fit anywhere else
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multibyte-safe trim function
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function mb_trim(string $string): string
|
|
|
|
{
|
|
|
|
return preg_replace('/(^\s+)|(\s+$)/u', '', $string);
|
|
|
|
}
|
2016-09-07 13:06:28 -04:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
/**
|
|
|
|
* Filter out db rows into one array
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @param mixed $index
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function dbFilter(array $array, $index): array
|
|
|
|
{
|
|
|
|
$newArray = [];
|
2012-11-08 14:28:49 -05:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
foreach ($array as $a)
|
|
|
|
{
|
|
|
|
$newArray[] = $a[$index];
|
|
|
|
}
|
2012-10-31 20:21:15 +00:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
return $newArray;
|
|
|
|
}
|
2012-10-31 20:21:15 +00:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
/**
|
|
|
|
* Zip a set of arrays together on common keys
|
|
|
|
*
|
|
|
|
* The $zipperInput array is an array of arrays indexed by their place in the output
|
|
|
|
* array.
|
|
|
|
*
|
|
|
|
* @param array $zipperInput
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function arrayZipper(array $zipperInput): array
|
2016-10-12 22:12:25 -04:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
$output = [];
|
2018-01-24 13:14:03 -05:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
foreach ($zipperInput as $appendKey => $values)
|
|
|
|
{
|
|
|
|
foreach ($values as $index => $value)
|
|
|
|
{
|
|
|
|
if ( ! isset($output[$index]))
|
|
|
|
{
|
|
|
|
$output[$index] = [];
|
|
|
|
}
|
|
|
|
$output[$index][$appendKey] = $value;
|
|
|
|
}
|
|
|
|
}
|
2016-10-12 22:12:25 -04:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
return $output;
|
|
|
|
}
|
2015-06-04 15:27:55 -04:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
/**
|
|
|
|
* Determine whether a value in the passed array matches the pattern
|
|
|
|
* passed
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @param string $pattern
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function regexInArray(array $array, string $pattern): bool
|
2018-01-24 13:14:03 -05:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
if (empty($array))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($array as $item)
|
2014-04-24 13:08:26 -04:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
if (is_scalar($item) && preg_match($pattern, $item))
|
2014-04-24 13:08:26 -04:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
return TRUE;
|
2014-04-24 13:08:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:14:03 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
2015-06-04 15:27:55 -04:00
|
|
|
|
2019-12-11 16:49:06 -05: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 QueryBuilderInterface|null
|
|
|
|
*/
|
|
|
|
function Query($params = ''): ?QueryBuilderInterface
|
2018-01-24 13:14:03 -05:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
if ($params === NULL)
|
2014-11-07 12:14:46 -05:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
return NULL;
|
2014-11-07 12:14:46 -05:00
|
|
|
}
|
2018-01-24 13:14:03 -05:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
$manager = ConnectionManager::getInstance();
|
2014-11-07 12:14:46 -05:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
// If you are getting a previously created connection
|
|
|
|
if (is_scalar($params))
|
|
|
|
{
|
|
|
|
return $manager->getConnection($params);
|
|
|
|
}
|
2014-06-09 17:02:14 -04:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
$paramsObject = (object)$params;
|
2018-01-19 16:48:52 -05:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
// Otherwise, return a new connection
|
|
|
|
return $manager->connect($paramsObject);
|
2018-01-24 13:14:03 -05:00
|
|
|
}
|
2015-06-04 15:27:55 -04:00
|
|
|
|
2012-11-08 14:28:49 -05:00
|
|
|
}
|
2018-01-19 16:48:52 -05:00
|
|
|
// End of common.php
|