Query/apiDocumentation/files/src/common.php.txt

122 lines
2.3 KiB
Plaintext
Raw Normal View History

2023-01-20 11:30:51 -05:00
<?php declare(strict_types=1);
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
* PHP version 8.1
*
* @package Query
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
2023-03-17 16:28:07 -04:00
use Query\{ConnectionManager, QueryBuilderInterface};
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
/**
* Global functions that don't really fit anywhere else
*/
/**
* Multibyte-safe trim function
*/
function mb_trim(string $string): string
{
return preg_replace('/(^\s+)|(\s+$)/u', '', $string);
}
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
/**
* Filter out db rows into one array
*/
function dbFilter(array $array, mixed $index): array
{
$newArray = [];
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
foreach ($array as $a)
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
$newArray[] = $a[$index];
}
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
return $newArray;
}
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04: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.
*/
function arrayZipper(array $zipperInput): array
{
$output = [];
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
foreach ($zipperInput as $appendKey => $values)
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
foreach ($values as $index => $value)
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
if ( ! isset($output[$index]))
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
$output[$index] = [];
2023-01-20 11:30:51 -05:00
}
2023-03-17 16:28:07 -04:00
$output[$index][$appendKey] = $value;
2023-01-20 11:30:51 -05:00
}
2023-03-17 16:28:07 -04:00
}
return $output;
}
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
/**
* Determine whether a value in the passed array matches the pattern
* passed
*/
function regexInArray(array $array, string $pattern): bool
{
if (empty($array))
{
2023-01-20 11:30:51 -05:00
return FALSE;
}
2023-03-17 16:28:07 -04:00
foreach ($array as $item)
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
if (is_scalar($item) && preg_match($pattern, (string) $item))
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
return TRUE;
2023-01-20 11:30:51 -05:00
}
2023-03-17 16:28:07 -04:00
}
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
return FALSE;
}
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -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.
*/
function Query(string|object|array|null $params = ''): ?QueryBuilderInterface
{
if ($params === NULL)
{
return NULL;
}
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
$manager = ConnectionManager::getInstance();
2023-01-20 11:30:51 -05:00
2023-03-17 16:28:07 -04:00
// If you are getting a previously created connection
if (is_string($params))
{
return $manager->getConnection($params);
2023-01-20 11:30:51 -05:00
}
2023-03-17 16:28:07 -04:00
$paramsObject = (object) $params;
// Otherwise, return a new connection
return $manager->connect($paramsObject);
2023-01-20 11:30:51 -05:00
}
2023-03-17 16:28:07 -04:00
2023-01-20 11:30:51 -05:00
// End of common.php