| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser\Node\Scalar; |
| 4: | |
| 5: | use PhpParser\Node\Scalar; |
| 6: | |
| 7: | class Float_ extends Scalar { |
| 8: | |
| 9: | public float $value; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | public function __construct(float $value, array $attributes = []) { |
| 18: | $this->attributes = $attributes; |
| 19: | $this->value = $value; |
| 20: | } |
| 21: | |
| 22: | public function getSubNodeNames(): array { |
| 23: | return ['value']; |
| 24: | } |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | public static function fromString(string $str, array $attributes = []): Float_ { |
| 30: | $attributes['rawValue'] = $str; |
| 31: | $float = self::parse($str); |
| 32: | |
| 33: | return new Float_($float, $attributes); |
| 34: | } |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public static function parse(string $str): float { |
| 46: | $str = str_replace('_', '', $str); |
| 47: | |
| 48: | |
| 49: | if ('0' === $str[0]) { |
| 50: | |
| 51: | if ('x' === $str[1] || 'X' === $str[1]) { |
| 52: | return hexdec($str); |
| 53: | } |
| 54: | |
| 55: | |
| 56: | if ('b' === $str[1] || 'B' === $str[1]) { |
| 57: | return bindec($str); |
| 58: | } |
| 59: | |
| 60: | |
| 61: | if (false === strpbrk($str, '.eE')) { |
| 62: | |
| 63: | |
| 64: | return octdec(substr($str, 0, strcspn($str, '89'))); |
| 65: | } |
| 66: | } |
| 67: | |
| 68: | |
| 69: | return (float) $str; |
| 70: | } |
| 71: | |
| 72: | public function getType(): string { |
| 73: | return 'Scalar_Float'; |
| 74: | } |
| 75: | } |
| 76: | |
| 77: | |
| 78: | class_alias(Float_::class, DNumber::class); |
| 79: | |