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

Breadcrumb

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

function JsonManipulator::removeMainKey

2 calls to JsonManipulator::removeMainKey()
JsonManipulator::removeMainKeyIfEmpty in vendor/composer/composer/src/Composer/Json/JsonManipulator.php
JsonManipulator::removeProperty in vendor/composer/composer/src/Composer/Json/JsonManipulator.php

File

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

Class

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

Namespace

Composer\Json

Code

public function removeMainKey(string $key) : bool {
    $decoded = JsonFile::parseJson($this->contents);
    if (!array_key_exists($key, $decoded)) {
        return true;
    }
    // key exists already
    $regex = '{' . self::DEFINES . '^(?P<start>\\s*\\{\\s*(?:(?&string)\\s*:\\s*(?&json)\\s*,\\s*)*?)' . '(?P<removal>' . preg_quote(JsonFile::encode($key)) . '\\s*:\\s*(?&json))\\s*,?\\s*(?P<end>.*)}sx';
    if (Preg::isMatch($regex, $this->contents, $matches)) {
        assert(is_string($matches['start']));
        assert(is_string($matches['removal']));
        assert(is_string($matches['end']));
        // invalid match due to un-regexable content, abort
        if (!@json_decode('{' . $matches['removal'] . '}')) {
            return false;
        }
        // check that we are not leaving a dangling comma on the previous line if the last line was removed
        if (Preg::isMatchStrictGroups('#,\\s*$#', $matches['start']) && Preg::isMatch('#^\\}$#', $matches['end'])) {
            $matches['start'] = rtrim(Preg::replace('#,(\\s*)$#', '$1', $matches['start']), $this->indent);
        }
        $this->contents = $matches['start'] . $matches['end'];
        if (Preg::isMatch('#^\\{\\s*\\}\\s*$#', $this->contents)) {
            $this->contents = "{\n}";
        }
        return true;
    }
    return false;
}
RSS feed
Powered by Drupal