Completed
Push — master ( 7e068f...d722f0 )
by Dan Michael O.
10:27
created

ExplainResponse::parseExplainResponse()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 3
eloc 22
nc 3
nop 1
1
<?php namespace Scriptotek\Sru;
2
3
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
4
5
/**
6
 * Explain response
7
 */
8
class ExplainResponse extends Response implements ResponseInterface
9
{
10
    /** @var string Server hostname */
11
    public $host;
12
13
    /** @var int Server port */
14
    public $port;
15
16
    /** @var object Server database */
17
    public $database;
18
19
    /** @var array Server indexes */
20
    public $indexes;
21
22
    /**
23
     * Create a new explain response
24
     *
25
     * @param string $text Raw XML response
26
     * @param Client $client SRU client reference (optional)
27
     */
28
    public function __construct($text = null, &$client = null)
29
    {
30
        parent::__construct($text, $client);
31
32
        $this->indexes = array();
33
34
        if (is_null($this->response)) {
35
            return;
36
        }
37
        $explain = $this->response->first('/srw:explainResponse/srw:record/srw:recordData/exp:explain');
38
        if (!$explain) {
39
            return;
40
        }
41
42
        $this->parseExplainResponse($explain);
43
    }
44
45
    protected function parseExplainResponse(QuiteSimpleXMLElement $node)
46
    {
47
        $serverInfo = $node->first('exp:serverInfo');
48
        $dbInfo = $node->first('exp:databaseInfo');
49
        $indexInfo = $node->first('exp:indexInfo');
50
51
        $this->host = $serverInfo->text('exp:host');
52
        $this->port = (int) $serverInfo->text('exp:port');
53
        $this->database = new \StdClass;
54
        $this->database->identifier = $serverInfo->text('exp:database');
55
        $this->database->title = $dbInfo->text('exp:title');
56
        $this->database->description = $dbInfo->text('exp:description');
57
58
        foreach ($indexInfo->xpath('exp:index') as $index) {
59
            $ind = new \StdClass;
60
            $ind->scan = ($index->attr('scan') == 'true');
61
            $ind->search = ($index->attr('search') == 'true');
62
            $ind->sort = ($index->attr('sort') == 'true');
63
            $ind->title = $index->text('exp:title');
64
            $ind->maps = array();
65
            foreach ($index->xpath('exp:map') as $map) {
66
                $set = $map->first('exp:name')->attr('set');
67
                $name = $map->text('exp:name');
68
                $ind->maps[] = $set . '.' . $name;
69
            }
70
            $this->indexes[] = $ind;
71
        }
72
73
        // TODO
74
    }
75
}
76