Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Int_.php

function Int_::fromString

Constructs an Int node from a string number literal.

Parameters

string $str String number literal (decimal, octal, hex or binary):

array<string, mixed> $attributes Additional attributes:

bool $allowInvalidOctal Whether to allow invalid octal numbers (PHP 5):

Return value

Int_ The constructed LNumber, including kind attribute

1 call to Int_::fromString()
ParserAbstract::parseLNumber in vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php

File

vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Int_.php, line 42

Class

Int_

Namespace

PhpParser\Node\Scalar

Code

public static function fromString(string $str, array $attributes = [], bool $allowInvalidOctal = false) : Int_ {
    $attributes['rawValue'] = $str;
    $str = str_replace('_', '', $str);
    if ('0' !== $str[0] || '0' === $str) {
        $attributes['kind'] = Int_::KIND_DEC;
        return new Int_((int) $str, $attributes);
    }
    if ('x' === $str[1] || 'X' === $str[1]) {
        $attributes['kind'] = Int_::KIND_HEX;
        return new Int_(hexdec($str), $attributes);
    }
    if ('b' === $str[1] || 'B' === $str[1]) {
        $attributes['kind'] = Int_::KIND_BIN;
        return new Int_(bindec($str), $attributes);
    }
    if (!$allowInvalidOctal && strpbrk($str, '89')) {
        throw new Error('Invalid numeric literal', $attributes);
    }
    // Strip optional explicit octal prefix.
    if ('o' === $str[1] || 'O' === $str[1]) {
        $str = substr($str, 2);
    }
    // use intval instead of octdec to get proper cutting behavior with malformed numbers
    $attributes['kind'] = Int_::KIND_OCT;
    return new Int_(intval($str, 8), $attributes);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal