Skip to content

Commit 09b6519

Browse files
authored
Use mb_convert_encoding when encoding detected, else try utf8_encode (gabrielbull#112)
1 parent 70a6c99 commit 09b6519

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/Request.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ public function request($access, $request, $endpointurl)
132132
]);
133133

134134
if ($response->getStatusCode() === 200) {
135-
if (function_exists('mb_convert_encoding')) {
136-
$body = mb_convert_encoding($body, 'UTF-8', mb_detect_encoding($body));
137-
}
135+
$body = $this->convertEncoding($body);
138136

139137
$xml = new SimpleXMLElement($body);
140138
if (isset($xml->Response) && isset($xml->Response->ResponseStatusCode)) {
@@ -218,4 +216,22 @@ public function getEndpointUrl()
218216
{
219217
return $this->endpointUrl;
220218
}
219+
220+
/**
221+
* @param $body
222+
* @return string
223+
*/
224+
protected function convertEncoding($body)
225+
{
226+
if (!function_exists('mb_convert_encoding')) {
227+
return $body;
228+
}
229+
230+
$encoding = mb_detect_encoding($body);
231+
if ($encoding) {
232+
return mb_convert_encoding($body, 'UTF-8', $encoding);
233+
}
234+
235+
return utf8_encode($body);
236+
}
221237
}

tests/Ups/Tests/EntityTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,4 @@ public function getListOfNodesWithoutNodeInterface()
192192
return [$item];
193193
}, $this->nodeListNoNodeInterface);
194194
}
195-
196195
}

tests/Ups/Tests/ExceptionTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
class ExceptionTest extends PHPUnit_Framework_TestCase
88
{
9-
109
public function testInvalidResponseException()
1110
{
1211
$exception = new InvalidResponseException('Test Message', 1);
@@ -24,5 +23,4 @@ public function testRequestException()
2423
$this->assertEquals('Test Message', $exception->getMessage());
2524
$this->assertEquals(1, $exception->getCode());
2625
}
27-
2826
}

0 commit comments

Comments
 (0)