Skip to content

Commit

Permalink
added array and header helper functions and split functions into sepa…
Browse files Browse the repository at this point in the history
…rate files
  • Loading branch information
enobrev committed Sep 1, 2016
1 parent d77fa62 commit fd42558
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 72 deletions.
43 changes: 43 additions & 0 deletions src/Arrays.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Enobrev;

/**
* @param array $aArray
* @param array ...$aKeys
* @return array
*/
function array_without_keys(array $aArray, ...$aKeys) {
$aDiff = [];
foreach($aKeys as $sKey) {
$aDiff[$sKey] = 1;
}

return array_diff_key($aArray, $aDiff);
}

/**
* @param array $aArray
* @return bool
*/
function array_is_multi(array $aArray) {
foreach ($aArray as $mValue) {
if (is_array($mValue)) return true;
}
return false;
}

/**
* @param string $sPath
* @param mixed $mValue
* @param string $sDelimiter
* @return array
*/
function array_from_path($sPath, $mValue, $sDelimiter = '.') {
$aPath = explode($sDelimiter, $sPath);
$aResponse = $mValue;
while(count($aPath) > 1) {
$aResponse = array(array_pop($aPath) => $aResponse);
}

return $aResponse;
}
74 changes: 2 additions & 72 deletions src/Tools.php → src/Debugging.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
<?php
namespace Enobrev;

/**
* @return bool
*/
function isCli() {
return php_sapi_name() == 'cli'
&& empty($_SERVER['REMOTE_ADDR']);
}

function _contentTypeIsNotHtml() {
$aHeaders = headers_list();
if (count($aHeaders)) {
foreach($aHeaders as $sHeader) {
$aHeader = explode(':', $sHeader);
if (strtolower($aHeader[0]) == 'content-type') {
return $aHeader[1] != 'text/html';
}
}
}

// text/html is default
return false;
}

/**
* @param $sMessage
*/
function _output($sMessage) {
if (isCli() || _contentTypeIsNotHtml()) {
if (isCli() || contentTypeIsNotHtml()) {
$sMessage = str_replace('<br />', "\n", $sMessage);
$sMessage = str_replace('<pre>', '', $sMessage);
$sMessage = str_replace('</pre>', '', $sMessage);
Expand Down Expand Up @@ -68,7 +45,7 @@ function dbg(...$aArgs) {
* @param bool $bReturn
* @return array|void
*/
function trace($bShort = true, $bReturn = false) {
function trace(bool $bShort = true, bool $bReturn = false) {
$oBacktrace = debug_backtrace();

if ($bShort) {
Expand Down Expand Up @@ -116,51 +93,4 @@ function trace($bShort = true, $bReturn = false) {
dbg($oBacktrace);
}
}
}

/**
* @return string
*/
function get_ip() {
if (isset($_SERVER["HTTP_X_REAL_IP"]) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$sIP = $_SERVER["HTTP_X_REAL_IP"]; // explicitly set in nginx load balancer
} else if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
$sIP = getenv("HTTP_CLIENT_IP");
} else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
$sIP = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
$sIP = getenv("REMOTE_ADDR");
} else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$sIP = $_SERVER['REMOTE_ADDR'];
} else {
$sIP = "unknown";
}

return $sIP;
}

/**
* @param string $word
* @return string
*/
function depluralize($word){
$rules = array(
'ss' => false,
'os' => 'o',
'ies' => 'y',
'xes' => 'x',
'oes' => 'o',
'ves' => 'f',
's' => ''
);

foreach(array_keys($rules) as $key){
if(substr($word, (strlen($key) * -1)) != $key)
continue;
if($key === false)
return $word;
return substr($word, 0, strlen($word) - strlen($key)) . $rules[$key];
}

return $word;
}
62 changes: 62 additions & 0 deletions src/Headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace Enobrev;

/**
* @return array
*/
function getHeaders() {
$aHeaders = [];
foreach ($_SERVER as $sName => $sValue) {
if (substr($sName, 0, 5) == 'HTTP_') {
$aHeaders[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($sName, 5)))))] = $sValue;
}
}
return $aHeaders;
}

/**
* @return bool
*/
function isCli() {
return php_sapi_name() == 'cli'
&& empty($_SERVER['REMOTE_ADDR']);
}

/**
* @return string
*/
function get_ip() {
if (isset($_SERVER["HTTP_X_REAL_IP"]) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$sIP = $_SERVER["HTTP_X_REAL_IP"]; // explicitly set in nginx load balancer
} else if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
$sIP = getenv("HTTP_CLIENT_IP");
} else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
$sIP = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
$sIP = getenv("REMOTE_ADDR");
} else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$sIP = $_SERVER['REMOTE_ADDR'];
} else {
$sIP = "unknown";
}

return $sIP;
}

/**
* @return bool
*/
function contentTypeIsNotHtml() {
$aHeaders = headers_list();
if (count($aHeaders)) {
foreach($aHeaders as $sHeader) {
$aHeader = explode(':', $sHeader);
if (strtolower($aHeader[0]) == 'content-type') {
return $aHeader[1] != 'text/html';
}
}
}

// text/html is default
return false;
}
28 changes: 28 additions & 0 deletions src/Strings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Enobrev;

/**
* @param string $word
* @return string
*/
function depluralize(string $word){
$rules = array(
'ss' => false,
'os' => 'o',
'ies' => 'y',
'xes' => 'x',
'oes' => 'o',
'ves' => 'f',
's' => ''
);

foreach(array_keys($rules) as $key){
if(substr($word, (strlen($key) * -1)) != $key)
continue;
if($key === false)
return $word;
return substr($word, 0, strlen($word) - strlen($key)) . $rules[$key];
}

return $word;
}

0 comments on commit fd42558

Please sign in to comment.