ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Streams.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 
5 
7 
19 final class Streams
20 {
21 
30  public static function ofString($string) : FileStream
31  {
32  if (!is_string($string)) {
33  throw new \InvalidArgumentException('The argument $string must be of type string but was "' . gettype($string) . '"');
34  }
35 
36  $stream = new Stream(fopen('php://memory', 'rw'));
37  $stream->write($string);
38  return $stream;
39  }
40 
41 
52  public static function ofResource($resource) : FileStream
53  {
54  if (!is_resource($resource)) {
55  throw new \InvalidArgumentException('The argument $resource must be of type resource but was "' . gettype($resource) . '"');
56  }
57 
58  return new Stream($resource);
59  }
60 
61 
69  public static function ofPsr7Stream(StreamInterface $stream) : FileStream
70  {
71  $resource = $stream->detach();
72  return self::ofResource($resource);
73  }
74 }
detach()
Separates any underlying resources from the stream.
$stream
PHP stream implementation.
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:52
static ofString($string)
Creates a new stream with an initial value.
Definition: Streams.php:30
Interface FileStream.
Definition: FileStream.php:20
static ofPsr7Stream(StreamInterface $stream)
Create a FileStream from a Psr7 compliant stream.
Definition: Streams.php:69
Describes a data stream.