1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Transport; |
4
|
|
|
|
5
|
|
|
use function GuzzleHttp\Psr7\uri_for; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
class Utils |
10
|
|
|
{ |
11
|
115 |
|
public static function jsonDecode(ResponseInterface $response, bool $assoc = true) |
12
|
|
|
{ |
13
|
|
|
$jsonErrors = [ |
14
|
115 |
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', |
15
|
115 |
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', |
16
|
115 |
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', |
17
|
115 |
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', |
18
|
115 |
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' |
19
|
115 |
|
]; |
20
|
|
|
|
21
|
115 |
|
$responseBody = (string) $response->getBody(); |
22
|
|
|
|
23
|
115 |
|
if (strlen($responseBody) === 0) { |
24
|
1 |
|
return $responseBody; |
25
|
1 |
|
} |
26
|
1 |
|
|
27
|
1 |
|
$data = json_decode($responseBody, $assoc); |
28
|
|
|
|
29
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
30
|
114 |
|
$last = json_last_error(); |
31
|
|
|
throw new \InvalidArgumentException( |
32
|
|
|
'Unable to parse JSON data: ' . (isset($jsonErrors[$last]) ? $jsonErrors[$last] : 'Unknown error') |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $data; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Method for flattening a nested array. |
41
|
94 |
|
* |
42
|
|
|
* @param array $data The nested array |
43
|
94 |
|
* @param string $key The key to extract |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public static function flattenJson($data, string $key = null) |
48
|
|
|
{ |
49
|
|
|
return (!empty($data) && $key && isset($data[$key])) ? $data[$key] : $data; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Method for normalize an URL string. |
54
|
|
|
* |
55
|
|
|
* Append the http:// prefix if not present, and add a |
56
|
6 |
|
* closing url separator when missing. |
57
|
|
|
* |
58
|
6 |
|
* @param string $url The url representation. |
59
|
6 |
|
* |
60
|
6 |
|
* @return string |
61
|
|
|
*/ |
62
|
6 |
|
public static function normalizeUrl(string $url): string |
63
|
|
|
{ |
64
|
|
|
if (strpos($url, 'http') === false) { |
65
|
|
|
$url = 'http://' . $url; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return rtrim($url, '/') . '/'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add an unlimited list of paths to a given URI. |
73
|
2 |
|
* |
74
|
|
|
* @param UriInterface $uri |
75
|
2 |
|
* @param ...$paths |
76
|
|
|
* |
77
|
|
|
* @return UriInterface |
78
|
5 |
|
*/ |
79
|
|
|
public static function addPaths(UriInterface $uri, ...$paths): UriInterface |
80
|
5 |
|
{ |
81
|
|
|
return uri_for(rtrim((string) $uri, '/') . '/' . implode('/', $paths)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function appendPath(UriInterface $uri, $path): UriInterface |
85
|
|
|
{ |
86
|
|
|
return uri_for(rtrim((string) $uri, '/') . '/' . $path); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: