1 | <?php |
||
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 || null !== $result->$property && !is_array($result->$property)) { |
||
15 | continue; |
||
16 | } |
||
17 | |||
18 | $childResult = $parser->setLines($this->getLines())->parse(); |
||
19 | if (null === $childResult) { |
||
20 | continue; |
||
21 | } |
||
22 | |||
23 | is_array($result->$property) ? $result->$property[] = $childResult : $result->$property = $childResult; |
||
24 | $parsedSequence = $parser->getSequence(); |
||
25 | |||
26 | break; |
||
27 | } |
||
28 | } while ($this->shouldParseNextLine($result, $parsedSequence) && $this->moveToNextLine()); |
||
29 | |||
30 | return $parsedSequence > -1 ? $result : null; |
||
31 | } |
||
32 | |||
33 | public function dump($result) |
||
34 | { |
||
35 | $children = $this->getChildren(); |
||
36 | uasort($children, function (ChildCoreInterface $core, ChildCoreInterface $core2) { |
||
37 | return $core->getSequence() > $core2->getSequence(); |
||
38 | }); |
||
39 | |||
40 | foreach ($children as $property => $dumper) { |
||
41 | $childResult = $result->$property; |
||
42 | if (null === $childResult) { |
||
43 | continue; |
||
44 | } |
||
45 | |||
46 | $dumper->setLines($this->getLines()); |
||
47 | |||
48 | is_array($childResult) ? array_walk($childResult, function ($val) use ($dumper) { |
||
49 | $dumper->dump($val); |
||
50 | }) : $dumper->dump($childResult); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | abstract protected function initResult(); |
||
55 | |||
56 | abstract protected function shouldParseNextLine($result, $parsedSequence); |
||
57 | |||
58 | abstract protected function getChildren(); |
||
59 | |||
60 | private function moveToNextLine() |
||
66 | } |
||
67 |