PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/LNumber.php

https://bitbucket.org/alan_cordova/api-sb-map
PHP | 67 lines | 38 code | 11 blank | 18 comment | 8 complexity | 707c707045b5053b7452ddc2338a2dc3 MD5 | raw file
 1<?php
 2
 3namespace PhpParser\Node\Scalar;
 4
 5use PhpParser\Error;
 6use PhpParser\Node\Scalar;
 7
 8class LNumber extends Scalar
 9{
10    /* For use in "kind" attribute */
11    const KIND_BIN = 2;
12    const KIND_OCT = 8;
13    const KIND_DEC = 10;
14    const KIND_HEX = 16;
15
16    /** @var int Number value */
17    public $value;
18
19    /**
20     * Constructs an integer number scalar node.
21     *
22     * @param int   $value      Value of the number
23     * @param array $attributes Additional attributes
24     */
25    public function __construct($value, array $attributes = array()) {
26        parent::__construct($attributes);
27        $this->value = $value;
28    }
29
30    public function getSubNodeNames() {
31        return array('value');
32    }
33
34    /**
35     * Constructs an LNumber node from a string number literal.
36     *
37     * @param string $str               String number literal (decimal, octal, hex or binary)
38     * @param array  $attributes        Additional attributes
39     * @param bool   $allowInvalidOctal Whether to allow invalid octal numbers (PHP 5)
40     *
41     * @return LNumber The constructed LNumber, including kind attribute
42     */
43    public static function fromString($str, array $attributes = array(), $allowInvalidOctal = false) {
44        if ('0' !== $str[0] || '0' === $str) {
45            $attributes['kind'] = LNumber::KIND_DEC;
46            return new LNumber((int) $str, $attributes);
47        }
48
49        if ('x' === $str[1] || 'X' === $str[1]) {
50            $attributes['kind'] = LNumber::KIND_HEX;
51            return new LNumber(hexdec($str), $attributes);
52        }
53
54        if ('b' === $str[1] || 'B' === $str[1]) {
55            $attributes['kind'] = LNumber::KIND_BIN;
56            return new LNumber(bindec($str), $attributes);
57        }
58
59        if (!$allowInvalidOctal && strpbrk($str, '89')) {
60            throw new Error('Invalid numeric literal', $attributes);
61        }
62
63        // use intval instead of octdec to get proper cutting behavior with malformed numbers
64        $attributes['kind'] = LNumber::KIND_OCT;
65        return new LNumber(intval($str, 8), $attributes);
66    }
67}