2023-04-15 22:54:07 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2023-08-14 15:15:05 +00:00
|
|
|
|
namespace Helper;
|
2023-11-02 13:57:39 +00:00
|
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
|
|
2023-08-14 15:15:05 +00:00
|
|
|
|
class Conversion
|
2023-04-15 22:54:07 +00:00
|
|
|
|
{
|
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-11-02 13:57:39 +00:00
|
|
|
|
$dataString = str_replace('||', '|', $dataString); // любители забивать руками параметры и задваивать разделители.
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
2023-11-02 13:57:39 +00:00
|
|
|
|
public static function secondsToTimeout(int|string $seconds): string
|
2023-07-07 15:36:23 +00:00
|
|
|
|
{
|
|
|
|
|
$seconds = (int)$seconds;
|
2023-11-02 13:57:39 +00:00
|
|
|
|
$time = new DateTime();
|
2023-07-07 15:36:23 +00:00
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|