1<?php declare(strict_types=1); 2 3namespace PhpParser\Node\Scalar; 4 5use PhpParser\Error; 6use PhpParser\Node\Scalar; 7 8class Int_ extends Scalar { 9 /* For use in "kind" attribute */ 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 /** @var int Number value */ 16 public int $value; 17 18 /** 19 * Constructs an integer number scalar node. 20 * 21 * @param int $value Value of the number 22 * @param array<string, mixed> $attributes Additional attributes 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 * Constructs an Int node from a string number literal. 35 * 36 * @param string $str String number literal (decimal, octal, hex or binary) 37 * @param array<string, mixed> $attributes Additional attributes 38 * @param bool $allowInvalidOctal Whether to allow invalid octal numbers (PHP 5) 39 * 40 * @return Int_ The constructed LNumber, including kind attribute 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 // Strip optional explicit octal prefix. 67 if ('o' === $str[1] || 'O' === $str[1]) { 68 $str = substr($str, 2); 69 } 70 71 // use intval instead of octdec to get proper cutting behavior with malformed numbers 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// @deprecated compatibility alias 82class_alias(Int_::class, LNumber::class); 83