<?php

namespace Fifa\ConnectServiceBus\Sdk\Utils;

class ToStringConverter
{
    /**
     * Creates string from an array
     *
     * @param array $subject - array to be transformed to string
     *
     * @returns string
     */
    static function arrayToString(array $subject): string
    {
        $text = '[';
        foreach ($subject as $key => $item) {
            $text .= '"' . $key . '": ' . self::mixedToString($item) . ', ';
        }
        $text = rtrim($text, ', ');
        return $text . ']';
    }

    /**
     * Creates string from an unknown object
     *
     * @param mixed $subject - object to be transformed to string
     *
     * @returns string
     */
    static function mixedToString($subject): string
    {
        if (is_null($subject)) {
            return 'null';
        }
        if (is_scalar($subject)) {
            return strval($subject);
        }
        if (is_array($subject)) {
            return self::arrayToString($subject);
        }
        if (method_exists($subject, '__toString')) {
            return $subject->__toString();
        }

        return self::arrayToString((array)$subject);
    }
}