Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Streams to be instantiated using GD resources #45

Merged
7 changes: 4 additions & 3 deletions docs/book/v2/api.md
Expand Up @@ -174,10 +174,11 @@ prefixed with `with` and `without` — all return a new instance with the ch
`Laminas\Diactoros\Stream` is an implementation of
[`Psr\Http\Message\StreamInterface`](https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php),
and provides a number of facilities around manipulating the composed PHP stream resource. The
constructor accepts a stream, which may be either:
constructor accepts a stream, which may be one of:

- a stream identifier; e.g., `php://input`, a filename, etc.
- a PHP stream resource
- A stream identifier; e.g., `php://input`, a filename, etc.
- A PHP stream resource; or
- A GD resource

If a stream identifier is provided, an optional second parameter may be provided, the file mode by
which to `fopen` the stream.
Expand Down
20 changes: 19 additions & 1 deletion src/Stream.php
Expand Up @@ -40,6 +40,11 @@
*/
class Stream implements StreamInterface
{
/**
* A list of allowed stream resource types that are allowed to instantiate a Stream
*/
private const ALLOWED_STREAM_RESOURCE_TYPES = ['gd', 'stream'];

/**
* @var resource|null
*/
Expand Down Expand Up @@ -345,7 +350,7 @@ private function setStream($stream, string $mode = 'r') : void
throw new Exception\InvalidArgumentException('Invalid stream reference provided');
}

if (! is_resource($resource) || 'stream' !== get_resource_type($resource)) {
if (! $this->isValidStreamResourceType($resource)) {
throw new Exception\InvalidArgumentException(
'Invalid stream provided; must be a string stream identifier or stream resource'
);
Expand All @@ -357,4 +362,17 @@ private function setStream($stream, string $mode = 'r') : void

$this->resource = $resource;
}

/**
* Determine if a resource is one of the resource types allowed to instantiate a Stream
*
* @param resource $resource Stream resource.
*/
private function isValidStreamResourceType($resource): bool
{
return (
is_resource($resource) &&
in_array(get_resource_type($resource), self::ALLOWED_STREAM_RESOURCE_TYPES, true)
);
}
}
5 changes: 0 additions & 5 deletions src/StreamFactory.php
Expand Up @@ -46,11 +46,6 @@ public function createStreamFromFile(string $file, string $mode = 'r') : StreamI
*/
public function createStreamFromResource($resource) : StreamInterface
{
if (! is_resource($resource) || 'stream' !== get_resource_type($resource)) {
throw new Exception\InvalidArgumentException(
'Invalid stream provided; must be a stream resource'
);
}
return new Stream($resource);
}
}
7 changes: 7 additions & 0 deletions test/StreamTest.php
Expand Up @@ -72,6 +72,13 @@ public function testCanInstantiteWithStreamResource()
$this->assertInstanceOf(Stream::class, $stream);
}

public function testCanInstantiateWithGDResource()
{
$resource = imagecreate(1, 1);
$stream = new Stream($resource);
$this->assertInstanceOf(Stream::class, $stream);
}

public function testIsReadableReturnsFalseIfStreamIsNotReadable()
{
$this->tmpnam = tempnam(sys_get_temp_dir(), 'diac');
Expand Down