<?php

namespace common\modules\orderPayment\WOP\OnlinePayments\Core\Infrastructure\Serializer\Concrete;

use common\modules\orderPayment\WOP\OnlinePayments\Core\Infrastructure\Serializer\Serializer;
use stdClass;
/**
 * Class JsonSerializer
 *
 * @package OnlinePayments\Core\Infrastructure\Serializer\Concrete
 */
class JsonSerializer extends Serializer
{
    /**
     * Serializes data.
     *
     * @param mixed $data Data to be serialized.
     *
     * @return string String representation of the serialized data.
     */
    protected function doSerialize($data): string
    {
        if (is_object($data)) {
            if (method_exists($data, 'toArray')) {
                $preparedArray = $data->toArray();
                $preparedArray['class_name'] = get_class($data);
                return json_encode($preparedArray);
            }
            if ($data instanceof stdClass) {
                $data->className = get_class($data);
            }
            return json_encode($data);
        }
        if (is_array($data)) {
            return json_encode($data);
        }
        return json_encode($data);
    }
    /**
     * Unserializes data.
     *
     * @param string $serialized Serialized data.
     *
     * @return mixed Unserialized data.
     */
    protected function doUnserialize(string $serialized)
    {
        $unserialized = json_decode($serialized, \true);
        if (!is_array($unserialized) || !array_key_exists('class_name', $unserialized)) {
            return $unserialized;
        }
        $class = $unserialized['class_name'];
        unset($unserialized['class_name']);
        return $class::fromArray($unserialized);
    }
}
