2011-06-05 18:40:04 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node\Scalar;
|
|
|
|
|
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
|
|
|
|
class LNumber extends Scalar
|
2011-06-05 18:40:04 +02:00
|
|
|
{
|
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
|
|
|
*/
|
2015-07-12 22:02:18 +02:00
|
|
|
public function __construct($value, array $attributes = array()) {
|
2015-05-02 22:17:34 +02:00
|
|
|
parent::__construct($attributes);
|
2015-02-28 18:44:28 +01:00
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubNodeNames() {
|
|
|
|
return array('value');
|
2011-08-09 14:55:45 +02:00
|
|
|
}
|
2011-11-26 16:09:39 +01:00
|
|
|
|
|
|
|
/**
|
2014-09-30 20:23:25 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2011-11-26 16:09:39 +01:00
|
|
|
* Parses an LNUMBER token (dec, hex, oct and bin notations) like PHP would.
|
|
|
|
*
|
|
|
|
* @param string $str A string number
|
|
|
|
*
|
|
|
|
* @return int The parsed number
|
|
|
|
*/
|
|
|
|
public static function parse($str) {
|
|
|
|
// handle plain 0 specially
|
|
|
|
if ('0' === $str) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if first char is 0 (and number isn't 0) it's a special syntax
|
|
|
|
if ('0' === $str[0]) {
|
|
|
|
// hex
|
|
|
|
if ('x' === $str[1] || 'X' === $str[1]) {
|
|
|
|
return hexdec($str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// bin
|
|
|
|
if ('b' === $str[1] || 'B' === $str[1]) {
|
|
|
|
return bindec($str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// oct (intval instead of octdec to get proper cutting behavior with malformed numbers)
|
|
|
|
return intval($str, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// dec
|
|
|
|
return (int) $str;
|
|
|
|
}
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|