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

TagDefinitions   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 81
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 31 5
A findOneByTag() 0 12 3
A get() 0 8 2
A getHeadProperties() 0 4 1
A getMediaSegmentProperties() 0 4 1
A getFootProperties() 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\Definition;
13
14
use Chrisyue\PhpM3u8\Config;
15
16
class TagDefinitions
17
{
18
    private $definitions;
19
20
    private $tagPropertyMap;
21
22
    private $headProperties;
23
24
    private $mediaSegmentProperties;
25
26
    private $footProperties;
27
28
    public function __construct($definitions)
29
    {
30
        foreach ($definitions as $property => $definition) {
31
            if (!isset($definition['tag'])) {
32
                throw new DefinitionException('every tag definition must contain "tag"');
33
            }
34
35
            $this->tagPropertyMap[$definition['tag']] = $property;
36
37
            $position = $definition['position'];
38
            if ('media-segment' === $definition['category']) {
39
                $this->mediaSegmentProperties[$definition['position']] = $property;
40
41
                continue;
42
            }
43
44
            if (0 > $position) {
45
                $this->headProperties[$position] = $property;
46
47
                continue;
48
            }
49
50
            $this->footProperties[$position] = $property;
51
        }
52
53
        $this->definitions = $definitions;
54
55
        ksort($this->headProperties);
56
        ksort($this->mediaSegmentProperties);
57
        ksort($this->footProperties);
58
    }
59
60
    public function findOneByTag($tag)
61
    {
62
        if (!is_string($tag)) {
63
            throw new \InvalidArgumentException('$tag can only be string, got %s', var_export($tag));
64
        }
65
66
        if (isset($this->tagPropertyMap[$tag])) {
67
            $property = $this->tagPropertyMap[$tag];
68
69
            return new TagDefinition($property, new Config($this->definitions[$property]));
70
        }
71
    }
72
73
    public function get($property)
74
    {
75
        if (!is_string($property)) {
76
            throw new \InvalidArgumentException('$property can only be string, got %s', var_export($tag));
77
        }
78
79
        return new TagDefinition($property, new Config($this->definitions[$property]));
80
    }
81
82
    public function getHeadProperties()
83
    {
84
        return $this->headProperties;
85
    }
86
87
    public function getMediaSegmentProperties()
88
    {
89
        return $this->mediaSegmentProperties;
90
    }
91
92
    public function getFootProperties()
93
    {
94
        return $this->footProperties;
95
    }
96
}
97