misuzu/utility.php

120 lines
2.6 KiB
PHP
Raw Normal View History

<?php
// both of these are provided by illuminate/database already
// but i feel like it makes sense to add these definitions regardless
if (!function_exists('starts_with')) {
2017-12-16 22:51:36 +00:00
function starts_with(string $string, string $text): bool
{
return substr($string, 0, strlen($text)) === $text;
}
}
if (!function_exists('ends_with')) {
2017-12-16 22:51:36 +00:00
function ends_with(string $string, string $text): bool
{
return substr($string, 0 - strlen($text)) === $text;
}
}
function password_entropy(string $password): int
{
return count(count_chars(utf8_decode($password), 1)) * log(256, 2);
}
function check_mx_record(string $email): bool
{
$domain = substr(strstr($email, '@'), 1);
return checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A');
}
2017-12-16 22:51:36 +00:00
function dechex_pad(int $value, int $padding = 2): string
{
return str_pad(dechex($value), $padding, '0', STR_PAD_LEFT);
}
2017-12-16 22:51:36 +00:00
function array_rand_value(array $array, $fallback = null)
{
if (!$array) {
return $fallback;
2017-12-16 22:51:36 +00:00
}
return $array[array_rand($array)];
}
2017-12-16 22:51:36 +00:00
function has_flag(int $flags, int $flag): bool
{
return ($flags & $flag) > 0;
}
2017-12-16 21:44:45 +00:00
2018-01-03 01:12:28 +00:00
function byte_symbol($bytes, $decimal = false)
{
if ($bytes < 1) {
return "0 B";
}
$divider = $decimal ? 1000 : 1024;
$symbols = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
$exp = floor(log($bytes) / log($divider));
$bytes = $bytes / pow($divider, floor($exp));
$symbol = $symbols[$exp];
return sprintf("%.2f %s%sB", $bytes, $symbol, $symbol !== '' && !$decimal ? 'i' : '');
}
function get_country_code(string $ipAddr, string $fallback = 'XX'): string
{
if (function_exists("geoip_country_code_by_name")) {
try {
$code = geoip_country_code_by_name($ipAddr);
if ($code) {
return $code;
}
} catch (\Exception $e) {
}
}
return $fallback;
}
2017-12-16 22:51:36 +00:00
function is_int_ex($value, int $boundary_low, int $boundary_high): bool
{
2017-12-16 21:44:45 +00:00
return is_int($value) && $value >= $boundary_low && $value <= $boundary_high;
}
2017-12-16 22:51:36 +00:00
function is_sbyte($value): bool
{
2017-12-16 21:44:45 +00:00
return is_int_ex($value, -0x80, 0x7F);
}
2017-12-16 22:51:36 +00:00
function is_byte($value): bool
{
2017-12-16 21:44:45 +00:00
return is_int_ex($value, 0x0, 0xFF);
}
2017-12-16 22:51:36 +00:00
function is_int16($value): bool
{
2017-12-16 21:44:45 +00:00
return is_int_ex($value, -0x8000, 0x7FFF);
}
2017-12-16 22:51:36 +00:00
function is_uint16($value): bool
{
2017-12-16 21:44:45 +00:00
return is_int_ex($value, 0x0, 0xFFFF);
}
2017-12-16 22:51:36 +00:00
function is_int32($value): bool
{
2017-12-16 21:44:45 +00:00
return is_int_ex($value, -0x80000000, 0x7FFFFFFF);
}
2017-12-16 22:51:36 +00:00
function is_uint32($value): bool
{
2017-12-16 21:44:45 +00:00
return is_int_ex($value, 0x0, 0xFFFFFFFF);
}
2017-12-16 22:51:36 +00:00
function is_int64($value): bool
{
2017-12-16 21:44:45 +00:00
return is_int_ex($value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
}