-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added array and header helper functions and split functions into sepa…
…rate files
- Loading branch information
Showing
4 changed files
with
135 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |