1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chrisyue\PhpM3u8\M3u8\Core; |
4
|
|
|
|
5
|
|
|
abstract class AbstractParentCore extends AbstractCore |
6
|
|
|
{ |
7
|
|
|
public function parse() |
8
|
|
|
{ |
9
|
|
|
$parsedSequence = -1; |
10
|
|
|
$result = $this->initResult(); |
11
|
|
|
|
12
|
|
|
do { |
13
|
|
|
foreach ($this->getChildren() as $property => $parser) { |
14
|
|
|
if ($parser->getSequence() < $parsedSequence) { |
15
|
|
|
continue; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
$oldChildValue = $this->getChildValue($result, $property); |
19
|
|
|
$isChildValueArray = is_array($oldChildValue); |
20
|
|
|
if (null !== $oldChildValue && !$isChildValueArray) { |
21
|
|
|
continue; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$childValue = $parser->setLines($this->getLines())->parse(); |
25
|
|
|
if (null === $childValue) { |
26
|
|
|
continue; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$isChildValueArray ? $this->addChildValue($result, $property, $childValue) : $this->setChildValue($result, $property, $childValue); |
30
|
|
|
$parsedSequence = $parser->getSequence(); |
31
|
|
|
|
32
|
|
|
break; |
33
|
|
|
} |
34
|
|
|
} while ($this->shouldParseNextLine($result, $parsedSequence) && $this->moveToNextLine()); |
35
|
|
|
|
36
|
|
|
return $parsedSequence > -1 ? $result : null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function dump($result) |
40
|
|
|
{ |
41
|
|
|
$children = $this->getChildren(); |
42
|
|
|
uasort($children, function (ChildCoreInterface $core, ChildCoreInterface $core2) { |
43
|
|
|
return $core->getSequence() > $core2->getSequence(); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
foreach ($children as $property => $dumper) { |
47
|
|
|
$childResult = $this->getChildValue($result, $property); |
48
|
|
|
if (null === $childResult) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$dumper->setLines($this->getLines()); |
53
|
|
|
|
54
|
|
|
is_array($childResult) ? array_walk($childResult, function ($val) use ($dumper) { |
55
|
|
|
$dumper->dump($val); |
56
|
|
|
}) : $dumper->dump($childResult); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
abstract protected function initResult(); |
61
|
|
|
|
62
|
|
|
abstract protected function getChildValue($result, $childKey); |
63
|
|
|
|
64
|
|
|
abstract protected function addChildValue($result, $childKey, $value); |
65
|
|
|
|
66
|
|
|
abstract protected function setChildValue($result, $childKey, $value); |
67
|
|
|
|
68
|
|
|
abstract protected function shouldParseNextLine($result, $parsedSequence); |
69
|
|
|
|
70
|
|
|
abstract protected function getChildren(); |
71
|
|
|
|
72
|
|
|
private function moveToNextLine() |
73
|
|
|
{ |
74
|
|
|
$this->getLines()->goNext(); |
75
|
|
|
|
76
|
|
|
return $this->getLines()->isValid(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|