Completed
Pull Request — master (#27)
by jean
12:06
created

XmlExtractor::extract()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ImportBundle\Extractor;
6
7
class XmlExtractor
8
{
9
    // http://drib.tech/programming/parse-large-xml-files-php
10
    public function extract(string $filepath, string $nodeName): \Traversable
11
    {
12
        $xml = new \XMLReader();
13
14
        if (false !== strpos($filepath, '.gz')) {
15
            $xml->open('compress.zlib://'.$filepath);
16
        } else {
17
            $xml->open($filepath);
18
        }
19
20
        $itemCount = 0;
21
        while ($xml->read()) {
22
            if ($nodeName != $xml->name) {
23
                continue;
24
            }
25
26
            ++$itemCount;
27
            $element = new \SimpleXMLElement($xml->readOuterXml());
28
            $data = json_decode(json_encode($element), true);
29
30
            yield $itemCount => $data;
31
            $xml->next();
32
        }
33
    }
34
}
35