Completed
Pull Request — develop (#27)
by Chris
01:48
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
/*
4
 * This file is part of the PhpM3u8 package.
5
 *
6
 * (c) Chrisyue <https://chrisyue.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Chrisyue\PhpM3u8\Parser;
13
14
use Chrisyue\PhpM3u8\Config;
15
use Chrisyue\PhpM3u8\Definition\TagDefinitions;
16
use Chrisyue\PhpM3u8\Line\Line;
17
use Chrisyue\PhpM3u8\Line\Lines;
18
19
class Parser
20
{
21
    private $tagDefinitions;
22
23
    private $valueParsers;
24
25
    private $dataBuilder;
26
27
    public function __construct(TagDefinitions $tagDefinitions, Config $valueParsers, DataBuilder $dataBuilder)
28
    {
29
        $this->tagDefinitions = $tagDefinitions;
30
        $this->valueParsers = $valueParsers;
31
        $this->dataBuilder = $dataBuilder;
32
    }
33
34
    public function parse(Lines $lines)
35
    {
36
        foreach ($lines as $line) {
37
            if ($line->isType(Line::TYPE_URI)) {
38
                $this->dataBuilder->addUri($line->getValue());
39
40
                continue;
41
            }
42
43
            $tag = $line->getTag();
44
45
            $definition = $this->tagDefinitions->findOneByTag($tag);
46
            if (null === $definition) {
47
                continue;
48
            }
49
50
            $valueType = $definition->getValueType();
51
            $value = $line->getValue();
52
53
            $parse = $this->valueParsers->get($valueType);
54
            if (is_callable($parse)) {
55
                $value = 'attribute-list' === $valueType ? $parse($value, $definition->getAttributeTypes()) : $parse($value);
56
            }
57
            $this->dataBuilder->addTag($definition, $value);
58
        }
59
60
        return $this->dataBuilder->getResult();
61
    }
62
}
63