1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlIndent; |
6
|
|
|
|
7
|
|
|
use YamlStandards\Model\Component\YamlCountOfParents; |
8
|
|
|
use YamlStandards\Model\Component\YamlService; |
9
|
|
|
|
10
|
|
|
class YamlIndentDataFactory |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string[] $fileLines |
14
|
|
|
* @param int $key |
15
|
|
|
* @param int $countOfIndents |
16
|
|
|
* @param string $fileLine current checked line in loop |
17
|
|
|
* @param bool $isCommentLine |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
9 |
|
public function getRightFileLines(array $fileLines, int $key, int $countOfIndents, string $fileLine, bool $isCommentLine = false): string |
21
|
|
|
{ |
22
|
9 |
|
if (YamlService::isLineComment($fileLines[$key])) { |
23
|
6 |
|
$key++; |
24
|
6 |
|
return $this->getRightFileLines($fileLines, $key, $countOfIndents, $fileLine, true); |
25
|
|
|
} |
26
|
|
|
|
27
|
9 |
|
$line = $fileLines[$key]; |
28
|
9 |
|
$trimmedLine = trim($line); |
29
|
9 |
|
$countOfRowIndents = YamlService::rowIndentsOf($line); |
30
|
9 |
|
$explodedLine = explode(':', $line); |
31
|
|
|
|
32
|
|
|
// empty line |
33
|
9 |
|
if (YamlService::isLineBlank($line)) { |
34
|
9 |
|
$fileRows = array_keys($fileLines); |
35
|
9 |
|
$lastFileRow = end($fileRows); |
36
|
|
|
/* set comment line indents by next non-empty line, e.g |
37
|
|
|
(empty line) |
38
|
|
|
# comment line |
39
|
|
|
(empty line) |
40
|
|
|
foo: bar |
41
|
|
|
*/ |
42
|
9 |
|
if ($isCommentLine && $lastFileRow !== $key) { |
43
|
3 |
|
$key++; |
44
|
3 |
|
return $this->getRightFileLines($fileLines, $key, $countOfIndents, $fileLine, true); |
45
|
|
|
} |
46
|
|
|
|
47
|
9 |
|
$correctIndents = YamlService::createCorrectIndentsByCountOfIndents(0); |
48
|
9 |
|
$trimmedFileLine = trim($fileLine); |
49
|
|
|
|
50
|
9 |
|
return $correctIndents . $trimmedFileLine; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// the highest parent |
54
|
9 |
|
if ($countOfRowIndents === 0) { |
55
|
|
|
// line is directive |
56
|
9 |
|
if (YamlService::hasLineThreeDashesOnStartOfLine($trimmedLine)) { |
57
|
6 |
|
$correctIndents = YamlService::createCorrectIndentsByCountOfIndents($countOfRowIndents); |
58
|
6 |
|
$trimmedFileLine = trim($fileLine); |
59
|
|
|
|
60
|
6 |
|
return $correctIndents . $trimmedFileLine; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// parent start as array, e.g. "- foo: bar" |
64
|
|
|
// skip comment line because we want result after this condition |
65
|
9 |
|
if ($isCommentLine === false && YamlService::isLineStartOfArrayWithKeyAndValue($trimmedLine)) { |
66
|
3 |
|
return $this->getCorrectLineForArrayWithKeyAndValue($line, $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
$correctIndents = YamlService::createCorrectIndentsByCountOfIndents($countOfRowIndents); |
70
|
9 |
|
$trimmedFileLine = trim($fileLine); |
71
|
|
|
|
72
|
9 |
|
return $correctIndents . $trimmedFileLine; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }" or "- foo:" |
76
|
9 |
|
if (YamlService::isLineStartOfArrayWithKeyAndValue($trimmedLine)) { |
77
|
9 |
|
return $this->getCorrectLineForArrayWithKeyAndValue($line, $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// children of array, description over name of function |
81
|
9 |
|
if ($this->belongLineToArray($fileLines, $key)) { |
82
|
6 |
|
$countOfParents = YamlCountOfParents::getCountOfParentsForLine($fileLines, $key); |
83
|
6 |
|
$correctIndents = YamlService::createCorrectIndentsByCountOfIndents($countOfParents * $countOfIndents); |
84
|
6 |
|
$trimmedFileLine = trim($fileLine); |
85
|
|
|
|
86
|
6 |
|
return $correctIndents . $trimmedFileLine; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// line without ':', e.g. array or string |
90
|
9 |
|
if (array_key_exists(1, $explodedLine) === false) { |
91
|
|
|
// is multidimensional array? |
92
|
9 |
|
if ($trimmedLine === '-') { |
93
|
6 |
|
$countOfParents = YamlCountOfParents::getCountOfParentsForLine($fileLines, $key); |
94
|
|
|
|
95
|
6 |
|
$correctIndents = YamlService::createCorrectIndentsByCountOfIndents($countOfParents * $countOfIndents); |
96
|
6 |
|
$trimmedFileLine = trim($fileLine); |
97
|
|
|
|
98
|
6 |
|
return $correctIndents . $trimmedFileLine; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// is array or string? |
102
|
6 |
|
$countOfParents = YamlCountOfParents::getCountOfParentsForLine($fileLines, $key); |
103
|
6 |
|
$correctIndents = YamlService::createCorrectIndentsByCountOfIndents($countOfParents * $countOfIndents); |
104
|
6 |
|
$trimmedFileLine = trim($fileLine); |
105
|
|
|
|
106
|
6 |
|
return $correctIndents . $trimmedFileLine; |
107
|
|
|
} |
108
|
|
|
|
109
|
9 |
|
$lineValue = $explodedLine[1]; |
110
|
9 |
|
$trimmedLineValue = trim($lineValue); |
111
|
|
|
|
112
|
|
|
// parent, not comment line |
113
|
9 |
|
if ($isCommentLine === false && (YamlService::isLineBlank($lineValue) || YamlService::isValueReuseVariable($trimmedLineValue))) { |
114
|
|
|
// fix situation when key is without value and is not parent, e.g.: " foo:" |
115
|
9 |
|
$nextLine = array_key_exists($key + 1, $fileLines) ? $fileLines[$key + 1] : ''; |
116
|
9 |
|
if (YamlService::rowIndentsOf($nextLine) > $countOfRowIndents) { |
117
|
9 |
|
$countOfParents = YamlCountOfParents::getCountOfParentsForLine($fileLines, $key); |
118
|
|
|
|
119
|
9 |
|
$correctIndents = YamlService::createCorrectIndentsByCountOfIndents($countOfParents * $countOfIndents); |
120
|
9 |
|
$trimmedFileLine = trim($fileLine); |
121
|
|
|
|
122
|
9 |
|
return $correctIndents . $trimmedFileLine; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
9 |
|
$countOfParents = YamlCountOfParents::getCountOfParentsForLine($fileLines, $key); |
127
|
9 |
|
$correctIndents = YamlService::createCorrectIndentsByCountOfIndents($countOfParents * $countOfIndents); |
128
|
9 |
|
$trimmedFileLine = trim($fileLine); |
129
|
|
|
|
130
|
9 |
|
return $correctIndents . $trimmedFileLine; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Belong line to children of array, e.g. |
135
|
|
|
* - foo: bar |
136
|
|
|
* baz: qux |
137
|
|
|
* quux: quuz |
138
|
|
|
* etc.: etc. |
139
|
|
|
* |
140
|
|
|
* @param string[] $fileLines |
141
|
|
|
* @param int $key |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
9 |
|
private function belongLineToArray(array $fileLines, int $key): bool |
145
|
|
|
{ |
146
|
9 |
|
while ($key >= 0) { |
147
|
9 |
|
$line = $fileLines[$key]; |
148
|
9 |
|
$key--; |
149
|
9 |
|
$prevLine = $fileLines[$key]; |
150
|
9 |
|
$trimmedPrevLine = trim($prevLine); |
151
|
|
|
|
152
|
9 |
|
if (YamlService::hasLineDashOnStartOfLine($trimmedPrevLine)) { |
153
|
9 |
|
$prevLine = preg_replace('/-/', ' ', $prevLine, 1); // replace '-' for space |
154
|
|
|
} |
155
|
|
|
|
156
|
9 |
|
if (YamlService::rowIndentsOf($prevLine) === YamlService::rowIndentsOf($line)) { |
157
|
9 |
|
if (YamlService::isLineStartOfArrayWithKeyAndValue($trimmedPrevLine)) { |
158
|
9 |
|
return true; |
159
|
|
|
} |
160
|
|
|
} else { |
161
|
9 |
|
break; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
9 |
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }" |
170
|
|
|
* |
171
|
|
|
* @param string $line |
172
|
|
|
* @param string[] $fileLines |
173
|
|
|
* @param int $key |
174
|
|
|
* @param int $countOfIndents |
175
|
|
|
* @param string $fileLine current checked line in loop |
176
|
|
|
* @param bool $isCommentLine |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
9 |
|
private function getCorrectLineForArrayWithKeyAndValue(string $line, array $fileLines, int $key, int $countOfIndents, string $fileLine, bool $isCommentLine): string |
180
|
|
|
{ |
181
|
9 |
|
$lineWithReplacedDashToSpace = preg_replace('/-/', ' ', $line, 1); |
182
|
9 |
|
$trimmedLineWithoutDash = trim($lineWithReplacedDashToSpace); |
183
|
|
|
|
184
|
9 |
|
$countOfParents = YamlCountOfParents::getCountOfParentsForLine($fileLines, $key); |
185
|
9 |
|
$correctIndentsOnStartOfLine = YamlService::createCorrectIndentsByCountOfIndents($countOfParents * $countOfIndents); |
186
|
|
|
|
187
|
9 |
|
$trimmedFileLine = trim($fileLine); |
188
|
9 |
|
if ($isCommentLine) { |
189
|
6 |
|
return $correctIndentsOnStartOfLine . $trimmedFileLine; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// solution "- { foo: bar }" |
193
|
9 |
|
if (YamlService::isCurlyBracketInStartOfString($trimmedLineWithoutDash)) { |
194
|
6 |
|
$correctIndentsBetweenDashAndBracket = YamlService::createCorrectIndentsByCountOfIndents(1); |
195
|
|
|
|
196
|
6 |
|
return $correctIndentsOnStartOfLine . '-' . $correctIndentsBetweenDashAndBracket . $trimmedLineWithoutDash; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// solution "- foo" (single value of an array) |
200
|
7 |
|
if (YamlService::isKeyInStartOfString($trimmedLineWithoutDash) === false) { |
201
|
4 |
|
$correctIndentsBetweenDashAndKey = YamlService::createCorrectIndentsByCountOfIndents(1); |
202
|
|
|
|
203
|
4 |
|
return $correctIndentsOnStartOfLine . '-' . $correctIndentsBetweenDashAndKey . $trimmedLineWithoutDash; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* solution for one or more values in array |
208
|
|
|
* "- foo: bar" |
209
|
|
|
* " baz: qux" |
210
|
|
|
*/ |
211
|
6 |
|
$correctIndentsBetweenDashAndKey = YamlService::createCorrectIndentsByCountOfIndents($countOfIndents - 1); // 1 space is dash, dash is as indent |
212
|
|
|
|
213
|
6 |
|
return $correctIndentsOnStartOfLine . '-' . $correctIndentsBetweenDashAndKey . $trimmedLineWithoutDash; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|