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

Line::fromString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
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