From 610975d53b15355ccdc5dc2668266c22a965d685 Mon Sep 17 00:00:00 2001 From: sroehrl Date: Mon, 29 Apr 2019 18:19:03 -0400 Subject: [PATCH] update DocBlocks --- DbCallFunctions.php | 40 +++++++++++++++++++++------- DbEnvironment.php | 26 +++++++++++++++--- DbOps.php | 44 ++++++++++++++++++++++++------- UuidHandler.php | 64 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 143 insertions(+), 31 deletions(-) diff --git a/DbCallFunctions.php b/DbCallFunctions.php index 891692d..8a1a492 100644 --- a/DbCallFunctions.php +++ b/DbCallFunctions.php @@ -9,30 +9,47 @@ namespace Neoan3\Apps; +use Countable; + /** * Class DbCallFunctions + * * @package Neoan3\Apps */ class DbCallFunctions { /** * @param $array + * * @return string */ - static function calls($array){ - if(is_countable($array)&&count($array) > 1) { + static function calls($array) { + $return = ''; + if(is_countable($array) && count($array) > 1) { array_shift($array); foreach($array as $key => $value) { $func = $key; - return self::$func($value); + $return .= self::$func($value); } } - return ''; + return $return; } + + /** + * @param $function + * @param $arguments + * + * @return mixed + */ + static function call($function, $arguments) { + return self::$function($arguments); + } + /** * @param $array + * * @return string */ - static function orderBy($array) { + private static function orderBy($array) { $origin = $array; if(count($array) > 1) { self::calls(array_shift($array)); @@ -42,30 +59,33 @@ static function orderBy($array) { /** * @param $array + * * @return string */ - static function groupBy($array) { + private static function groupBy($array) { $origin = $array; if(count($array) > 1) { self::calls(array_shift($array)); } - return ' GROUP BY ' . $origin[0] . (intval($origin[1])>0?', ' . $origin[1]:''); + return ' GROUP BY ' . $origin[0] . (intval($origin[1]) > 0 ? ', ' . $origin[1] : ''); } /** * @param $array + * * @return string */ - static function limit($array) { + private static function limit($array) { $origin = $array; if(count($array) > 1) { self::calls(array_shift($array)); } - return ' LIMIT ' . $origin[0] . (intval($origin[1])>0?', ' . $origin[1]:''); + return ' LIMIT ' . $origin[0] . (intval($origin[1]) > 0 ? ', ' . $origin[1] : ''); } } + // Polyfill for PHP<7.3 -if (!function_exists('is_countable')) { +if(!function_exists('is_countable')) { function is_countable($var) { return (is_array($var) || $var instanceof Countable); } diff --git a/DbEnvironment.php b/DbEnvironment.php index 3dcabfb..f692451 100644 --- a/DbEnvironment.php +++ b/DbEnvironment.php @@ -17,8 +17,12 @@ class DbEnvironment { */ private $_db; + /** + * @var array + */ private $envVariables; + /** * DbEnvironment constructor. * @@ -41,7 +45,10 @@ function __construct() { return $this; } - function bindMysqli($mysqli) { + /** + * @param $mysqli + */ + public function bindMysqli($mysqli) { $this->_db = $mysqli; } @@ -50,16 +57,27 @@ function bindMysqli($mysqli) { * * @return object */ - function setCharset($charset) { + public function setCharset($charset) { $this->_db->set_charset($charset); return $this->_db->get_charset(); } - function set($property, $value) { + /** + * @param $property + * @param $value + */ + public function set($property, $value) { $this->envVariables['db_' . $property] = $value; } - function get($var) { + /** + * Get environment variable + * + * @param $var + * + * @return mixed + */ + public function get($var) { return $this->envVariables['db_' . $var]; } diff --git a/DbOps.php b/DbOps.php index 94217b1..0a3b891 100644 --- a/DbOps.php +++ b/DbOps.php @@ -22,6 +22,11 @@ class DbOps { */ private static $_env; + /** + * DbOps constructor. + * + * @param $env + */ function __construct($env) { self::$_env = $env; } @@ -29,14 +34,14 @@ function __construct($env) { /** * @return array */ - public function getExclusions() { + protected function getExclusions() { return self::$preparedExclusions; } /** - * + * clears Exclusions */ - public function clearExclusions() { + protected function clearExclusions() { self::$preparedExclusions = []; } @@ -44,7 +49,7 @@ public function clearExclusions() { * @param $value * @param $type */ - public function addExclusion($value, $type = '') { + protected function addExclusion($value, $type = '') { if ($type == '') { $type = $this->mysqliStmtType($value); } @@ -56,11 +61,13 @@ public function addExclusion($value, $type = '') { * @param $type * @return array */ - public function prepareBinding($value, $type) { + private function prepareBinding($value, $type) { return ['type'=>$type,'value'=>$value]; } /** + * EASY markup interpretation to influence conditions-behavior + * * @param $string * @param bool $set * @param bool $prepared @@ -68,7 +75,7 @@ public function prepareBinding($value, $type) { * @return array|bool|string * @throws DbException */ - public function operandi($string, $set = false, $prepared = false) { + protected function operandi($string, $set = false, $prepared = false) { if(empty($string) && $string !== "0") { return ($set ? ' = NULL' : ' IS NULL'); } @@ -127,10 +134,13 @@ public function operandi($string, $set = false, $prepared = false) { } /** + * EASY markup interpretation to influence select-behavior + * * @param $string + * * @return string */ - public function selectandi($string) { + protected function selectandi($string) { $firstLetter = strtolower(substr($string, 0, 1)); $rest = substr($string, 1); switch($firstLetter) { @@ -146,11 +156,25 @@ public function selectandi($string) { return $return . $this->checkAs($string); } + /** + * Shorthand for sanitizing and adding backticks + * + * @param $string + * + * @return string + */ private function _sanitizeAndAddBackticks($string) { return $this->addBackticks(Db::sanitizeKey($string)); } - public function addBackticks($string) { + /** + * Adds backticks to keys, tables & columns + * + * @param $string + * + * @return string + */ + protected function addBackticks($string) { $parts = explode('.', $string); $result = ''; foreach($parts as $i => $part) { @@ -167,7 +191,7 @@ public function addBackticks($string) { * @param $rest * @return string */ - public function checkAs($rest) { + protected function checkAs($rest) { if(empty($rest) || $rest == '' || strpos($rest,'*')!== false){ // catch asterisk-selector return ''; @@ -188,7 +212,7 @@ public function checkAs($rest) { * @param $rest * @return mixed */ - public function cleanAs($rest) { + protected function cleanAs($rest) { $as = explode(':',$rest); $als = explode('.',$rest); if(count($as)>1){ diff --git a/UuidHandler.php b/UuidHandler.php index ba69844..5f324a6 100644 --- a/UuidHandler.php +++ b/UuidHandler.php @@ -8,29 +8,46 @@ namespace Neoan3\Apps; -use mysqli_stmt; +/** + * Class UuidHandler + * + * @package Neoan3\Apps + */ class UuidHandler { + /** + * @var + */ public $uuid; + /** + * @var DbOps + */ + static $_ops; /** * UuidHandler constructor. + * + * @param DbOps $ops + * * @throws DbException */ - function __construct() { + function __construct($ops) { + self::$_ops = $ops; try { if (!$id = $this->newUuid()) { throw new DbException(); } } catch (DbException $e) { - DbOps::formatError(['connection'], 'Cannot create UUID: Connection failed.'); + self::$_ops->formatError(['connection'], 'Cannot create UUID: Connection failed.'); } finally { return $this->newUuid(); } } /** + * Generates new UUID + * * @return $this * @throws DbException */ @@ -43,6 +60,13 @@ public function newUuid(){ return $this; } + /** + * handles binary to hex conversion + * + * @param $resultArray + * + * @return mixed + */ public function convertBinaryResults($resultArray){ foreach ($resultArray as $i => $item){ if(is_numeric($i)){ @@ -56,26 +80,52 @@ public function convertBinaryResults($resultArray){ return $resultArray; } + /** + * Converts short UUIDs to RFC 4122 conform format + * + * @param bool $uuid + * + * @return bool|mixed + */ public function convertToCompliantUuid($uuid=false){ $id = ($uuid?$uuid:$this->uuid); - $arr = [8,13,18,23]; + $arr = [8, 13, 18, 23]; foreach ($arr as $part){ $id = substr_replace($id, '-', $part, 0); } return $id; } + /** + * Converts to binary + * + * @param bool $uuid + * + * @return string + */ public function insertAsBinary($uuid=false){ - return '{ = '.$this->unhexUuid($uuid).' }'; + return '{ = ' . $this->unhexUuid($uuid) . ' }'; } + /** + * converts from binary to hex + * + * @param bool $uuid + * + * @return string + */ private function unhexUuid($uuid=false){ - return 'UNHEX("'.($uuid?$uuid:$this->uuid).'")'; + return 'UNHEX("' . ($uuid?$uuid:$this->uuid) . '")'; } + /** + * @param bool $newUuid + * + * @return string + */ private function hexUuid($newUuid=false){ - return 'HEX('.($newUuid?'UUID()':'"'.$this->uuid.'"').')'; + return 'HEX(' . ($newUuid?'UUID()': '"' . $this->uuid . '"') . ')'; } }