You are here

protected function JsonPathReplacer::replaceItem in Subrequests 8.2

Same name and namespace in other branches
  1. 3.x src/JsonPathReplacer.php \Drupal\subrequests\JsonPathReplacer::replaceItem()

Searches for JSONPath tokens in the request and replaces it with the values from previous responses.

@returns \Drupal\subrequests\Subrequest[] The new list of requests. Note that if a JSONPath token yields many values then several replaced subrequests will be generated from the input subrequest.

Parameters

\Drupal\subrequests\Subrequest $subrequest: The list of requests that can contain tokens.

\Symfony\Component\HttpFoundation\Response[] $pool: The pool of responses that can content the values to replace.

1 call to JsonPathReplacer::replaceItem()
JsonPathReplacer::replaceBatch in src/JsonPathReplacer.php
Performs the JSON Path replacements in the whole batch.

File

src/JsonPathReplacer.php, line 48

Class

JsonPathReplacer

Namespace

Drupal\subrequests

Code

protected function replaceItem(Subrequest $subrequest, array $pool) {
  $token_replacements = [
    'uri' => $this
      ->extractTokenReplacements($subrequest, 'uri', $pool),
    'body' => $this
      ->extractTokenReplacements($subrequest, 'body', $pool),
  ];
  if (count($token_replacements['uri']) !== 0) {
    return $this
      ->replaceBatch($this
      ->doReplaceTokensInLocation($token_replacements, $subrequest, 'uri'), $pool);
  }
  if (count($token_replacements['body']) !== 0) {
    return $this
      ->replaceBatch($this
      ->doReplaceTokensInLocation($token_replacements, $subrequest, 'body'), $pool);
  }

  // If there are no replacements necessary, then just return the initial
  // request.
  $subrequest->_resolved = TRUE;
  return [
    $subrequest,
  ];
}