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

Breadcrumb

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

function JsonManipulator::format

Parameters

mixed $data:

4 calls to JsonManipulator::format()
JsonManipulator::addLink in vendor/composer/composer/src/Composer/Json/JsonManipulator.php
JsonManipulator::addMainKey in vendor/composer/composer/src/Composer/Json/JsonManipulator.php
JsonManipulator::addSubNode in vendor/composer/composer/src/Composer/Json/JsonManipulator.php
JsonManipulator::removeSubNode in vendor/composer/composer/src/Composer/Json/JsonManipulator.php

File

vendor/composer/composer/src/Composer/Json/JsonManipulator.php, line 551

Class

JsonManipulator
@author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Json

Code

public function format($data, int $depth = 0, bool $wasObject = false) : string {
    if ($data instanceof \stdClass || $data instanceof \ArrayObject) {
        $data = (array) $data;
        $wasObject = true;
    }
    if (is_array($data)) {
        if (\count($data) === 0) {
            return $wasObject ? '{' . $this->newline . str_repeat($this->indent, $depth + 1) . '}' : '[]';
        }
        if (array_is_list($data)) {
            foreach ($data as $key => $val) {
                $data[$key] = $this->format($val, $depth + 1);
            }
            return '[' . implode(', ', $data) . ']';
        }
        $out = '{' . $this->newline;
        $elems = [];
        foreach ($data as $key => $val) {
            $elems[] = str_repeat($this->indent, $depth + 2) . JsonFile::encode((string) $key) . ': ' . $this->format($val, $depth + 1);
        }
        return $out . implode(',' . $this->newline, $elems) . $this->newline . str_repeat($this->indent, $depth + 1) . '}';
    }
    return JsonFile::encode($data);
}
RSS feed
Powered by Drupal