Completed
Push — master ( b59592...c78116 )
by Faiz
01:47
created

XML::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace FaizShukri\Quran\Supports;
4
5
class XML
6
{
7
    private $xml;
8
9 39
    public function __construct($xmlPath)
10
    {
11 39
        $this->xml = $this->pathToXML($xmlPath);
12 39
    }
13
14 39
    public function find($surah, array $ayah)
15
    {
16 39
        $xml = new \SimpleXMLElement($this->xml);
17
18 39
        $xpath = '//sura[@index=' . $surah . ']/aya[' . implode(' or ', array_map(function ($a) {
19 39
                return '@index=' . $a;
20 39
            }, $ayah)) . ']';
21
22 39
        $xpathResult = $xml->xpath($xpath);
23 39
        $result = [];
24
25 39
        while (list(, $node) = each($xpathResult)) {
26 39
            $node = (array)$node;
27 39
            $verse = current($node);
28 39
            $result[$verse['index']] = $verse['text'];
29 39
        }
30
31 39
        return $result;
32
    }
33
34 39
    public function pathToXML($xmlPath)
35
    {
36 39
        $xml = fopen($xmlPath, "r");
37 39
        $res = fread($xml, filesize($xmlPath));
38 39
        fclose($xml);
39
40 39
        return $res;
41
    }
42
}