Skip to content

Commit

Permalink
update DocBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sroehrl committed Apr 29, 2019
1 parent acbfc0a commit 610975d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 31 deletions.
40 changes: 30 additions & 10 deletions DbCallFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}
Expand Down
26 changes: 22 additions & 4 deletions DbEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ class DbEnvironment {
*/
private $_db;

/**
* @var array
*/
private $envVariables;


/**
* DbEnvironment constructor.
*
Expand All @@ -41,7 +45,10 @@ function __construct() {
return $this;
}

function bindMysqli($mysqli) {
/**
* @param $mysqli
*/
public function bindMysqli($mysqli) {
$this->_db = $mysqli;
}

Expand All @@ -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];
}

Expand Down
44 changes: 34 additions & 10 deletions DbOps.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,34 @@ class DbOps {
*/
private static $_env;

/**
* DbOps constructor.
*
* @param $env
*/
function __construct($env) {
self::$_env = $env;
}

/**
* @return array
*/
public function getExclusions() {
protected function getExclusions() {
return self::$preparedExclusions;
}

/**
*
* clears Exclusions
*/
public function clearExclusions() {
protected function clearExclusions() {
self::$preparedExclusions = [];
}

/**
* @param $value
* @param $type
*/
public function addExclusion($value, $type = '') {
protected function addExclusion($value, $type = '') {
if ($type == '') {
$type = $this->mysqliStmtType($value);
}
Expand All @@ -56,19 +61,21 @@ 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
*
* @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');
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 '';
Expand All @@ -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){
Expand Down
64 changes: 57 additions & 7 deletions UuidHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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)){
Expand All @@ -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 . '"') . ')';
}

}

0 comments on commit 610975d

Please sign in to comment.