Completed
Pull Request — develop (#27)
by Chris
02:45
created

DataBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addUri() 0 9 2
B addTag() 0 24 6
A getResult() 0 4 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\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