You are here

protected function JsonPathReplacer::doReplaceTokensInLocation in Subrequests 8.2

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

Creates replacements for either the body or the URI.

@returns \Drupal\subrequests\Subrequest[] The replaced subrequests.

@private

Parameters

array $token_replacements: Holds the info to replace text.

\Drupal\subrequests\Subrequest $tokenized_subrequest: The original copy of the subrequest.

string $token_location: Either 'body' or 'uri'.

1 call to JsonPathReplacer::doReplaceTokensInLocation()
JsonPathReplacer::replaceItem in src/JsonPathReplacer.php
Searches for JSONPath tokens in the request and replaces it with the values from previous responses.

File

src/JsonPathReplacer.php, line 86

Class

JsonPathReplacer

Namespace

Drupal\subrequests

Code

protected function doReplaceTokensInLocation(array $token_replacements, $tokenized_subrequest, $token_location) {
  $replacements = [];
  $tokens_per_content_id = $token_replacements[$token_location];
  $index = 0;

  // First figure out the different token resolutions and their token.
  $grouped_by_token = [];
  foreach ($tokens_per_content_id as $resolutions_per_token) {
    foreach ($resolutions_per_token as $token => $resolutions) {
      $grouped_by_token[] = array_map(function ($resolution) use ($token) {
        return [
          'token' => $token,
          'value' => $resolution,
        ];
      }, $resolutions);
    }
  }

  // Then calculate the points.
  $points = $this
    ->getPoints($grouped_by_token);
  foreach ($points as $point) {

    // Clone the subrequest.
    $cloned = clone $tokenized_subrequest;
    $cloned->requestId = sprintf('%s#%s{%s}', $tokenized_subrequest->requestId, $token_location, $index);
    $index++;

    // Now replace all the tokens in the request member.
    $token_subject = $this
      ->serializeMember($token_location, $cloned->{$token_location});
    foreach ($point as $replacement) {

      // Do all the different replacements on the same subject.
      $token_subject = $this
        ->replaceTokenSubject($replacement['token'], $replacement['value'], $token_subject);
    }
    $cloned->{$token_location} = $this
      ->deserializeMember($token_location, $token_subject);
    array_push($replacements, $cloned);
  }
  return $replacements;
}