thingui/helpers/global_functions.php
2022-09-23 08:46:47 -06:00

290 lines
6.9 KiB
PHP

<?php
/*
|--------------------------------------------------------------------------
| Global Functions
|--------------------------------------------------------------------------
|
| This is a home for functions that don't belong to any one class and
| that should be available anywhere in the application.
|
*/
if (! function_exists('snake2Title')) {
/**
* Convert a snake case string to a title with spaces
* and every word capitalized.
*
* @since 1.0.0
*
* @param string $stakeSlug A snake case string, commonly a slug
*
* @return string
*/
function snake2Title(string $snakeSlug): string
{
$output = preg_replace('/\_/', ' ', $snakeSlug);
return ucwords($output);
}
}
if (! function_exists('carbon')) {
/**
* Return a Carbon object based on a given timestring.
* It will attempt to find a timezone in the current
* session but default to UTC.
*
* @since 1.0.0
*
* @param string|null $timestring
*
* @return \Carbon\Carbon
*/
function carbon(?string $timestring = null): \Carbon\Carbon
{
$carbon = Carbon\Carbon::now(session('timezone_name'));
if (! empty($timestring)) {
$carbon = Carbon\Carbon::parse($timestring, session('timezone_name'));
}
return $carbon;
}
}
if (! function_exists('jddayofweek')) {
/**
* Returns the day of the week. Can return a string or an integer depending on the mode.
*
* @since 1.0.0
*
* @param int|null $intDay
* @param int $mode
*
* @return string
*/
function jddayofweek(?int $intDay = null, int $mode = 0): string
{
if (is_null($intDay)) {
$intDay = date('l');
}
if ($mode === 0) {
return $intDay;
}
return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][$intDay];
}
}
if (! function_exists('is_serialized')) {
/**
* Check a value to find if it was serialized.
*
* @since 1.0.0
*
* @param mixed $data
* @param bool $strict
*
* @return bool
*/
function is_serialized($data, bool $strict = true): bool
{
// If it isn't a string, it isn't serialized.
if (! is_string($data)) {
return false;
}
$data = trim($data);
if ('N;' === $data) {
return true;
}
if (strlen($data) < 4) {
return false;
}
if (':' !== $data[1]) {
return false;
}
if ($strict) {
$lastc = substr($data, -1);
if (';' !== $lastc && '}' !== $lastc) {
return false;
}
} else {
$semicolon = strpos($data, ';');
$brace = strpos($data, '}');
// Either ; or } must exist.
if (!$semicolon && !$brace) {
return false;
}
// But neither must be in the first X characters.
if ($semicolon && $semicolon < 3) {
return false;
}
if ($brace && $brace < 4) {
return false;
}
}
$token = $data[0];
switch ($token) {
case 's':
if ($strict) {
if ('"' !== substr($data, -2, 1)) {
return false;
}
} elseif (!strpos($data, '"')) {
return false;
}
// Or else fall through.
case 'a':
case 'O':
return (bool) preg_match("/^{$token}:[0-9]+:/s", $data);
case 'b':
case 'i':
case 'd':
$end = $strict ? '$' : '';
return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);
}
return false;
}
}
if (! function_exists('maybe_unserialize')) {
/**
* Unserialize data only if it was serialized. Will return
* an array if it was a serialized string, otherwise it
* will return whatever was passed into the function
* leaving it untouched.
*
* @since 1.0.0
*
* @param mixed $data
*
* @return mixed
*/
function maybe_unserialize($data)
{
if (is_serialized($data)) { // Don't attempt to unserialize data that wasn't serialized going in.
return @unserialize(trim($data));
}
return $data;
}
}
if (! function_exists('cel2Fah')) {
/**
* Convert from celsius to fahrenheit.
*
* @since 1.0.0
*
* @param float|int|string $celsius
* @param int $precision
*
* @return float
*/
function cel2Fah($celsius, int $preceision = 0): float
{
return round((($celsius * (9/5)) + 32), $preceision);
}
}
if (! function_exists('fah2Cel')) {
/**
* Convert from fahrenheit to celsius.
*
* @since 1.0.0
*
* @param float|int|string $fahrenheit
* @param int $precision
*
* @return float
*/
function fah2Cel($fahrenheit, int $preceision = 1): float
{
return round(($fahrenheit - 32 * (5/9)), $preceision);
}
}
if (! function_exists('meters2Miles')) {
/**
* Convert from meters to miles.
*
* @since 1.0.0
*
* @param float|int|string $meters
* @param int $precision
*
* @return float
*/
function meters2Miles($meters, int $preceision = 1): float
{
return round(($meters * 0.00062137), $preceision);
}
}
if (! function_exists('kilometers2Miles')) {
/**
* Convert from kilometers to meters.
*
* @since 1.0.0
*
* @param float|int|string $kilometers
* @param int $precision
*
* @return float
*/
function kilometers2Miles($kilometers, int $preceision = 1): float
{
return round(($kilometers * 1.609), $preceision);
}
}
if (! function_exists('m2Km')) {
/**
* Convert from meters to kilometers.
*
* @since 1.0.0
*
* @param float|int|string $meters
* @param int $precision
*
* @return float
*/
function m2Km($meters, int $preceision = 1): float
{
return round(($meters / 1000), $preceision);
}
}
if (! function_exists('mm2Inches')) {
/**
* Convert from milimeters to inches.
*
* @since 1.0.0
*
* @param float|int|string $milimeters
* @param int $precision
*
* @return float
*/
function mm2Inches($milimeters, int $preceision = 1): float
{
return round(($milimeters / 25.4), $preceision);
}
}
if (! function_exists('pa2Mbar')) {
/**
* Convert from pascals to milibars.
*
* @since 1.0.0
*
* @param float|int|string $pascals
* @param int $precision
*
* @return float
*/
function pa2Mbar($pascals, int $preceision = 1): float
{
return round(($pascals / 100), $preceision);
}
}