Zend\Diactoros\Stream::write PHP Method

write() public method

public write ( $string )
    public function write($string)
    {
        if (!$this->resource) {
            throw new RuntimeException('No resource available; cannot write');
        }
        if (!$this->isWritable()) {
            throw new RuntimeException('Stream is not writable');
        }
        $result = fwrite($this->resource, $string);
        if (false === $result) {
            throw new RuntimeException('Error writing to stream');
        }
        return $result;
    }

Usage Example

 /**
  * Convert a CakePHP response into a PSR7 one.
  *
  * @param CakeResponse $response The CakePHP response to convert
  * @return PsrResponse $response The equivalent PSR7 response.
  */
 public static function toPsr(CakeResponse $response)
 {
     $status = $response->statusCode();
     $headers = $response->header();
     if (!isset($headers['Content-Type'])) {
         $headers['Content-Type'] = $response->type();
     }
     $body = $response->body();
     $stream = 'php://memory';
     if (is_string($body)) {
         $stream = new Stream('php://memory', 'wb');
         $stream->write($response->body());
     }
     if (is_callable($body)) {
         $stream = new CallbackStream($body);
     }
     // This is horrible, but CakePHP doesn't have a getFile() method just yet.
     $fileProp = new \ReflectionProperty($response, '_file');
     $fileProp->setAccessible(true);
     $file = $fileProp->getValue($response);
     if ($file) {
         $stream = new Stream($file->path, 'rb');
     }
     return new DiactorosResponse($stream, $status, $headers);
 }
All Usage Examples Of Zend\Diactoros\Stream::write