-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathHelpers.php
57 lines (46 loc) · 1.11 KB
/
Helpers.php
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace Milo\Github;
/**
* Just helpers.
*
* The JSON encode/decode methods are stolen from Nette Utils (https://github.com/nette/utils).
*
* @author David Grudl
* @author Miloslav Hůla (https://github.com/milo)
*/
class Helpers
{
private static Http\IClient $client;
/**
* @throws JsonException
*/
public static function jsonEncode(mixed $value): string
{
$json = json_encode($value, JSON_UNESCAPED_UNICODE);
if ($error = json_last_error()) {
throw new JsonException(json_last_error_msg(), $error);
}
return $json;
}
/**
* @throws JsonException
*/
public static function jsonDecode(string $json): mixed
{
$value = json_decode($json, false, 512, JSON_BIGINT_AS_STRING);
if ($error = json_last_error()) {
throw new JsonException(json_last_error_msg(), $error);
}
return $value;
}
public static function createDefaultClient(bool $newInstance = false): Http\IClient
{
if (empty(self::$client) || $newInstance) {
self::$client = extension_loaded('curl')
? new Http\CurlClient
: new Http\StreamClient;
}
return self::$client;
}
}