Query/src/common.php

142 lines
2.7 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
*
2020-04-10 20:54:31 -04:00
* PHP version 7.4
2016-09-07 13:17:17 -04:00
*
2019-12-11 16:49:42 -05:00
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2020-03-18 11:31:56 -04:00
* @copyright 2012 - 2020 Timothy J. Warren
2019-12-11 16:49:42 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat/Query
* @version 3.0.0
2012-10-31 16:21:15 -04:00
*/
2019-12-11 16:49:06 -05:00
namespace {
use Query\ConnectionManager;
use Query\QueryBuilderInterface;
2019-12-11 16:49:06 -05:00
/**
* 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);
}
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 = [];
2019-12-11 16:49:06 -05:00
foreach ($array as $a)
{
$newArray[] = $a[$index];
}
2012-10-31 16:21:15 -04:00
2019-12-11 16:49:06 -05:00
return $newArray;
}
2012-10-31 16:21:15 -04: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;
}
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)
{
2019-12-11 16:49:06 -05:00
if (is_scalar($item) && preg_match($pattern, $item))
{
2019-12-11 16:49:06 -05:00
return TRUE;
}
}
2018-01-24 13:14:03 -05:00
return FALSE;
}
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)
{
2019-12-11 16:49:06 -05:00
return NULL;
}
2018-01-24 13:14:03 -05:00
2019-12-11 16:49:06 -05:00
$manager = ConnectionManager::getInstance();
2019-12-11 16:49:06 -05:00
// If you are getting a previously created connection
if (is_scalar($params))
{
return $manager->getConnection($params);
}
2019-12-11 16:49:06 -05:00
$paramsObject = (object)$params;
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
}
}
// End of common.php