BlobChunk::process()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace dlindberg\BlobChunk;
6
7
class BlobChunk
8
{
9
    /**
10
     * @var Config
11
     */
12
    private $config;
13
14
    public function __construct(Config $config = null)
15
    {
16
        $this->config = ($config instanceof Config ? $config : Config::createConfig());
17
    }
18
19
    public function __invoke(string $input): array
20
    {
21
        return $this->parse($input);
22
    }
23
24
    public function parse(string $input): array
25
    {
26
        return $this->run($this->config->docFactory->getNode($input)->firstChild);
27
    }
28
29
    public static function process(string $input, Config $config = null): array
30
    {
31
        return (new self($config))->parse($input);
32
    }
33
34
    private function run(\DOMNode $node, $carry = []): array
35
    {
36
        if ($this->config->manager->isParentNode($node)) {
37
            $carry = \array_merge($carry, $this->run($node->firstChild, []));
38
        } elseif ($node instanceof \DOMElement) {
39
            $carry[] = $this->config->parser->parse($node);
40
        }
41
42
        return null !== $node->nextSibling ?  $this->run($node->nextSibling, $carry) : $carry;
43
    }
44
}
45