Completed
Pull Request — develop (#27)
by Chris
01:52
created

AttrListTransformer::doTransform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Transformer;
4
5
use Chrisyue\PhpM3u8\Transformer\KvTransformer;
6
use Chrisyue\PhpM3u8\DataAccessor\DataAccessorInterface;
7
use Chrisyue\PhpM3u8\Transformer\AttrTransformersInterface;
8
9
class AttrListTransformer extends AbstractTransformer
10
{
11
    private $kvTransformer;
12
13
    private $attrTransformers;
14
15
    private $dataAccessor;
16
17
    public function __construct(
18
        KvTransformer $kvTransformer,
19
        AttrTransformersInterface $attrTransformers,
20
        DataAccessorInterface $dataAccessor
21
    ) {
22
        $this->kvTransformer = $kvTransformer;
23
        $this->attrTransformers = $attrTransformers;
24
        $this->dataAccessor = $dataAccessor;
25
    }
26
27
    public function supports($origin)
28
    {
29
        return is_string($origin) && $this->kvTransformer->supports($origin);
30
    }
31
32
    public function getDataAccessor()
33
    {
34
        return $this->dataAccessor;
35
    }
36
37
    protected function doTransform($origin)
38
    {
39
        $attrs = $this->kvTransformer->transform($origin);
40
        $this->dataAccessor->reset();
41
42
        foreach ($attrs as $name => $value) {
43
            $key = $this->attrTransformers->getKeyByName($name);
44
            if (null !== $key) {
45
                $value = $this->attrTransformers->get($key)->transform($value);
46
                $this->dataAccessor->set($key, $value);
47
            }
48
        }
49
50
        return $this->dataAccessor->getData();
51
    }
52
}
53