public static function createStreamFor($resource = '', array $options = []): StreamInterface
    {
        if (\is_scalar($resource)) {
            $stream = self::tryFopen('php://temp', 'r+');

            if ($resource !== '') {
                \fwrite($stream, (string) $resource);
                \fseek($stream, 0);
            }

            return new Stream($stream, $options);
        }

        $type = \gettype($resource);

        if ($type === 'resource') {
            return new Stream($resource, $options);
        }

        if ($type === 'object') {
            if ($resource instanceof StreamInterface) {
                return $resource;
            }

            if ($resource instanceof Iterator) {
                return new PumpStream(static function () use ($resource) {
                    if (! $resource->valid()) {
                        return false;
                    }

                    $result = $resource->current();
                    $resource->next();

                    return $result;
                }, $options);
            }

            if (\method_exists($resource, '__toString')) {
                return self::createStreamFor($resource->__toString(), $options);
            }
        }

        if ($type === 'NULL') {
            return new Stream(self::tryFopen('php://temp', 'r+'), $options);
        }

        if (\is_callable($resource)) {
            return new PumpStream($resource, $options);
        }

        throw new InvalidArgumentException('Invalid resource type: ' . \gettype($resource));
    }