1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-11 17:09:41 +01:00
PHP-Parser/lib/PhpParser/Node/Scalar/LNumber.php

61 lines
1.4 KiB
PHP
Raw Normal View History

2011-06-05 18:40:04 +02:00
<?php
namespace PhpParser\Node\Scalar;
use PhpParser\Node\Scalar;
2011-06-05 18:40:04 +02:00
/**
* @property int $value Number value
*/
class LNumber extends Scalar
2011-06-05 18:40:04 +02:00
{
/**
* Constructs an integer number scalar node.
*
* @param int $value Value of the number
* @param array $attributes Additional attributes
*/
public function __construct($value = 0, array $attributes = array()) {
parent::__construct(
array(
'value' => $value
),
$attributes
);
}
/**
2014-09-30 20:23:25 +02:00
* @internal
*
* 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;
}
2011-06-05 18:40:04 +02:00
}