Completed
Pull Request — develop (#27)
by Chris
02:25
created

ParentDumper::dump()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Dumper;
4
5
use Chrisyue\PhpM3u8\DataAccessor\FactoryInterface;
6
7
class ParentDumper implements DumperInterface
8
{
9
    private $dataAccessorFactory;
10
11
    private $children;
12
13
    public function __construct(FactoryInterface $dataAccessorFactory, array $children)
14
    {
15
        $this->dataAccessorFactory = $dataAccessorFactory;
16
        $this->children = $children;
17
    }
18
19
    public function setChild($key, ChildDumperInterface $child)
20
    {
21
        $this->children[$key] = $child;
22
    }
23
24
    public function dump($value)
25
    {
26
        uasort($this->children, function (ChildDumperInterface $child, ChildDumperInterface $child2) {
27
            return $child->getSequence() > $child2->getSequence();
28
        });
29
30
        $dataAccessor = $this->dataAccessorFactory->createByData($value);
31
        foreach ($this->children as $key => $child) {
32
            $childResult = $dataAccessor->get($key);
33
            if (null === $childResult) {
34
                continue;
35
            }
36
37
            $child->isRepeatable() ? array_walk($childResult, function ($val) use ($child) {
38
                $child->dump($val);
39
            }) : $child->dump($childResult);
40
        }
41
    }
42
}
43