public function setResource($resource = 'php://temp', $mode = 'w+b')
    {
        if (is_resource($this->resource)) {
            throw new DomainException('The resource is already set.');
        }

        if (is_resource($resource)) {
            $this->resource = $resource;
        } elseif (is_string($resource)) {
            set_error_handler(function () {
                throw new Exception('Failed to open stream.');
            }, E_WARNING);
            try {
                $this->resource = fopen($resource, $mode);
            } catch (Exception $ex) {
                restore_error_handler();
                throw new InvalidArgumentException(sprintf(
                    'Invalid path "%s" specified.',
                    $resource
                ));
            }
            restore_error_handler();
        } else {
            throw new InvalidArgumentException(sprintf(
                'Invalid resource "%s" provided.',
                is_object($resource) ? get_class($resource) : gettype($resource)
            ));
        }

        return $this;
    }