|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chrisyue\PhpM3u8\M3u8\AnnotationReadable; |
|
4
|
|
|
|
|
5
|
|
|
use Chrisyue\PhpM3u8\Stream\StreamInterface; |
|
6
|
|
|
use Chrisyue\PhpM3u8\M3u8\AbstractM3u8; |
|
7
|
|
|
use Chrisyue\PhpM3u8\M3u8\M3u8Interface; |
|
8
|
|
|
use Chrisyue\PhpM3u8\M3u8\SequenceAwareInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractAnnotationReadable extends AbstractM3u8 |
|
11
|
|
|
{ |
|
12
|
|
|
private $refProperties; |
|
13
|
|
|
|
|
14
|
|
|
public function parse() |
|
15
|
|
|
{ |
|
16
|
|
|
do { |
|
17
|
|
|
$lineInfo = $this->getStream()->read(); |
|
18
|
|
|
if (null === $lineInfo) { |
|
19
|
|
|
continue; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
foreach ($this->getRefProperties() as $refProp) { |
|
23
|
|
|
$m3u8 = $this->getAnnotationReader()->getPropertyAnnotation($refProp, M3u8Interface::class); |
|
24
|
|
|
if (null === $m3u8) { |
|
25
|
|
|
continue; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$value = null === $this->getResult() ? null : $refProp->getValue($this->getResult()); |
|
29
|
|
|
if (null !== $value && !is_array($value)) { |
|
30
|
|
|
continue; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$parsed = $m3u8->setStream($this->getStream())->parse()->getResult(); |
|
34
|
|
|
if (null !== $parsed) { |
|
35
|
|
|
is_array($value) ? $value[] = $parsed : $value = $parsed; |
|
36
|
|
|
$refProp->setValue($this->ensureResult(), $value); |
|
37
|
|
|
|
|
38
|
|
|
break; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} while ($this->couldTryNextLine() && $this->moveToNextLine()); |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function dump() |
|
47
|
|
|
{ |
|
48
|
|
|
$refProps = $this->getRefProperties(); |
|
49
|
|
|
$dumpers = []; |
|
50
|
|
|
foreach ($refProps as $refProp) { |
|
51
|
|
|
$m3u8 = $this->getAnnotationReader()->getPropertyAnnotation($refProp, M3u8Interface::class); |
|
52
|
|
|
if (null === $m3u8) { |
|
53
|
|
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$dumpers[$m3u8->getSequence()] = compact('m3u8', 'refProp'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
ksort($dumpers); |
|
60
|
|
|
|
|
61
|
|
|
foreach ($dumpers as $dumper) { |
|
62
|
|
|
$value = $dumper['refProp']->getValue($this->getResult()); |
|
63
|
|
|
if (null === $value) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$dumper['m3u8']->setStream($this->getStream()); |
|
68
|
|
|
|
|
69
|
|
|
!is_array($value) ? $dumper['m3u8']->setResult($value)->dump() : array_walk($value, function($val) use ($dumper) { |
|
70
|
|
|
$dumper['m3u8']->setResult($val)->dump(); |
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
abstract protected function couldTryNextLine(); |
|
76
|
|
|
|
|
77
|
|
|
abstract protected function getAnnotationReader(); |
|
78
|
|
|
|
|
79
|
|
|
abstract protected function getClass(); |
|
80
|
|
|
|
|
81
|
|
|
abstract protected function createResult(); |
|
82
|
|
|
|
|
83
|
|
|
private function getRefProperties() |
|
84
|
|
|
{ |
|
85
|
|
|
if (null === $this->refProperties) { |
|
86
|
|
|
$refClass = new \ReflectionClass($this->getClass()); |
|
87
|
|
|
$this->refProperties = $refClass->getProperties(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->refProperties; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function moveToNextLine() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->getStream()->next(); |
|
96
|
|
|
|
|
97
|
|
|
return $this->getStream()->valid(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function ensureResult() |
|
101
|
|
|
{ |
|
102
|
|
|
$result = $this->getResult(); |
|
103
|
|
|
if (null === $result) { |
|
104
|
|
|
$result = $this->createResult(); |
|
105
|
|
|
$this->setResult($result); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $result; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|