|
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\Line; |
|
13
|
|
|
|
|
14
|
|
|
class Line |
|
15
|
|
|
{ |
|
16
|
|
|
const TYPE_URI = 'uri'; |
|
17
|
|
|
const TYPE_TAG = 'tag'; |
|
18
|
|
|
|
|
19
|
|
|
private $tag; |
|
20
|
|
|
|
|
21
|
|
|
private $value; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct($tag = null, $value = null) |
|
24
|
|
|
{ |
|
25
|
|
|
if (null === $tag && null === $value) { |
|
26
|
|
|
throw new \InvalidArgumentException('$tag and $value can not both be null'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$this->tag = $tag; |
|
30
|
|
|
$this->value = $value; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function fromString($line) |
|
34
|
|
|
{ |
|
35
|
|
|
$line = trim($line); |
|
36
|
|
|
if (empty($line)) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ('#' !== $line[0]) { |
|
41
|
|
|
return new self(null, $line); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$line = ltrim($line, '#'); |
|
45
|
|
|
if (empty($line)) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
list($tag, $value) = array_pad(explode(':', $line, 2), 2, true); |
|
50
|
|
|
|
|
51
|
|
|
return new self($tag, $value); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getTag() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->tag; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getValue() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->value; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function isType($type) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getType() === $type; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function __toString() |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->isType(self::TYPE_URI)) { |
|
72
|
|
|
return $this->value; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (true === $this->value) { |
|
76
|
|
|
return sprintf('#%s', $this->tag); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (false === $this->value) { |
|
80
|
|
|
return ''; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return sprintf('#%s:%s', $this->tag, $this->value); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function getType() |
|
87
|
|
|
{ |
|
88
|
|
|
if (null !== $this->tag) { |
|
89
|
|
|
return self::TYPE_TAG; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return self::TYPE_URI; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|