game/_incl_data/class/Core/ConversionHelper.php

26 lines
755 B
PHP

<?php
namespace Core;
class ConversionHelper
{
/** Ïðåâðàùàåò ñòðîêó data ('a=1|b=2|c=3') èç ÁÄ â ìàññèâ [a=>1, b=>2, c=>3].
* @param string $dataString
* @return array
*/
public static function dataStringToArray(string $dataString): array
{
$arr = json_decode(str_replace(['=', '|'], ['":', ',"'], '{"' . $dataString . '}'), true);
return $arr ?: [];
}
/** Ïðåâðàùàåò ìàññèâ [a=>1, b=>2, c=>3] â ñòðîêó data ('a=1|b=2|c=3') äëÿ ÁÄ.
* @param array $dataArray
* @return string
*/
public static function arrayToDataString(array $dataArray): string
{
$str = json_encode($dataArray);
return $str ? str_replace(['":', ',"', '{"', '}'], ['=', '|'], $str) : '';
}
}