-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathParserHelper.php
More file actions
58 lines (50 loc) · 1.62 KB
/
ParserHelper.php
File metadata and controls
58 lines (50 loc) · 1.62 KB
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
<?php
declare(strict_types=1);
namespace JsonStreamingParser;
abstract class ParserHelper
{
public static function isDigit(string $ctext): bool
{
// Only concerned with the first character in a number.
return ctype_digit($ctext) || '-' === $ctext;
}
public static function isHexCharacter(string $char): bool
{
return ctype_xdigit($char);
}
/**
* @see http://stackoverflow.com/questions/1805802/php-convert-unicode-codepoint-to-utf-8
*/
public static function convertCodepointToCharacter(int $char): string
{
if ($char <= 0x7F) {
return \chr($char);
}
if ($char <= 0x7FF) {
return \chr(($char >> 6) + 192).\chr(($char & 63) + 128);
}
if ($char <= 0xFFFF) {
return \chr(($char >> 12) + 224).\chr((($char >> 6) & 63) + 128).\chr(($char & 63) + 128);
}
if ($char <= 0x1FFFFF) {
return \chr(($char >> 18) + 240)
.\chr((($char >> 12) & 63) + 128)
.\chr((($char >> 6) & 63) + 128)
.\chr(($char & 63) + 128);
}
return '';
}
/**
* @return float|int|null
*/
public static function convertToNumber(string $text)
{
// thanks to #andig for the fix for big integers
if (filter_var($text, FILTER_VALIDATE_INT) !== false && (float) $text === (float) ((int) $text)) {
// natural number PHP_INT_MIN < $num < PHP_INT_MAX
return (int) $text;
}
// real number or natural number outside PHP_INT_MIN ... PHP_INT_MAX
return (float) $text;
}
}