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