You are here

public function StreamEncoder::decode in Replication 8

Same name and namespace in other branches
  1. 8.2 src/Encoder/StreamEncoder.php \Drupal\replication\Encoder\StreamEncoder::decode()

File

src/Encoder/StreamEncoder.php, line 39

Class

StreamEncoder

Namespace

Drupal\replication\Encoder

Code

public function decode($data, $format, array $context = []) {
  if (!is_scalar($data)) {
    throw new \InvalidArgumentException(sprintf('Data argument is not a scalar.'));
  }
  $uri = !empty($context['uri']) ? $context['uri'] : 'temporary://' . $this->random
    ->name();
  $mode = !empty($context['mode']) ? $context['mode'] : 'w+b';
  $stream = fopen($uri, $mode);
  $data = $format == 'base64_stream' ? base64_decode($data) : $data;
  fwrite($stream, $data);

  // Put the file pointer back to the beginning.
  rewind($stream);
  return $stream;
}