SimpleResultStream.php 936 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. namespace AsyncAws\Core\Test;
  4. use AsyncAws\Core\Stream\ResultStream;
  5. /**
  6. * Simple streamable body used for testing.
  7. *
  8. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  9. */
  10. class SimpleResultStream implements ResultStream
  11. {
  12. /**
  13. * @var string
  14. */
  15. private $data;
  16. public function __construct(string $data)
  17. {
  18. $this->data = $data;
  19. }
  20. public function getChunks(): iterable
  21. {
  22. yield $this->data;
  23. }
  24. public function getContentAsString(): string
  25. {
  26. return $this->data;
  27. }
  28. public function getContentAsResource()
  29. {
  30. $resource = fopen('php://temp', 'rw+');
  31. try {
  32. fwrite($resource, $this->data);
  33. // Rewind
  34. fseek($resource, 0, \SEEK_SET);
  35. return $resource;
  36. } catch (\Throwable $e) {
  37. fclose($resource);
  38. throw $e;
  39. }
  40. }
  41. }