-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathHelpers.php
101 lines (81 loc) · 2.38 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
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 $jsonMessages = [
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'Syntax error, malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
JSON_ERROR_UTF8 => 'Invalid UTF-8 sequence',
];
/** @var Http\IClient */
private static $client;
/**
* @param mixed
* @return string
*
* @throws JsonException
*/
public static function jsonEncode($value)
{
if (PHP_VERSION_ID < 50500) {
set_error_handler(function($severity, $message) { // needed to receive 'recursion detected' error
restore_error_handler();
throw new JsonException($message);
});
}
$json = json_encode($value, JSON_UNESCAPED_UNICODE);
if (PHP_VERSION_ID < 50500) {
restore_error_handler();
}
if ($error = json_last_error()) {
$message = isset(static::$jsonMessages[$error])
? static::$jsonMessages[$error]
: (PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error');
throw new JsonException($message, $error);
}
$json = str_replace(array("\xe2\x80\xa8", "\xe2\x80\xa9"), array('\u2028', '\u2029'), $json);
return $json;
}
/**
* @param mixed
* @return string
*
* @throws JsonException
*/
public static function jsonDecode($json)
{
$json = (string) $json;
if (!preg_match('##u', $json)) {
throw new JsonException('Invalid UTF-8 sequence', 5); // PECL JSON-C
}
$value = json_decode($json, FALSE, 512, (defined('JSON_C_VERSION') && PHP_INT_SIZE > 4) ? 0 : JSON_BIGINT_AS_STRING);
if ($value === NULL && $json !== '' && strcasecmp($json, 'null')) { // '' does not clear json_last_error()
$error = json_last_error();
throw new JsonException(isset(static::$jsonMessages[$error]) ? static::$jsonMessages[$error] : 'Unknown error', $error);
}
return $value;
}
/**
* @param bool
* @return Http\IClient
*/
public static function createDefaultClient($newInstance = FALSE)
{
if (self::$client === NULL || $newInstance) {
self::$client = extension_loaded('curl')
? new Http\CurlClient
: new Http\StreamClient;
}
return self::$client;
}
}