<commit_before><?php namespace Scriptotek\Sru;
 
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;

/**
 * SRU response, containing a list of records or some error
 */
class Response {

    /** @var string Raw XML response */
    protected $rawResponse;

    /** @var Client Reference to SRU client object */
    protected $client;

    /** @var Record[] Array of records */
    public $records;

    /** @var string Error message */
    public $error;

    /** @var string SRU protocol version */
    public $version;

    /** @var int Total number of records in the result set */
    public $numberOfRecords;

    /** @var int Position of next record in the result set, or null if no such record exist */
    public $nextRecordPosition;

    /** @var string The CQL query used to generate the response */
    public $query;

    /**
     * Create a new response
     *
     * @param string $text Raw XML response
     * @param Client $client SRU client reference (optional)
     */
    public function __construct($text, &$client = null)
    {
        $this->rawResponse = $text; 
        try {
            $doc = new QuiteSimpleXMLElement($text);
        } catch (\Exception $e) {
            throw new \Exception('Invalid XML received');
        }

        $doc->registerXPathNamespaces(array(
            'srw' => 'http://www.loc.gov/zing/srw/',
            'd' => 'http://www.loc.gov/zing/srw/diagnostic/'
        ));

        $this->version = $doc->text('/srw:searchRetrieveResponse/srw:version');
        $this->numberOfRecords = (int) $doc->text('/srw:searchRetrieveResponse/srw:numberOfRecords');
        $this->nextRecordPosition = (int) $doc->text('/srw:searchRetrieveResponse/srw:nextRecordPosition') ?: null;

        $this->records = array();
        foreach ($doc->xpath('/srw:searchRetrieveResponse/srw:records/srw:record') as $record) {
            $this->records[] = new Record($record);
        }

        $this->client = $client;

        // The server may echo the request back to the client along with the response
        $this->query = $doc->text('/srw:searchRetrieveResponse/srw:echoedSearchRetrieveRequest/srw:query') ?: null;

    }

    /**
     * Get the raw xml response
     *
     * @return string
     */
    public function asXml()<commit_msg>Fix so response includes error message<commit_after><?php namespace Scriptotek\Sru;
 
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;

/**
 * SRU response, containing a list of records or some error
 */
class Response {

    /** @var string Raw XML response */
    protected $rawResponse;

    /** @var Client Reference to SRU client object */
    protected $client;

    /** @var Record[] Array of records */
    public $records;

    /** @var string Error message */
    public $error;

    /** @var string SRU protocol version */
    public $version;

    /** @var int Total number of records in the result set */
    public $numberOfRecords;

    /** @var int Position of next record in the result set, or null if no such record exist */
    public $nextRecordPosition;

    /** @var string The CQL query used to generate the response */
    public $query;

    /**
     * Create a new response
     *
     * @param string $text Raw XML response
     * @param Client $client SRU client reference (optional)
     */
    public function __construct($text, &$client = null)
    {
        $this->rawResponse = $text; 
        try {
            $doc = new QuiteSimpleXMLElement($text);
        } catch (\Exception $e) {
            throw new \Exception('Invalid XML received');
        }

        $doc->registerXPathNamespaces(array(
            'srw' => 'http://www.loc.gov/zing/srw/',
            'd' => 'http://www.loc.gov/zing/srw/diagnostic/'
        ));

        $this->version = $doc->text('/srw:searchRetrieveResponse/srw:version');
        $this->numberOfRecords = (int) $doc->text('/srw:searchRetrieveResponse/srw:numberOfRecords');
        $this->nextRecordPosition = (int) $doc->text('/srw:searchRetrieveResponse/srw:nextRecordPosition') ?: null;

        $this->records = array();
        foreach ($doc->xpath('/srw:searchRetrieveResponse/srw:records/srw:record') as $record) {
            $this->records[] = new Record($record);
        }

        $e = $doc->first('/srw:searchRetrieveResponse/srw:diagnostics');
        if ($e) {
            $this->error = $e->text('d:diagnostic/d:messsage') . ' : ' . $e->text('d:diagnostic/d:details'); 
        }

        $this->client = $client;

        // The server may echo the request back to the client along with the response
        $this->query = $doc->text('/srw:searchRetrieveResponse/srw:echoedSearchRetrieveRequest/srw:query') ?: null;

    }

    /**
     * Get the raw xml response
     *
     * @return string
     */
    public function asXml()
{'fraction_non_alphanumeric': 0.135202492211838, 'fraction_numerical': 0.0, 'mean_word_length': 2.551622418879056, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '2922365', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1365, 'n_words': 379}