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

Breadcrumb

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

function BuilderHelpers::normalizeValue

Normalizes a value: Converts nulls, booleans, integers, floats, strings and arrays into their respective nodes

Parameters

Node\Expr|bool|null|int|float|string|array|\UnitEnum $value The value to normalize:

Return value

Expr The normalized value

7 calls to BuilderHelpers::normalizeValue()
BuilderFactory::args in vendor/nikic/php-parser/lib/PhpParser/BuilderFactory.php
Normalizes an argument list.
BuilderFactory::val in vendor/nikic/php-parser/lib/PhpParser/BuilderFactory.php
Creates node a for a literal value.
ClassConst::addConst in vendor/nikic/php-parser/lib/PhpParser/Builder/ClassConst.php
Add another constant to const group
ClassConst::__construct in vendor/nikic/php-parser/lib/PhpParser/Builder/ClassConst.php
Creates a class constant builder
EnumCase::setValue in vendor/nikic/php-parser/lib/PhpParser/Builder/EnumCase.php
Sets the value.

... See full list

File

vendor/nikic/php-parser/lib/PhpParser/BuilderHelpers.php, line 222

Class

BuilderHelpers
This class defines helpers used in the implementation of builders. Don't use it directly.

Namespace

PhpParser

Code

public static function normalizeValue($value) : Expr {
    if ($value instanceof Node\Expr) {
        return $value;
    }
    if (is_null($value)) {
        return new Expr\ConstFetch(new Name('null'));
    }
    if (is_bool($value)) {
        return new Expr\ConstFetch(new Name($value ? 'true' : 'false'));
    }
    if (is_int($value)) {
        return new Scalar\Int_($value);
    }
    if (is_float($value)) {
        return new Scalar\Float_($value);
    }
    if (is_string($value)) {
        return new Scalar\String_($value);
    }
    if (is_array($value)) {
        $items = [];
        $lastKey = -1;
        foreach ($value as $itemKey => $itemValue) {
            // for consecutive, numeric keys don't generate keys
            if (null !== $lastKey && ++$lastKey === $itemKey) {
                $items[] = new Node\ArrayItem(self::normalizeValue($itemValue));
            }
            else {
                $lastKey = null;
                $items[] = new Node\ArrayItem(self::normalizeValue($itemValue), self::normalizeValue($itemKey));
            }
        }
        return new Expr\Array_($items);
    }
    if ($value instanceof \UnitEnum) {
        return new Expr\ClassConstFetch(new FullyQualified(\get_class($value)), new Identifier($value->name));
    }
    throw new \LogicException('Invalid value');
}

API Navigation

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