Completed
Pull Request — develop (#27)
by Chris
05:29
created

Parser::parse()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
cc 6
nc 6
nop 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Parser;
4
5
use Chrisyue\PhpM3u8\Line\Line;
6
use Chrisyue\PhpM3u8\Rule\ParsingRules;
7
use Chrisyue\PhpM3u8\Line\Lines;
8
use Chrisyue\PhpM3u8\Parser\DataBuilder;
9
use Chrisyue\PhpM3u8\Definition\TagDefinitions;
10
use Chrisyue\PhpM3u8\Config;
11
12
class Parser
13
{
14
    private $tagDefinitions;
15
16
    private $valueParsers;
17
18
    private $dataBuilder;
19
20
    public function __construct(TagDefinitions $tagDefinitions, Config $valueParsers, DataBuilder $dataBuilder)
21
    {
22
        $this->tagDefinitions = $tagDefinitions;
23
        $this->valueParsers = $valueParsers;
24
        $this->dataBuilder = $dataBuilder;
25
    }
26
27
    public function parse(Lines $lines)
28
    {
29
        foreach ($lines as $line) {
30
            if ($line->isType(Line::TYPE_URI)) {
31
                $this->dataBuilder->addUri($line->getValue());
32
33
                continue;
34
            }
35
36
            $tag = $line->getTag();
37
38
            $definition = $this->tagDefinitions->findOneByTag($tag);
39
            if (null === $definition) {
40
                continue;
41
            }
42
43
            $valueType = $definition->getValueType();
44
            $value = $line->getValue();
45
46
            $parse = $this->valueParsers->get($valueType);
47
            if (is_callable($parse)) {
48
                $value = 'attribute-list' === $valueType ? $parse($value, $definition->getAttributeTypes()) : $parse($value);
49
            }
50
            $this->dataBuilder->addTag($definition, $value);
51
        }
52
53
        return $this->dataBuilder->getResult();
54
    }
55
}
56