ExplainResponse::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 3
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
     * @param string $url Request URL
28
     */
29
    public function __construct($text = null, &$client = null, $url = null)
30
    {
31
        parent::__construct($text, $client, $url);
32
33
        $this->indexes = array();
34
35
        if (is_null($this->response)) {
36
            return;
37
        }
38
        $explain = $this->response->first('/srw:explainResponse/srw:record/srw:recordData/exp:explain');
39
        if (!$explain) {
40
            return;
41
        }
42
43
        $this->parseExplainResponse($explain);
44
    }
45
46
    protected function parseExplainResponse(QuiteSimpleXMLElement $node)
47
    {
48
        $serverInfo = $node->first('exp:serverInfo');
49
        $dbInfo = $node->first('exp:databaseInfo');
50
        $indexInfo = $node->first('exp:indexInfo');
51
52
        $this->host = $serverInfo->text('exp:host');
53
        $this->port = (int) $serverInfo->text('exp:port');
54
        $this->database = new \StdClass;
55
        $this->database->identifier = $serverInfo->text('exp:database');
56
        $this->database->title = $dbInfo->text('exp:title');
57
        $this->database->description = $dbInfo->text('exp:description');
58
59
        foreach ($indexInfo->xpath('exp:index') as $index) {
60
            $ind = new \StdClass;
61
            $ind->scan = ($index->attr('scan') == 'true');
62
            $ind->search = ($index->attr('search') == 'true');
63
            $ind->sort = ($index->attr('sort') == 'true');
64
            $ind->title = $index->text('exp:title');
65
            $ind->maps = array();
66
            foreach ($index->xpath('exp:map') as $map) {
67
                $set = $map->first('exp:name')->attr('set');
68
                $name = $map->text('exp:name');
69
                $ind->maps[] = $set . '.' . $name;
70
            }
71
            $this->indexes[] = $ind;
72
        }
73
74
        // TODO
75
    }
76
}
77