| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * SQL Query Builder / Database Abstraction Layer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * PHP version 7 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package     Query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author      Timothy J. Warren <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright   2012 - 2016 Timothy J. Warren | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license     http://www.opensource.org/licenses/mit-license.html  MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @link        https://git.timshomepage.net/aviat4ion/Query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Query\{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     ConnectionManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     QueryBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | // -------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Global functions that don't really fit anywhere else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * Multibyte-safe trim function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | function mb_trim(string $string): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	return preg_replace('/(^\s+)|(\s+$)/u', '', $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | // -------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * Filter out db rows into one array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  * @param array $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * @param mixed $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | function dbFilter(array $array, $index): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	$newArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	foreach($array as $a) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$newArray[] = $a[$index]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	return $newArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | // -------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  * Zip a set of arrays together on common keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  * The $zipperInput array is an array of arrays indexed by their place in the output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  * array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  * @param array $zipperInput | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | function arrayZipper(array $zipperInput): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	$output = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	foreach($zipperInput as $appendKey => $values) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		foreach($values as $index => $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			if ( ! isset($output[$index])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				$output[$index] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			$output[$index][$appendKey] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	return $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | // -------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  * Determine whether a value in the passed array matches the pattern | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  * passed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  * @param array $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  * @param string $pattern | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | function regexInArray(array $array, string $pattern): bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 	if (empty($array)) | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 		return FALSE; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 	foreach($array as $item) | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 		if (is_scalar($item) && preg_match($pattern, $item)) | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 			return TRUE; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 	return FALSE; | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  | } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | // -------------------------------------------------------------------------- | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  * Connection function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  * Send an array or object as connection parameters to create a connection. If | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  * the array or object has an 'alias' parameter, passing that string to this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  * function will return that connection. Passing no parameters returns the last | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  * connection created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  * @param string|object|array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  * @return QueryBuilderInterface|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | function Query($params = ''): ?QueryBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	if ($params === NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		return NULL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	$manager = ConnectionManager::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	// If you are getting a previously created connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	if (is_scalar($params)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		return $manager->getConnection($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	$paramsObject = (object) $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	// Otherwise, return a new connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	return $manager->connect($paramsObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  | // End of common.php | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |  |