2023-04-15 22:54:07 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Core;
|
|
|
|
|
class ConversionHelper
|
|
|
|
|
{
|
2023-06-13 00:44:13 +00:00
|
|
|
|
/** Превращает строку data ('a=1|b=2|c=3') из БД в массив [a=>1, b=>2, c=>3].
|
2023-04-15 22:54:07 +00:00
|
|
|
|
* @param string $dataString
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function dataStringToArray(string $dataString): array
|
|
|
|
|
{
|
2023-07-26 04:11:35 +00:00
|
|
|
|
if (mb_substr($dataString, -1) === '|') {
|
|
|
|
|
$dataString = rtrim($dataString, '|');
|
|
|
|
|
}
|
2023-04-15 22:54:07 +00:00
|
|
|
|
$arr = json_decode(str_replace(['=', '|'], ['":', ',"'], '{"' . $dataString . '}'), true);
|
|
|
|
|
return $arr ?: [];
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 00:44:13 +00:00
|
|
|
|
/** Превращает массив [a=>1, b=>2, c=>3] в строку data ('a=1|b=2|c=3') для БД.
|
2023-04-15 22:54:07 +00:00
|
|
|
|
* @param array $dataArray
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function arrayToDataString(array $dataArray): string
|
|
|
|
|
{
|
|
|
|
|
$str = json_encode($dataArray);
|
|
|
|
|
return $str ? str_replace(['":', ',"', '{"', '}'], ['=', '|'], $str) : '';
|
|
|
|
|
}
|
2023-07-07 15:36:23 +00:00
|
|
|
|
|
|
|
|
|
/** Превращает количество секунд в человекопонятное Х мес. Х дн. Х ч. Х мин. Х сек.,
|
|
|
|
|
* используемое обычно для отображения игровых таймаутов.
|
|
|
|
|
* @param int|string $seconds
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function secondsToTimeout($seconds): string
|
|
|
|
|
{
|
|
|
|
|
$seconds = (int)$seconds;
|
|
|
|
|
$time = new \DateTime();
|
|
|
|
|
$time->setTimestamp($seconds);
|
|
|
|
|
$sec = intval($time->format('s'));
|
|
|
|
|
$min = intval($time->format('i'));
|
|
|
|
|
$hr = intval($time->format('G'));
|
|
|
|
|
$day = intval($time->format('j'));
|
|
|
|
|
$month = intval($time->format('n'));
|
|
|
|
|
$timeout = '';
|
|
|
|
|
if ($month > 1) {
|
|
|
|
|
$timeout .= $month . ' мес. ';
|
|
|
|
|
}
|
|
|
|
|
if ($day > 1) {
|
|
|
|
|
$timeout .= $day . ' дн. ';
|
|
|
|
|
}
|
|
|
|
|
if ($hr) {
|
|
|
|
|
$timeout .= $hr . ' ч. ';
|
|
|
|
|
}
|
|
|
|
|
if ($sec && !$min) {
|
|
|
|
|
$timeout .= $sec . ' сек. ';
|
|
|
|
|
} elseif ($min) {
|
|
|
|
|
$timeout .= $min . ' мин. ';
|
|
|
|
|
}
|
|
|
|
|
return $timeout;
|
|
|
|
|
}
|
|
|
|
|
}
|