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

XmlExtractor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A extract() 0 22 4
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