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

DataBuilder::addUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Parser;
4
5
use Chrisyue\PhpM3u8\Definition\TagDefinition;
6
7
class DataBuilder
8
{
9
    private $currentUriAware;
10
11
    private $result;
12
13
    public function __construct()
14
    {
15
        $this->result = new \ArrayObject();
16
    }
17
18
    public function addUri($uri)
19
    {
20
        if (null === $this->currentUriAware) {
21
            throw new DataBuildingException('uri found, but doesn\'t know how to handle it');
22
        }
23
24
        $this->currentUriAware['uri'] = $uri;
25
        $this->currentUriAware = null;
26
    }
27
28
    public function addTag(TagDefinition $definition, $data)
29
    {
30
        $parent = $this->result;
31
        if (null === $this->currentUriAware) {
32
            if ('media-segment' === $definition->getCategory()) {
33
                $this->currentUriAware = new \ArrayObject();
34
                $this->result['mediaSegments'][] = $this->currentUriAware;
35
            } elseif ($definition->isUriAware()) {
36
                $this->currentUriAware = $data;
37
            }
38
        }
39
40
        if ('media-segment' === $definition->getCategory()) {
41
            $parent = $this->currentUriAware;
42
        }
43
44
        if ($definition->isMultiple()) {
45
            $parent[$definition->getProperty()][] = $data;
46
47
            return;
48
        }
49
50
        $parent[$definition->getProperty()] = $data;
51
    }
52
53
    public function getResult()
54
    {
55
        return $this->result;
56
    }
57
}
58