forked from Brunojoe1994/-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringNormalizer.php
More file actions
37 lines (31 loc) · 983 Bytes
/
StringNormalizer.php
File metadata and controls
37 lines (31 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
declare(strict_types=1);
namespace Strata\Data\Decode;
use Strata\Data\Exception\DecoderException;
use Symfony\Contracts\HttpClient\ResponseInterface;
class StringNormalizer
{
/**
* Take a string or object, and return a string representing the content
*
* @param mixed $data
* @return string
* @throws DecoderException
*/
public static function getString($data): string
{
if (is_string($data)) {
return $data;
}
if (is_object($data)) {
if ($data instanceof ResponseInterface) {
return $data->getContent();
}
if (method_exists($data, '__toString')) {
return $data->__toString();
}
throw new DecoderException(sprintf('Cannot convert object of type "%s" into a string', get_class($data)));
}
throw new DecoderException(sprintf('Cannot convert %s into a string', gettype($data)));
}
}