1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlSpacesBetweenGroups; |
6
|
|
|
|
7
|
|
|
use YamlStandards\Model\Component\YamlService; |
8
|
|
|
|
9
|
|
|
class YamlSpacesBetweenGroupsDataFactory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param string[] $yamlLines |
13
|
|
|
* @param int $level |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
7 |
|
public function getCorrectYamlContentWithSpacesBetweenGroups(array $yamlLines, int $level): string |
17
|
|
|
{ |
18
|
7 |
|
$correctYamlLines = []; |
19
|
|
|
|
20
|
7 |
|
foreach ($yamlLines as $key => $yamlLine) { |
21
|
7 |
|
if (YamlService::isLineComment($yamlLine)) { |
22
|
6 |
|
$correctYamlLines[$key] = $yamlLine; |
23
|
6 |
|
continue; |
24
|
|
|
} |
25
|
|
|
|
26
|
7 |
|
$lineLevel = $this->getLevelOfCurrentLine($key, $yamlLines, YamlService::rowIndentsOf($yamlLine)); |
27
|
7 |
|
$correctYamlLines[$key] = $yamlLine; |
28
|
|
|
|
29
|
7 |
|
if ($lineLevel <= $level) { |
30
|
7 |
|
$correctYamlLines = $this->getCorrectYamlLinesWithSpace($correctYamlLines, $key); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
7 |
|
ksort($correctYamlLines, SORT_NATURAL); |
35
|
|
|
|
36
|
7 |
|
return implode("\n", $correctYamlLines); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param int $key |
41
|
|
|
* @param string[] $yamlLines |
42
|
|
|
* @param int $previousCountOfIndents |
43
|
|
|
* @param int $currentLineLevel |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
8 |
|
private function getLevelOfCurrentLine(int $key, array $yamlLines, int $previousCountOfIndents, int $currentLineLevel = 1): int |
47
|
|
|
{ |
48
|
8 |
|
$yamlLine = $yamlLines[$key]; |
49
|
8 |
|
$countOfRowIndents = YamlService::rowIndentsOf($yamlLine); |
50
|
8 |
|
$key--; |
51
|
|
|
|
52
|
8 |
|
if (YamlService::isLineComment($yamlLine)) { |
53
|
6 |
|
return $this->getLevelOfCurrentLine($key, $yamlLines, $previousCountOfIndents, $currentLineLevel); |
54
|
|
|
} |
55
|
|
|
|
56
|
8 |
|
if ($countOfRowIndents < $previousCountOfIndents) { |
57
|
8 |
|
$currentLineLevel++; |
58
|
8 |
|
$previousCountOfIndents = $countOfRowIndents; |
59
|
|
|
|
60
|
8 |
|
if ($countOfRowIndents > 0) { |
61
|
8 |
|
return $this->getLevelOfCurrentLine($key, $yamlLines, $previousCountOfIndents, $currentLineLevel); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
8 |
|
if ($countOfRowIndents > 0) { |
66
|
8 |
|
return $this->getLevelOfCurrentLine($key, $yamlLines, $previousCountOfIndents, $currentLineLevel); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
return $currentLineLevel; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* add empty line before current line if current line is not first element in parent |
74
|
|
|
* |
75
|
|
|
* @param string[] $correctYamlLines |
76
|
|
|
* @param int $key |
77
|
|
|
* @return string[] |
78
|
|
|
*/ |
79
|
7 |
|
private function getCorrectYamlLinesWithSpace(array $correctYamlLines, int $key): array |
80
|
|
|
{ |
81
|
7 |
|
$yamlLine = $correctYamlLines[$key]; |
82
|
7 |
|
$key--; |
83
|
|
|
|
84
|
7 |
|
if (reset($correctYamlLines) === $yamlLine) { |
85
|
7 |
|
return $correctYamlLines; |
86
|
|
|
} |
87
|
|
|
|
88
|
7 |
|
while (array_key_exists($key, $correctYamlLines) && YamlService::isLineComment($correctYamlLines[$key])) { |
89
|
6 |
|
$key--; |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
if (array_key_exists($key, $correctYamlLines) === false) { |
93
|
6 |
|
return $correctYamlLines; |
94
|
|
|
} |
95
|
|
|
|
96
|
7 |
|
$previousYamlLine = $correctYamlLines[$key]; |
97
|
7 |
|
if (YamlService::rowIndentsOf($previousYamlLine) < YamlService::rowIndentsOf($yamlLine)) { |
98
|
7 |
|
return $correctYamlLines; |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
$correctYamlLines[$key . 'space'] = ''; |
102
|
|
|
|
103
|
7 |
|
return $correctYamlLines; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|