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

DataBuilder::addTag()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 16
nop 2
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\Definition\TagDefinition;
15
16
class DataBuilder
17
{
18
    private $currentUriAware;
19
20
    private $result;
21
22
    public function __construct()
23
    {
24
        $this->result = new \ArrayObject();
25
    }
26
27
    public function addUri($uri)
28
    {
29
        if (null === $this->currentUriAware) {
30
            throw new DataBuildingException('uri found, but doesn\'t know how to handle it');
31
        }
32
33
        $this->currentUriAware['uri'] = $uri;
34
        $this->currentUriAware = null;
35
    }
36
37
    public function addTag(TagDefinition $definition, $data)
38
    {
39
        $parent = $this->result;
40
        if (null === $this->currentUriAware) {
41
            if ('media-segment' === $definition->getCategory()) {
42
                $this->currentUriAware = new \ArrayObject();
43
                $this->result['mediaSegments'][] = $this->currentUriAware;
44
            } elseif ($definition->isUriAware()) {
45
                $this->currentUriAware = $data;
46
            }
47
        }
48
49
        if ('media-segment' === $definition->getCategory()) {
50
            $parent = $this->currentUriAware;
51
        }
52
53
        if ($definition->isMultiple()) {
54
            $parent[$definition->getProperty()][] = $data;
55
56
            return;
57
        }
58
59
        $parent[$definition->getProperty()] = $data;
60
    }
61
62
    public function getResult()
63
    {
64
        return $this->result;
65
    }
66
}
67