No Description

LNumber.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace PhpParser\Node\Scalar;
  3. use PhpParser\Node\Scalar;
  4. /**
  5. * @property int $value Number value
  6. */
  7. class LNumber extends Scalar
  8. {
  9. /**
  10. * Constructs an integer number scalar node.
  11. *
  12. * @param int $value Value of the number
  13. * @param array $attributes Additional attributes
  14. */
  15. public function __construct($value = 0, array $attributes = array()) {
  16. parent::__construct(
  17. array(
  18. 'value' => $value
  19. ),
  20. $attributes
  21. );
  22. }
  23. /**
  24. * @internal
  25. *
  26. * Parses an LNUMBER token (dec, hex, oct and bin notations) like PHP would.
  27. *
  28. * @param string $str A string number
  29. *
  30. * @return int The parsed number
  31. */
  32. public static function parse($str) {
  33. // handle plain 0 specially
  34. if ('0' === $str) {
  35. return 0;
  36. }
  37. // if first char is 0 (and number isn't 0) it's a special syntax
  38. if ('0' === $str[0]) {
  39. // hex
  40. if ('x' === $str[1] || 'X' === $str[1]) {
  41. return hexdec($str);
  42. }
  43. // bin
  44. if ('b' === $str[1] || 'B' === $str[1]) {
  45. return bindec($str);
  46. }
  47. // oct (intval instead of octdec to get proper cutting behavior with malformed numbers)
  48. return intval($str, 8);
  49. }
  50. // dec
  51. return (int) $str;
  52. }
  53. }