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;
|
|
|
|
|
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
|
|
|
|
class DNumber extends Scalar
|
2011-06-05 18:40:04 +02:00
|
|
|
{
|
2015-02-28 18:44:28 +01:00
|
|
|
/** @var float Number value */
|
|
|
|
public $value;
|
|
|
|
|
2011-08-09 14:55:45 +02:00
|
|
|
/**
|
|
|
|
* Constructs a float number scalar node.
|
|
|
|
*
|
2012-04-29 23:32:09 +02:00
|
|
|
* @param float $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(float $value, array $attributes = []) {
|
2019-05-12 14:55:21 +02:00
|
|
|
$this->attributes = $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
|
|
|
}
|
2012-01-15 16:28:25 +01:00
|
|
|
|
|
|
|
/**
|
2014-09-30 20:23:25 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2012-01-15 16:28:25 +01:00
|
|
|
* Parses a DNUMBER token like PHP would.
|
|
|
|
*
|
|
|
|
* @param string $str A string number
|
|
|
|
*
|
|
|
|
* @return float The parsed number
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public static function parse(string $str) : float {
|
2019-06-30 12:13:28 +02:00
|
|
|
$str = str_replace('_', '', $str);
|
|
|
|
|
2012-01-15 16:28:25 +01:00
|
|
|
// if string contains any of .eE just cast it to float
|
|
|
|
if (false !== strpbrk($str, '.eE')) {
|
|
|
|
return (float) $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise it's an integer notation that overflowed into a float
|
|
|
|
// if it starts with 0 it's one of the special integer notations
|
|
|
|
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
|
|
|
|
// substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit (8 or 9)
|
|
|
|
// so that only the digits before that are used
|
|
|
|
return octdec(substr($str, 0, strcspn($str, '89')));
|
|
|
|
}
|
|
|
|
|
|
|
|
// dec
|
|
|
|
return (float) $str;
|
|
|
|
}
|
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_DNumber';
|
|
|
|
}
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|