1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\Component; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Yaml\Inline; |
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
|
10
|
|
|
class YamlService |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $pathToYamlFile |
14
|
|
|
* @throws \Symfony\Component\Yaml\Exception\ParseException |
15
|
|
|
* @return string[]|string[][] |
16
|
|
|
*/ |
17
|
13 |
|
public static function getYamlData(string $pathToYamlFile): array |
18
|
|
|
{ |
19
|
13 |
|
return (array)Yaml::parse(file_get_contents($pathToYamlFile), Yaml::PARSE_CUSTOM_TAGS); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $key |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
3 |
|
public static function hasArrayKeyUnderscoreAsFirstCharacter(string $key): bool |
27
|
|
|
{ |
28
|
3 |
|
return strpos($key, '_') === 0; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $key |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
3 |
|
public static function hasNotArrayKeyUnderscoreAsFirstCharacter(string $key): bool |
36
|
|
|
{ |
37
|
3 |
|
return strpos($key, '_') !== 0; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $yamlLine |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
23 |
|
public static function isLineNotBlank(string $yamlLine): bool |
45
|
|
|
{ |
46
|
23 |
|
return trim($yamlLine) !== ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $yamlLine |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
15 |
|
public static function isLineBlank(string $yamlLine): bool |
54
|
|
|
{ |
55
|
15 |
|
return trim($yamlLine) === ''; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $yamlLine |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
16 |
|
public static function isLineComment(string $yamlLine): bool |
63
|
|
|
{ |
64
|
16 |
|
return preg_match('/^\s*#/', $yamlLine) === 1; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $value |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
9 |
|
public static function isValueReuseVariable(string $value): bool |
72
|
|
|
{ |
73
|
9 |
|
return strpos($value, '&') === 0; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $value |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
10 |
|
public static function hasLineDashOnStartOfLine(string $value): bool |
81
|
|
|
{ |
82
|
10 |
|
return strpos($value, '-') === 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $trimmedLine |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
9 |
|
public static function hasLineThreeDashesOnStartOfLine(string $trimmedLine): bool |
90
|
|
|
{ |
91
|
9 |
|
return strpos($trimmedLine, '---') === 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $value |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
9 |
|
public static function isCurlyBracketInStartOfString(string $value): bool |
99
|
|
|
{ |
100
|
9 |
|
return strpos($value, '{') === 0; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }" |
105
|
|
|
* |
106
|
|
|
* @param string $trimmedLine |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
10 |
|
public static function isLineStartOfArrayWithKeyAndValue(string $trimmedLine): bool |
110
|
|
|
{ |
111
|
10 |
|
return $trimmedLine !== '-' && self::hasLineDashOnStartOfLine($trimmedLine); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* value starting with key, e.g. 'foo: bar' or '"foo bar": baz' |
116
|
|
|
* |
117
|
|
|
* @param string $value |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
7 |
|
public static function isKeyInStartOfString(string $value): bool |
121
|
|
|
{ |
122
|
7 |
|
return (bool)preg_match('~^(' . Inline::REGEX_QUOTED_STRING . '|[^ \'"{\[].*?) *:(\s|$)~u', $value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* line possibly opening an array, e.g. 'foo:' or '- foo:' |
127
|
|
|
* |
128
|
|
|
* @param string $trimmedLine |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
10 |
|
public static function isLineOpeningAnArray(string $trimmedLine): bool |
132
|
|
|
{ |
133
|
10 |
|
return (bool)preg_match('~^(- +)*(' . Inline::REGEX_QUOTED_STRING . '|[^ \'"{\[].*?) *:$~u', $trimmedLine); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $line |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
28 |
|
public static function rowIndentsOf(string $line): int |
141
|
|
|
{ |
142
|
28 |
|
return strlen($line) - strlen(ltrim($line)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $line |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
4 |
|
public static function keyIndentsOf(string $line): int |
150
|
|
|
{ |
151
|
4 |
|
return strlen($line) - strlen(ltrim($line, '- ')); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param int $countOfIndents |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
11 |
|
public static function createCorrectIndentsByCountOfIndents(int $countOfIndents): string |
159
|
|
|
{ |
160
|
11 |
|
return str_repeat(' ', $countOfIndents); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $line |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
9 |
|
public static function hasLineValue(string $line): bool |
168
|
|
|
{ |
169
|
9 |
|
$explodedLine = explode(':', $line); |
170
|
|
|
|
171
|
9 |
|
return array_key_exists(1, $explodedLine) && self::isLineNotBlank($explodedLine[1]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $line |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
7 |
|
public static function hasLineColon(string $line): bool |
179
|
|
|
{ |
180
|
7 |
|
return strpos($line, ':') !== false; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|