1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\LinkedIn\Http; |
4
|
|
|
|
5
|
|
|
use Happyr\LinkedIn\Exception\InvalidArgumentException; |
6
|
|
|
use Happyr\LinkedIn\Exception\LinkedInTransferException; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class ResponseConverter |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param ResponseInterface $response |
13
|
|
|
* @param string $format |
|
|
|
|
14
|
|
|
* @param string $dataType |
15
|
|
|
* |
16
|
|
|
* @return ResponseInterface|\Psr\Http\Message\StreamInterface|\SimpleXMLElement|string |
17
|
|
|
* |
18
|
|
|
* @throws InvalidArgumentException |
19
|
|
|
* @throws LinkedInTransferException |
20
|
|
|
*/ |
21
|
|
|
public static function convert(ResponseInterface $response, $requestFormat, $dataType) |
22
|
|
|
{ |
23
|
|
|
if (($requestFormat === 'json' && $dataType === 'simple_xml') || |
24
|
|
|
($requestFormat === 'xml' && $dataType === 'array')) { |
25
|
|
|
throw new InvalidArgumentException('Can not use reponse data format "%s" with the request format "%s".', $dataType, $requestFormat); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
switch ($dataType) { |
29
|
|
|
case 'array': |
30
|
|
|
return self::convertToArray($response); |
31
|
|
|
case 'string': |
32
|
|
|
return $response->getBody()->__toString(); |
33
|
|
|
case 'simple_xml': |
34
|
|
|
return self::convertToSimpleXml($response); |
35
|
|
|
case 'stream': |
36
|
|
|
return $response->getBody(); |
37
|
|
|
case 'psr7': |
38
|
|
|
return $response; |
39
|
|
|
default: |
40
|
|
|
throw new InvalidArgumentException('Format "%s" is not supported', $dataType); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ResponseInterface $response |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public static function convertToArray(ResponseInterface $response) |
50
|
|
|
{ |
51
|
|
|
return json_decode($response->getBody(), true); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ResponseInterface $response |
56
|
|
|
* |
57
|
|
|
* @return \SimpleXMLElement |
58
|
|
|
* |
59
|
|
|
* @throws LinkedInTransferException |
60
|
|
|
*/ |
61
|
|
|
public static function convertToSimpleXml(ResponseInterface $response) |
62
|
|
|
{ |
63
|
|
|
$body = $response->getBody(); |
64
|
|
|
try { |
65
|
|
|
return new \SimpleXMLElement((string) $body ?: '<root />'); |
66
|
|
|
} catch (\Exception $e) { |
67
|
|
|
throw new LinkedInTransferException('Unable to parse response body into XML.'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.