<?php

declare(strict_types=1);
namespace Tests\Adawolfa\Retrofit;
use Adawolfa\Retrofit;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use ReflectionIntersectionType;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;

final class Converter implements Retrofit\Converter
{

	public function toString(mixed $value, ReflectionMethod $method, ReflectionParameter $parameter): ?string
	{
		return strtoupper($parameter->name);
	}

	public function toStream(mixed $value, ReflectionMethod $method, ReflectionParameter $parameter): StreamInterface
	{
		return Utils::streamFor(strtoupper($parameter->name));
	}

	public function fromStream(ResponseInterface $response, ReflectionMethod $method): string
	{
		$returnType = $method->getReturnType();

		if ($returnType instanceof ReflectionNamedType
			&& in_array($returnType->getName(), ['string', 'mixed'], true)) {
			return strtoupper($response->getBody()->getContents());
		}

		throw new Retrofit\UnsupportedConversionException;
	}

}