1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\Node\Scalar; |
4: | |
5: | use PhpParser\Error; |
6: | use PhpParser\Node\Scalar; |
7: | |
8: | class LNumber extends Scalar |
9: | { |
10: | |
11: | const KIND_BIN = 2; |
12: | const KIND_OCT = 8; |
13: | const KIND_DEC = 10; |
14: | const KIND_HEX = 16; |
15: | |
16: | |
17: | public $value; |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | public function __construct(int $value, array $attributes = []) { |
26: | $this->attributes = $attributes; |
27: | $this->value = $value; |
28: | } |
29: | |
30: | public function getSubNodeNames() : array { |
31: | return ['value']; |
32: | } |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | public static function fromString(string $str, array $attributes = [], bool $allowInvalidOctal = false) : LNumber { |
44: | $attributes['rawValue'] = $str; |
45: | |
46: | $str = str_replace('_', '', $str); |
47: | |
48: | if ('0' !== $str[0] || '0' === $str) { |
49: | $attributes['kind'] = LNumber::KIND_DEC; |
50: | return new LNumber((int) $str, $attributes); |
51: | } |
52: | |
53: | if ('x' === $str[1] || 'X' === $str[1]) { |
54: | $attributes['kind'] = LNumber::KIND_HEX; |
55: | return new LNumber(hexdec($str), $attributes); |
56: | } |
57: | |
58: | if ('b' === $str[1] || 'B' === $str[1]) { |
59: | $attributes['kind'] = LNumber::KIND_BIN; |
60: | return new LNumber(bindec($str), $attributes); |
61: | } |
62: | |
63: | if (!$allowInvalidOctal && strpbrk($str, '89')) { |
64: | throw new Error('Invalid numeric literal', $attributes); |
65: | } |
66: | |
67: | |
68: | if ('o' === $str[1] || 'O' === $str[1]) { |
69: | $str = substr($str, 2); |
70: | } |
71: | |
72: | |
73: | $attributes['kind'] = LNumber::KIND_OCT; |
74: | return new LNumber(intval($str, 8), $attributes); |
75: | } |
76: | |
77: | public function getType() : string { |
78: | return 'Scalar_LNumber'; |
79: | } |
80: | } |
81: | |