Query/src/common.php

142 lines
2.6 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
*/
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
*/
2018-01-24 13:14:03 -05:00
/**
* Multibyte-safe trim function
*
* @param string $string
* @return string
*/
function mb_trim(string $string): string
2012-10-31 16:21:15 -04:00
{
2018-01-24 13:14:03 -05:00
return preg_replace('/(^\s+)|(\s+$)/u', '', $string);
2012-10-31 16:21:15 -04:00
}
2018-01-24 13:14:03 -05:00
/**
* Filter out db rows into one array
*
* @param array $array
* @param mixed $index
* @return array
*/
function dbFilter(array $array, $index): array
2012-10-31 16:21:15 -04:00
{
2018-01-24 13:14:03 -05:00
$newArray = [];
2012-10-31 16:21:15 -04:00
2018-01-24 13:14:03 -05:00
foreach($array as $a)
2016-10-12 22:12:25 -04:00
{
2018-01-24 13:14:03 -05:00
$newArray[] = $a[$index];
2016-10-12 22:12:25 -04:00
}
2018-01-24 13:14:03 -05:00
return $newArray;
2016-10-12 22:12:25 -04:00
}
2018-01-24 13:14:03 -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
{
2018-01-24 13:14:03 -05:00
$output = [];
2018-01-24 13:14:03 -05:00
foreach($zipperInput as $appendKey => $values)
{
foreach($values as $index => $value)
{
2018-01-24 13:14:03 -05:00
if ( ! isset($output[$index]))
{
2018-01-24 13:14:03 -05:00
$output[$index] = [];
}
2018-01-24 13:14:03 -05:00
$output[$index][$appendKey] = $value;
}
}
2018-01-24 13:14:03 -05:00
return $output;
}
2018-01-24 13:14:03 -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
if (empty($array))
{
2018-01-24 13:14:03 -05:00
return FALSE;
}
2018-01-24 13:14:03 -05:00
foreach($array as $item)
{
if (is_scalar($item) && preg_match($pattern, $item))
{
2018-01-24 13:14:03 -05:00
return TRUE;
}
}
2018-01-24 13:14:03 -05:00
return FALSE;
}
2018-01-24 13:14:03 -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
if ($params === NULL)
2014-03-31 12:58:43 -04:00
{
2018-01-24 13:14:03 -05:00
return NULL;
}
2018-01-24 13:14:03 -05:00
$manager = ConnectionManager::getInstance();
2018-01-24 13:14:03 -05:00
// If you are getting a previously created connection
if (is_scalar($params))
{
return $manager->getConnection($params);
}
2018-01-24 13:14:03 -05:00
$paramsObject = (object) $params;
2018-01-24 13:14:03 -05:00
// Otherwise, return a new connection
return $manager->connect($paramsObject);
}
2018-01-24 13:14:03 -05:00
// End of common.php