<commit_before><?php

namespace Quazardous\PriceministerWs\Response;

use Quazardous\PriceministerWs\RuntimeException;

class BasicResponse {
    protected $rawBody = null;
    protected $rawHeaders = null;
    
    public function __construct($rawHeaders, $rawBody) {
        $this->rawHeaders = $rawHeaders;
        $this->rawBody = $rawBody;
    }
    
    public function getRawHeaders() {
        return  $this->rawHeaders;
    }
    
    public function getRawBody() {
        return  $this->rawBody;
    }
    
    protected $xml = null;
    /**
     * Return the body as SimpleXMLElement.
     * @throws \Quazardous\PriceministerWs\RuntimeException
     * @return SimpleXMLElement
     */
    public function getBodyAsSimpleXmlElement() {
        if (!$this->xml) {
        
            $body = $this->rawBody;
            $start = substr($body, 0, 256);
            
            $encoding = 'UTF-8';
            $xml_tag_size = false;
            $matches = null;
            if (preg_match( '@(<\?xml[^>]+encoding="([^\s"]+)[^?]*\?>)@si', $start, $matches )) {
                $encoding = $matches[2];
                $xml_tag_size = strlen($matches[1]);
            }
            
            if ($xml_tag_size !== false) {
                $body = '<?xml version="1.0" encoding="UTF-8"?>'.substr($body, $xml_tag_size);
            }
            
            $body = iconv($encoding, 'UTF-8//TRANSLIT', $body);
            $xml = simplexml_load_string($body);
            if ($xml === false) {
                throw new RuntimeException('Response content is no valid XML', RuntimeException::NO_VALID_XML);
            }
            
            $this->xml = $xml;
        }
        return  $this->xml;
    }
    
    protected $headers = null;
    /**
     * Return the headers as associative array or return the given header.
     * @param string $key
     * @return string|string[]
     */
    public function getHeaders($key = null) {
        if (!$this->headers) {
            $this->headers = http_parse_headers ( $this->rawHeaders );
        }
        if ($key) {
            if (isset($this->headers[$key])) {
                return $this->headers[$key];
            }
        }
        return $this->headers;
    }
}<commit_msg>add callback to cleanup the body before XML parsing<commit_after><?php

namespace Quazardous\PriceministerWs\Response;

use Quazardous\PriceministerWs\RuntimeException;

class BasicResponse {
    protected $rawBody = null;
    protected $rawHeaders = null;
    
    public function __construct($rawHeaders, $rawBody) {
        $this->rawHeaders = $rawHeaders;
        $this->rawBody = $rawBody;
    }
    
    public function getRawHeaders() {
        return  $this->rawHeaders;
    }
    
    public function getRawBody() {
        return  $this->rawBody;
    }
    
    protected $xml = null;
    /**
     * Return the body as SimpleXMLElement.
     * @throws \Quazardous\PriceministerWs\RuntimeException
     * @return SimpleXMLElement
     */
    public function getBodyAsSimpleXmlElement($bodyCallback = null) {
        if (!$this->xml) {
        
            $body = $this->rawBody;
            $start = substr($body, 0, 256);
            
            $encoding = 'UTF-8';
            $xml_tag_size = false;
            $matches = null;
            if (preg_match( '@(<\?xml[^>]+encoding="([^\s"]+)[^?]*\?>)@si', $start, $matches )) {
                $encoding = $matches[2];
                $xml_tag_size = strlen($matches[1]);
            }
            
            if ($xml_tag_size !== false) {
                $body = '<?xml version="1.0" encoding="UTF-8"?>'.substr($body, $xml_tag_size);
            }
            
            $body = iconv($encoding, 'UTF-8//TRANSLIT', $body);
            if ($bodyCallback) {
                $body = call_user_func($bodyCallback, $body);
            }
            $xml = simplexml_load_string($body);
            if ($xml === false) {
                throw new RuntimeException('Response content is no valid XML', RuntimeException::NO_VALID_XML);
            }
            
            $this->xml = $xml;
        }
        return  $this->xml;
    }
    
    protected $headers = null;
    /**
     * Return the headers as associative array or return the given header.
     * @param string $key
     * @return string|string[]
     */
    public function getHeaders($key = null) {
        if (!$this->headers) {
            $this->headers = http_parse_headers ( $this->rawHeaders );
        }
        if ($key) {
            if (isset($this->headers[$key])) {
                return $this->headers[$key];
            }
        }
        return $this->headers;
    }
}
{'fraction_non_alphanumeric': 0.13715389568576947, 'fraction_numerical': 0.00472204335694355, 'mean_word_length': 1.785415421398685, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 2, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '6694739', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1338, 'n_words': 284}