diff --git a/init.php b/init.php index 89334f5..536fdc8 100644 --- a/init.php +++ b/init.php @@ -1,4 +1,6 @@ - $c[$k] ?? [] + [$i[$k]]];}, $c);}, []); +}} /* $payload = (function_exists('array_merge_save')) ? array_merge_save($_REQUEST_JSON, $_GET, $_POST) : array_merge($_POST, $_GET, $_REQUEST_JSON); */ +///*/ функция разглаживает многомерный ассоциативный массив в одномерный с точками в ключах///*/ +if(!function_exists('array_flatten')){function array_flatten($array, $prefix = ''){ + $ret = [];foreach($array as $k => $v){ + $cur_k = is_string($k) ? ($prefix !== '' ? $prefix.'.'.$k : $k) : $prefix; + if(is_array($v)){$ret = array_merge($ret, array_flatten($v, $cur_k));}else + if(is_string($k)){$ret[$cur_k] = $v;}else + if($cur_k !== ''){$ret[$cur_k] = $v;}} +return $ret;}} //------------------------------------------------------------------------------FILE-SYSTEM------------------------------------------------------------------------------------------------------------------------------// ///*/ ahilespelid Метод возвращает путь до папки local/php_interface Bitrix при учёте что текущий файл лежит в local/php_interface///*/ if(!function_exists('functions_path')){function functions_path(string $file=''){ @@ -89,6 +104,20 @@ function array_unique_key($array, $key){ $ret = (empty($file)) ? $ret : ((file_exists($f = $ret.DIRECTORY_SEPARATOR.$file)) ? $f : null); return $ret;}} +///*/ ahilespelid Метод возвращает путь до папки img_path ///*/ +if(!function_exists('img_path')){function img_path(string $file=''){ + $ret = resource_path('img'); + $ret = (empty($file)) ? $ret : ((file_exists($f = $ret.DIRECTORY_SEPARATOR.$file)) ? $f : null); +return $ret;}} + +///*/ ahilespelid Метод возвращает путь до папки img_path ///*/ +if(!function_exists('img_base64')){function img_base64(string $file, string $type = ''){ + if(empty($file = img_path($file))){return null;}else{ + $mime = (empty($type)) ? getimagesize($file)['mime'] : $type; + $ret = 'data:'.$mime.';base64,'.base64_encode(file_get_contents($file)); + } +return $ret;}} + //------------------------------------------------------------------------------IS-CONDITIONS----------------------------------------------------------------------------------------------------------------------------// ///*/ Функция проверка строки на дату ///*/ if(!function_exists('is_date')){ @@ -102,10 +131,12 @@ function is_date($d){ return (preg_match('/^[7|8][0-9]{'.$minDigits.','.$maxDigits.'}\z/', $s)) ? $s : null;}} ///*/ahilespelid Проверка строки на email///*/ if(!function_exists('is_email')){function is_email(string $email){return (false !== filter_var($email, FILTER_VALIDATE_EMAIL)) ? $email : null;}} +///*/ahilespelid Проверка строки на ip///*/ +if(!function_exists('is_ip')){function is_ip(string $ip){return (false !== filter_var($ip, FILTER_VALIDATE_IP)) ? $ip : null;}} ///*/ Фукция проверяет строку на json ///*/ -if(!function_exists('is_json')){function is_json($json){ +if(!function_exists('is_json')){function is_json(string $json){ $decoded = @json_decode($json); -return \JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, \JSON_FORCE_OBJEC) : null;}} +return \JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, \JSON_FORCE_OBJECT) : null;}} ///*/ Фукция проверяет переменную на true ///*/ if(!function_exists('is_true')){function is_true($bool){ $ret = (true === is_bool($bool) && true === $bool) ? true : false; @@ -121,8 +152,17 @@ function translit(string $t){ return strtr($t, $converter);} ///*/ Функция разбивает строку по разделителям переданным в массиве///*/ if(!function_exists('mexplode')){function mexplode(array $delimiters, string $string){ - $chr = '::::::::::::::::::::::::::::::::::::::::::::::::'; + $chr = '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::'; return explode($chr, str_replace($delimiters, $chr, $string));}} +///*/ Функция переводчик google///*/ +if(!function_exists('google_translate')){function google_translate($text, $from_lan = 'ru', $to_lan = 'en', $key = 'AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw'){ + $obj = json_decode(file_get_contents('https://translation.googleapis.com/language/translate/v2?q='.urlencode($text).'&source='.$from_lan.'&target='.$to_lan .'&format=text&key='.$key), true); + $ret = trim($obj['data']['translations']['0']['translatedText']); +return (empty($ret)) ? false : $ret;}} +///*/ Функция проверяет вхождение слова в строку ///*/ +if(!function_exists('word_contains')){function word_contains(string $text, string $word): bool { + $escaped = preg_quote($word, '/'); +return (bool) preg_match("/(^|\W)" . $escaped . "(\W|$)/", $text);}} //------------------------------------------------------------------------------STRING-GENERATORS------------------------------------------------------------------------------------------------------------------------// ///*/ Функция генерирует случайный префикс из ascii таблицы///*/ @@ -134,7 +174,6 @@ function translit(string $t){ ///*/ Функция генерирует GUID из com_create_guid///*/ if(!function_exists('GUID')){function GUID(){return strtoupper(com_create_guid());}} - //------------------------------------------------------------------------------ANALOG-----------------------------------------------------------------------------------------------------------------------------------// ///*/ Функция аналог str_starts_with из php 8 ///*/ if(!function_exists('str_starts_with')){function str_starts_with(string $haystack, string $needle){ @@ -147,7 +186,7 @@ function translit(string $t){ $data[8] = chr(ord($data[8]) & 0x3f | 0x80); return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));}} -//------------------------------------------------------------------------------DATA-EXCHANGE-----------------------------------------------------------------------------------------------------------------------------------// +//------------------------------------------------------------------------------DATA-EXCHANGE----------------------------------------------------------------------------------------------------------------------------// ///*/ Метод эмитации post запроса из php///*/ if(!function_exists('post')){function post(string $url, array $data, array $headers = [], bool $data_json_encode = false){ $data = ($data_json_encode) ? json_encode($data) : http_build_query($data); @@ -163,3 +202,46 @@ function translit(string $t){ return $ret;}} ///*/ahilespelid///*/ +///*/musa///*/ +///*/Функция для форматирования даты///*/ + +if(!function_exists('format_date_intl')){ + function format_date_intl(string $date, string $lang = 'russian'): ?string { + if(!$d = is_date($date)){return null;} + + $supportedLocales = ['russian' => 'ru_RU', 'english' => 'en_US']; + $locale = $supportedLocales[$lang] ?? 'ru_RU'; +// $d = DateTime::createFromFormat('Y-m-d H:i:s', $date) ?: new DateTime($date); + $now = new DateTime(); + $yesterday = (clone $now)->modify('-1 day'); + + $diff = $now->getTimestamp() - $d->getTimestamp(); + + if($diff < 3600 && $d->format('Y-m-d') === $now->format('Y-m-d')){ + $minutes = floor($diff / 60); + return $minutes > 0 + ? ($lang === 'ru' ? "$minutes мин. назад" : "$minutes min. ago") + : ($lang === 'ru' ? "только что" : "just now");} + + if($diff < 86400 && $d->format('Y-m-d') === $now->format('Y-m-d')){ + $hours = floor($diff / 3600); + return $lang === 'ru' ? "$hours ч. назад" : "$hours h. ago";} + + if($d->format('Y-m-d') === $yesterday->format('Y-m-d')){ + $timeFormat = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::SHORT); + return ($lang === 'ru' ? 'Вчера в ' : 'Yesterday at ') . $timeFormat->format($d);} + + $dateFormat = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE); +return $dateFormat->format($d);}} + + +// if(!function_exists('format_date_intl')){function format_date_intl($date, $lang = 'ru_RU', $date_format = 'd MMMM yyyy'){ +// if(!($d = is_date($date))){return null;} +// $formatter = new IntlDateFormatter('ru_RU', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Moscow', IntlDateFormatter::GREGORIAN, $date_format); +// $timestamp = $d->getTimestamp(); +// return $formatter->format($timestamp);}} + +///*/ ВЫНЕСЕМ НА БРИФ ///*/ +if(!function_exists('first_slash')) {function first_slash($str){return ('/' == $str) ? $str : '/' . $str;}} + +///*/musa///*/