Happyr\LinkedIn\Http\ResponseConverter::convert PHP Method

convert() public static method

Convert a PSR-7 response to a data type you want to work with.
public static convert ( Psr\Http\Message\ResponseInterface $response, string $requestFormat, string $dataType ) : Psr\Http\Message\ResponseInterface | Psr\Http\Message\StreamInterface | SimpleXMLElement | string
$response Psr\Http\Message\ResponseInterface
$requestFormat string
$dataType string
return Psr\Http\Message\ResponseInterface | Psr\Http\Message\StreamInterface | SimpleXMLElement | string
    public static function convert(ResponseInterface $response, $requestFormat, $dataType)
    {
        if ($requestFormat === 'json' && $dataType === 'simple_xml' || $requestFormat === 'xml' && $dataType === 'array') {
            throw new InvalidArgumentException('Can not use reponse data format "%s" with the request format "%s".', $dataType, $requestFormat);
        }
        switch ($dataType) {
            case 'array':
                return self::convertToArray($response);
            case 'string':
                return $response->getBody()->__toString();
            case 'simple_xml':
                return self::convertToSimpleXml($response);
            case 'stream':
                return $response->getBody();
            case 'psr7':
                return $response;
            default:
                throw new InvalidArgumentException('Format "%s" is not supported', $dataType);
        }
    }

Usage Example

 /**
  * @expectedException \Happyr\LinkedIn\Exception\InvalidArgumentException
  */
 public function testConvertJsonToFoobar()
 {
     $body = '{"foo":"bar"}';
     $response = new Response(200, [], $body);
     ResponseConverter::convert($response, 'json', 'foobar');
 }
All Usage Examples Of Happyr\LinkedIn\Http\ResponseConverter::convert