1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\LinkedIn\Http; |
4
|
|
|
|
5
|
|
|
use Happyr\LinkedIn\Exceptions\LinkedInApiException; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
class ResponseConverter |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param ResponseInterface $response |
12
|
|
|
* @param string $format |
|
|
|
|
13
|
|
|
* |
14
|
|
|
* @return ResponseInterface|\Psr\Http\Message\StreamInterface|\SimpleXMLElement|string |
15
|
|
|
* |
16
|
|
|
* @throws LinkedInApiException |
17
|
|
|
*/ |
18
|
|
|
public static function convert(ResponseInterface $response, $requestFormat, $dataType) |
19
|
|
|
{ |
20
|
|
|
if (($requestFormat === 'json' && $dataType === 'simple_xml') || |
21
|
|
|
($requestFormat === 'xml' && $dataType === 'array')) { |
22
|
|
|
throw new \InvalidArgumentException(sprintf('Can not use reponse data format "%s" with the request format "s%"', $dataType, $requestFormat)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
switch ($dataType) { |
26
|
|
|
case 'array': |
27
|
|
|
return self::convertToArray($response); |
28
|
|
|
case 'string': |
29
|
|
|
return $response->getBody()->__toString(); |
30
|
|
|
case 'simple_xml': |
31
|
|
|
return self::convertToSimpleXml($response); |
32
|
|
|
case 'stream': |
33
|
|
|
return $response->getBody(); |
34
|
|
|
case 'psr7': |
35
|
|
|
return $response; |
36
|
|
|
default: |
37
|
|
|
throw new \InvalidArgumentException(sprintf('Format "%s" is not supported', $dataType)); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ResponseInterface $response |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public static function convertToArray(ResponseInterface $response) |
47
|
|
|
{ |
48
|
|
|
return json_decode($response->getBody(), true); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ResponseInterface $response |
53
|
|
|
* |
54
|
|
|
* @return \SimpleXMLElement |
55
|
|
|
* |
56
|
|
|
* @throws LinkedInApiException |
57
|
|
|
*/ |
58
|
|
|
public static function convertToSimpleXml(ResponseInterface $response) |
59
|
|
|
{ |
60
|
|
|
$body = $response->getBody(); |
61
|
|
|
try { |
62
|
|
|
return new \SimpleXMLElement((string) $body ?: '<root />'); |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
throw new LinkedInApiException( |
65
|
|
|
array( |
66
|
|
|
'error' => array( |
67
|
|
|
'message' => 'Unable to parse response body into XML: '.$e->getMessage(), |
68
|
|
|
'type' => 'XmlParseException', |
69
|
|
|
), |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.