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