No Description

DNumber.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace PhpParser\Node\Scalar;
  3. use PhpParser\Node\Scalar;
  4. /**
  5. * @property float $value Number value
  6. */
  7. class DNumber extends Scalar
  8. {
  9. /**
  10. * Constructs a float number scalar node.
  11. *
  12. * @param float $value Value of the number
  13. * @param array $attributes Additional attributes
  14. */
  15. public function __construct($value = 0.0, array $attributes = array()) {
  16. parent::__construct(
  17. array(
  18. 'value' => $value
  19. ),
  20. $attributes
  21. );
  22. }
  23. /**
  24. * @internal
  25. *
  26. * Parses a DNUMBER token like PHP would.
  27. *
  28. * @param string $str A string number
  29. *
  30. * @return float The parsed number
  31. */
  32. public static function parse($str) {
  33. // if string contains any of .eE just cast it to float
  34. if (false !== strpbrk($str, '.eE')) {
  35. return (float) $str;
  36. }
  37. // otherwise it's an integer notation that overflowed into a float
  38. // if it starts with 0 it's one of the special integer notations
  39. if ('0' === $str[0]) {
  40. // hex
  41. if ('x' === $str[1] || 'X' === $str[1]) {
  42. return hexdec($str);
  43. }
  44. // bin
  45. if ('b' === $str[1] || 'B' === $str[1]) {
  46. return bindec($str);
  47. }
  48. // oct
  49. // substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit (8 or 9)
  50. // so that only the digits before that are used
  51. return octdec(substr($str, 0, strcspn($str, '89')));
  52. }
  53. // dec
  54. return (float) $str;
  55. }
  56. }