<commit_before><?php

namespace Meng\Soap\HttpBinding;

use Meng\Soap\Interpreter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Stream;

class HttpBinding
{
    private $interpreter;
    private $builder;

    public function __construct(Interpreter $interpreter, RequestBuilder $builder)
    {
        $this->interpreter = $interpreter;
        $this->builder = $builder;
    }

    /**
     * Embed SOAP messages in PSR-7 HTTP Requests
     *
     * @param $name
     * @param array $arguments
     * @param array|null $options
     * @param null $inputHeaders
     * @return RequestInterface
     */
    public function request($name, array $arguments, array $options = null, $inputHeaders = null)
    {
        $soapRequest = $this->interpreter->request($name, $arguments, $options, $inputHeaders);
        if ($soapRequest->getSoapVersion() == '1') {
            $this->builder->isSOAP11();
        } else {
            $this->builder->isSOAP12();
        }
        $this->builder->setEndpoint($soapRequest->getEndpoint());
        $this->builder->setSoapAction($soapRequest->getSoapAction());
        $stream = fopen('php://temp', 'r+');
        fwrite($stream, $soapRequest->getSoapMessage());
        fseek($stream, 0);
        $this->builder->setSoapMessage(new Stream($stream));
        return $this->builder->getSoapHttpRequest();
    }

    /**
     * Retrieve SOAP messages from PSR-7 HTTP responses
     *
     * @param ResponseInterface $response
     * @param $name
     * @param array|null $output_headers
     * @return mixed
     */
    public function response(ResponseInterface $response, $name, array &$output_headers = null)
    {
        return $this->interpreter->response($response->getBody()->__toString(), $name, $output_headers);
    }
}<commit_msg>Use Zend Diactoros stream to replace php native stream operations - it will throw proper errors when write and rewind.<commit_after><?php

namespace Meng\Soap\HttpBinding;

use Meng\Soap\Interpreter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Stream;

class HttpBinding
{
    private $interpreter;
    private $builder;

    public function __construct(Interpreter $interpreter, RequestBuilder $builder)
    {
        $this->interpreter = $interpreter;
        $this->builder = $builder;
    }

    /**
     * Embed SOAP messages in PSR-7 HTTP Requests
     *
     * @param string $name
     * @param array $arguments
     * @param array $options
     * @param mixed $inputHeaders
     * @return RequestInterface
     */
    public function request($name, array $arguments, array $options = null, $inputHeaders = null)
    {
        $soapRequest = $this->interpreter->request($name, $arguments, $options, $inputHeaders);
        if ($soapRequest->getSoapVersion() == '1') {
            $this->builder->isSOAP11();
        } else {
            $this->builder->isSOAP12();
        }
        $this->builder->setEndpoint($soapRequest->getEndpoint());
        $this->builder->setSoapAction($soapRequest->getSoapAction());
        $stream = new Stream('php://temp', 'r+');
        $stream->write($soapRequest->getSoapMessage());
        $stream->rewind();
        $this->builder->setSoapMessage($stream);
        return $this->builder->getSoapHttpRequest();
    }

    /**
     * Retrieve SOAP messages from PSR-7 HTTP responses
     *
     * @param ResponseInterface $response
     * @param string $name
     * @param array $outputHeaders
     * @return mixed
     * @throws \SoapFault
     */
    public function response(ResponseInterface $response, $name, array &$outputHeaders = null)
    {
        return $this->interpreter->response($response->getBody()->__toString(), $name, $outputHeaders);
    }
}
{'fraction_non_alphanumeric': 0.13651967256403486, 'fraction_numerical': 0.003960918933192501, 'mean_word_length': 2.983175604626709, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1506012', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1093, 'n_words': 240}