Files
web/app/helpers.php

225 lines
4.5 KiB
PHP

<?php
/*
* Copyright (c) 2025, Антон Аксенов
* This file is part of iptv.axenov.dev web interface
* MIT License: https://git.axenov.dev/IPTV/web/src/branch/master/LICENSE
*/
declare(strict_types=1);
use App\Core\IniFile;
use App\Core\Kernel;
use Slim\App;
/**
* Returns path to root application directory
*
* @param string $path
* @return string
*/
function root_path(string $path = ''): string
{
return rtrim(sprintf('%s/%s', dirname($_SERVER['DOCUMENT_ROOT']), $path), '/');
}
/**
* Return path to application configuration directory
*
* @param string $path
* @return string
*/
function config_path(string $path = ''): string
{
return root_path("config/$path");
}
/**
* Returns path to app cache
*
* @param string $path
* @return string
*/
function cache_path(string $path = ''): string
{
return root_path("cache/$path");
}
/**
* Returns path to app views
*
* @param string $path
* @return string
*/
function views_path(string $path = ''): string
{
return root_path("views/$path");
}
/**
* Returns base URL
*
* @param string $route
* @return string
*/
function base_url(string $route = ''): string
{
return rtrim(sprintf('%s/%s', env('APP_URL'), $route), '/');
}
/**
* Returns value of environment var
*
* @param string $key
* @param mixed|null $default
* @return mixed
*/
function env(string $key, mixed $default = null): mixed
{
return $_ENV[$key] ?? $_SERVER[$key] ?? $default;
}
/**
* Returns kernel object
*
* @return Kernel
*/
function kernel(): Kernel
{
return Kernel::instance();
}
/**
* Returns app object
*
* @return App
*/
function app(): App
{
return Kernel::instance()->app();
}
/**
* Get config values
*
* @param string $key
* @param mixed|null $default
* @return mixed
*/
function config(string $key, mixed $default = null): mixed
{
return Kernel::instance()->config($key, $default);
}
/**
* Get Redis instance
*
* @return Redis
*/
function redis(): Redis
{
return Kernel::instance()->redis();
}
/**
* Returns any value as boolean
*
* @param mixed $value
* @return bool
*/
function bool(mixed $value): bool
{
is_string($value) && $value = strtolower(trim($value));
$positives = [true, 1, '1', '+', 'yes', 'on', 'true', 'enable', 'enabled'];
if (in_array($value, $positives, true)) {
return true;
}
$negatives = [false, 0, '0', '-', 'no', 'off', 'false', 'disable', 'disabled'];
if (in_array($value, $negatives, true)) {
return false;
}
return (bool)$value;
}
/**
* Проверяет значениен на пустоту
*
* @param $value
* @return bool
*/
function is_blank($value): bool
{
if (is_null($value)) {
return true;
}
if (is_string($value)) {
return trim($value) === '';
}
if (is_numeric($value) || is_bool($value)) {
return false;
}
if ($value instanceof Countable) {
return count($value) === 0;
}
return empty($value);
}
/**
* Возвращает натуральное представление значения переменной или null
*
* @param mixed $value
* @return int|null
*/
function int(mixed $value): ?int
{
if (is_blank($value)) {
return null;
}
$filtered = filter_var($value, FILTER_VALIDATE_INT);
return $filtered === false ? (int)$value : $filtered;
}
/**
* Возвращает первый элемент массива без перемотки указателя
*
* @param array $array Входной массив
* @param callable|null $callback Замыкание для предварительной фильтрации вх. массива
* @return mixed
*/
function array_first(array $array, ?callable $callback = null): mixed
{
is_null($callback) || $array = array_filter($array, $callback);
return $array[array_key_first($array)] ?? null;
}
/**
* Возвращает последний элемент массива без перемотки указателя
*
* @param array $array Входной массив
* @param callable|null $callback Замыкание для предварительной фильтрации вх. массива
* @return mixed
*/
function array_last(array $array, ?callable $callback = null): mixed
{
is_null($callback) || $array = array_filter($array, $callback);
return $array[array_key_last($array)] ?? null;
}
/**
* Get ini-file instance
*
* @return IniFile
*/
function ini(): IniFile
{
return Kernel::instance()->ini();
}