Skip to content

Commit

Permalink
add helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dunkrupp committed Mar 15, 2018
1 parent f7568c1 commit 5b0a50a
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

if (!function_exists('strip_scheme')) {
/**
* Strips the scheme off of the uri.
* @param string $string
* @return null|string
*/
Expand All @@ -15,6 +16,7 @@ function strip_scheme($string) {

if (!function_exists('is_domain')) {
/**
* Checks if string is a valid form of domain.
* @param $string
* @return false|int
*/
Expand All @@ -25,6 +27,7 @@ function is_domain($string) {

if (!function_exists('is_ip')) {
/**
* Checks if is valid form of IP.
* @param string $string
* @return bool
*/
Expand All @@ -35,6 +38,7 @@ function is_ip($string) {

if (!function_exists('is_ipv4')) {
/**
* Checks if string is valid IPv4.
* @param string $string
* @return bool
*/
Expand All @@ -45,6 +49,7 @@ function is_ipv4($string) {

if (!function_exists('is_ipv6')) {
/**
* Checks if string is valid IPv6.
* @param string $string
* @return bool
*/
Expand All @@ -53,10 +58,40 @@ function is_ipv6($string) {
}
}

function ipv6_numeric($ip) {
$binNum = '';
foreach (unpack('C*', inet_pton($ip)) as $byte) {
$binNum .= str_pad(decbin($byte), 8, "0", STR_PAD_LEFT);
if (!function_exists('is_json')) {
/**
* Note: Slim Request specific.
* Checks if is \Slim\Http\Request object and is Content-Type: application/json.
* @param $request
* @return bool
*/
function is_json($request) {
if ($request instanceof \Slim\Http\Request && $request->getContentType() === 'application/json') {
return true;
}
return false;
}
return base_convert(ltrim($binNum, '0'), 2, 10);
}
}

if (!function_exists('is_post')) {
/**
* @return bool
*/
function is_post() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
return true;
}
return false;
}
}

if (!function_exists('ipv6_numeric')) {
function ipv6_numeric($ip) {
$binNum = '';
foreach (unpack('C*', inet_pton($ip)) as $byte) {
$binNum .= str_pad(decbin($byte), 8, "0", STR_PAD_LEFT);
}

return base_convert(ltrim($binNum, '0'), 2, 10);
}
}

0 comments on commit 5b0a50a

Please sign in to comment.