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

Line   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 78
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isSameType() 0 4 1
A getTag() 0 4 1
A getValue() 0 4 1
A isCategory() 0 4 1
A fromString() 0 19 4
A __toString() 0 16 4
A getCategory() 0 8 2
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Line;
4
5
class Line implements LineInterface
6
{
7
    private $tag;
8
9
    private $value;
10
11
    public function __construct($tag = null, $value = null)
12
    {
13
        $this->tag = $tag;
14
        $this->value = $value;
15
    }
16
17
    public function isSameType(LineInterface $line)
18
    {
19
        return $line->getTag() === $this->tag;
20
    }
21
22
    public function getTag()
23
    {
24
        return $this->tag;
25
    }
26
27
    public function getValue()
28
    {
29
        return $this->value;
30
    }
31
32
    public function isCategory($type)
33
    {
34
        return $this->getCategory() === $type;
35
    }
36
37
    static public function fromString($line)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
38
    {
39
        $line = trim($line);
40
        if (empty($line)) {
41
            return;
42
        }
43
44
        if ('#' !== $line[0]) {
45
            return new Line(null, $line);
46
        }
47
48
        if (strlen($line) < 2) {
49
            return;
50
        }
51
52
        list($tag, $value) = array_pad(explode(':', $line, 2), 2, '');
53
54
        return new self($tag, $value);
55
    }
56
57
    public function __toString()
58
    {
59
        if ($this->isCategory(LineInterface::CATEGORY_URI)) {
60
            return $this->value;
61
        }
62
63
        if (is_bool($this->value)) {
64
            if ($this->value) {
65
                return $this->tag;
66
            }
67
68
            return '';
69
        }
70
71
        return sprintf('%s:%s', $this->tag, $this->value);
72
    }
73
74
    private function getCategory()
75
    {
76
        if (null !== $this->tag) {
77
            return LineInterface::CATEGORY_TAG;
78
        }
79
80
        return LineInterface::CATEGORY_URI;
81
    }
82
}
83