|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Audiens\DoubleclickClient\entity; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
|
6
|
|
|
|
|
7
|
|
|
class ApiResponse |
|
8
|
|
|
{ |
|
9
|
|
|
public const STATUS_SUCCESS = 'OK'; |
|
10
|
|
|
|
|
11
|
|
|
/** @var bool */ |
|
12
|
|
|
protected $successful = false; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $response; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Error */ |
|
18
|
|
|
protected $error; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
protected $responseArray; |
|
22
|
|
|
|
|
23
|
|
|
public function getResponse(): string |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->response; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function isSuccessful(): bool |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->successful; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getError(): Error |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->error; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getResponseArray(): array |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->responseArray; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function fromResponse(Response $response): ApiResponse |
|
44
|
|
|
{ |
|
45
|
|
|
$self = new self(); |
|
46
|
|
|
$error = new Error(); |
|
47
|
|
|
$responseContent = self::getResponseContent($response); |
|
48
|
|
|
|
|
49
|
|
|
$self->successful = false; |
|
50
|
|
|
$self->response = $responseContent; |
|
51
|
|
|
$self->responseArray = self::arrayFromXml($responseContent); |
|
52
|
|
|
|
|
53
|
|
|
if (!isset($self->responseArray['body']['envelope']['body']['fault'])) { |
|
54
|
|
|
$self->successful = true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!$self->isSuccessful()) { |
|
58
|
|
|
$error = Error::fromArray($self->responseArray['body']['envelope']['body']['fault']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$self->error = $error; |
|
62
|
|
|
|
|
63
|
|
|
return $self; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private static function getResponseContent(Response $response): string |
|
67
|
|
|
{ |
|
68
|
|
|
$responseContent = $response->getBody()->getContents(); |
|
69
|
|
|
$response->getBody()->rewind(); |
|
70
|
|
|
|
|
71
|
|
|
return static::asciiToUTF8($responseContent); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private static function arrayFromXml($xmlString): array |
|
75
|
|
|
{ |
|
76
|
|
|
$document = new \DOMDocument(); // create dom element |
|
77
|
|
|
|
|
78
|
|
|
libxml_use_internal_errors(true); // silence errors |
|
79
|
|
|
$document->loadHTML($xmlString); |
|
80
|
|
|
libxml_clear_errors(); |
|
81
|
|
|
|
|
82
|
|
|
$xml = $document->saveXML($document->documentElement); |
|
83
|
|
|
$xml = simplexml_load_string($xml); // create xml |
|
84
|
|
|
|
|
85
|
|
|
return json_decode(json_encode($xml), true); // extract array |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Converts a ASCII string with UTF-8 characters in a UTF-8 string. |
|
90
|
|
|
* Correctly encoded UTF-8 strings will be passed-through. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $string |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function asciiToUTF8(string $string): string |
|
97
|
|
|
{ |
|
98
|
|
|
$windows1252Chars = [ |
|
99
|
|
|
"\xC3\x80", "\xC3\x81", "\xC3\x82", "\xC3\x83", "\xC3\x84", |
|
100
|
|
|
"\xC3\x85", "\xC3\x86", "\xC3\x87", "\xC3\x88", "\xC3\x89", |
|
101
|
|
|
"\xC3\x8A", "\xC3\x8B", "\xC3\x8C", "\xC3\x8D", "\xC3\x8E", |
|
102
|
|
|
"\xC3\x8F", "\xC3\x90", "\xC3\x91", "\xC3\x92", "\xC3\x93", |
|
103
|
|
|
"\xC3\x94", "\xC3\x95", "\xC3\x96", "\xC3\x97", "\xC3\x98", |
|
104
|
|
|
"\xC3\x99", "\xC3\x9A", "\xC3\x9B", "\xC3\x9C", "\xC3\x9D", |
|
105
|
|
|
"\xC3\x9E", "\xC3\x9F", "\xC3\xA0", "\xC3\xA1", "\xC3\xA2", |
|
106
|
|
|
"\xC3\xA3", "\xC3\xA4", "\xC3\xA5", "\xC3\xA6", "\xC3\xA7", |
|
107
|
|
|
"\xC3\xA8", "\xC3\xA9", "\xC3\xAA", "\xC3\xAB", "\xC3\xAC", |
|
108
|
|
|
"\xC3\xAD", "\xC3\xAE", "\xC3\xAF", "\xC3\xB0", "\xC3\xB1", |
|
109
|
|
|
"\xC3\xB2", "\xC3\xB3", "\xC3\xB4", "\xC3\xB5", "\xC3\xB6", |
|
110
|
|
|
"\xC3\xB7", "\xC3\xB8", "\xC3\xB9", "\xC3\xBA", "\xC3\xBB", |
|
111
|
|
|
"\xC3\xBC", "\xC3\xBD", "\xC3\xBE", "\xC3\xBF", |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
|
|
foreach ($windows1252Chars as $nonUtfChars) { |
|
115
|
|
|
if (stripos($string, $nonUtfChars) !== false) { |
|
116
|
|
|
return mb_convert_encoding($string, 'Windows-1252'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $string; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|