2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2011-06-05 18:40:04 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node\Scalar;
|
|
|
|
|
2016-04-18 13:59:18 +02:00
|
|
|
use PhpParser\Error;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
|
|
|
|
class LNumber extends Scalar
|
2011-06-05 18:40:04 +02:00
|
|
|
{
|
2016-03-09 21:10:55 +01:00
|
|
|
/* For use in "kind" attribute */
|
|
|
|
const KIND_BIN = 2;
|
|
|
|
const KIND_OCT = 8;
|
|
|
|
const KIND_DEC = 10;
|
|
|
|
const KIND_HEX = 16;
|
|
|
|
|
2015-02-28 18:44:28 +01:00
|
|
|
/** @var int Number value */
|
|
|
|
public $value;
|
|
|
|
|
2011-08-09 14:55:45 +02:00
|
|
|
/**
|
|
|
|
* Constructs an integer number scalar node.
|
|
|
|
*
|
2012-04-29 23:32:09 +02:00
|
|
|
* @param int $value Value of the number
|
|
|
|
* @param array $attributes Additional attributes
|
2011-08-09 14:55:45 +02:00
|
|
|
*/
|
2017-08-13 14:06:08 +02:00
|
|
|
public function __construct(int $value, array $attributes = []) {
|
2015-05-02 22:17:34 +02:00
|
|
|
parent::__construct($attributes);
|
2015-02-28 18:44:28 +01:00
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getSubNodeNames() : array {
|
2017-08-13 14:06:08 +02:00
|
|
|
return ['value'];
|
2011-08-09 14:55:45 +02:00
|
|
|
}
|
2011-11-26 16:09:39 +01:00
|
|
|
|
2016-03-10 12:51:47 +01:00
|
|
|
/**
|
|
|
|
* Constructs an LNumber node from a string number literal.
|
|
|
|
*
|
2016-04-18 13:59:18 +02:00
|
|
|
* @param string $str String number literal (decimal, octal, hex or binary)
|
|
|
|
* @param array $attributes Additional attributes
|
|
|
|
* @param bool $allowInvalidOctal Whether to allow invalid octal numbers (PHP 5)
|
2016-03-10 12:51:47 +01:00
|
|
|
*
|
|
|
|
* @return LNumber The constructed LNumber, including kind attribute
|
|
|
|
*/
|
2017-08-13 14:06:08 +02:00
|
|
|
public static function fromString(string $str, array $attributes = [], bool $allowInvalidOctal = false) : LNumber {
|
2016-03-10 13:01:42 +01:00
|
|
|
if ('0' !== $str[0] || '0' === $str) {
|
2016-03-10 12:51:47 +01:00
|
|
|
$attributes['kind'] = LNumber::KIND_DEC;
|
2016-03-10 13:01:42 +01:00
|
|
|
return new LNumber((int) $str, $attributes);
|
2016-03-10 12:51:47 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 13:01:42 +01:00
|
|
|
if ('x' === $str[1] || 'X' === $str[1]) {
|
|
|
|
$attributes['kind'] = LNumber::KIND_HEX;
|
|
|
|
return new LNumber(hexdec($str), $attributes);
|
2011-11-26 16:09:39 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 13:01:42 +01:00
|
|
|
if ('b' === $str[1] || 'B' === $str[1]) {
|
|
|
|
$attributes['kind'] = LNumber::KIND_BIN;
|
|
|
|
return new LNumber(bindec($str), $attributes);
|
2011-11-26 16:09:39 +01:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:59:18 +02:00
|
|
|
if (!$allowInvalidOctal && strpbrk($str, '89')) {
|
|
|
|
throw new Error('Invalid numeric literal', $attributes);
|
|
|
|
}
|
|
|
|
|
2016-03-10 13:01:42 +01:00
|
|
|
// use intval instead of octdec to get proper cutting behavior with malformed numbers
|
|
|
|
$attributes['kind'] = LNumber::KIND_OCT;
|
|
|
|
return new LNumber(intval($str, 8), $attributes);
|
2011-11-26 16:09:39 +01:00
|
|
|
}
|
2017-11-12 21:25:57 +01:00
|
|
|
|
2018-01-10 18:57:48 +01:00
|
|
|
public function getType() : string {
|
2017-11-12 21:25:57 +01:00
|
|
|
return 'Scalar_LNumber';
|
|
|
|
}
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|