LNumber.php 1.4 KB

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