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
|
20 |
|
public static function isLineNotBlank(string $yamlLine): bool |
45
|
|
|
{ |
46
|
20 |
|
return trim($yamlLine) !== ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $yamlLine |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
15 |
|
public static function isLineComment(string $yamlLine): bool |
54
|
|
|
{ |
55
|
15 |
|
return preg_match('/^\s*#/', $yamlLine) === 1; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $value |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
9 |
|
public static function isValueReuseVariable(string $value): bool |
63
|
|
|
{ |
64
|
9 |
|
return strpos($value, '&') === 0; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $value |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
9 |
|
public static function hasLineDashOnStartOfLine(string $value): bool |
72
|
|
|
{ |
73
|
9 |
|
return strpos($value, '-') === 0; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $trimmedLine |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
9 |
|
public static function hasLineThreeDashesOnStartOfLine(string $trimmedLine): bool |
81
|
|
|
{ |
82
|
9 |
|
return strpos($trimmedLine, '---') === 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $value |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
9 |
|
public static function isCurlyBracketInStartOfString(string $value): bool |
90
|
|
|
{ |
91
|
9 |
|
return strpos($value, '{') === 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }" |
96
|
|
|
* |
97
|
|
|
* @param string $trimmedLine |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
9 |
|
public static function isLineStartOfArrayWithKeyAndValue(string $trimmedLine): bool |
101
|
|
|
{ |
102
|
9 |
|
return $trimmedLine !== '-' && self::hasLineDashOnStartOfLine($trimmedLine); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* value starting with key, e.g. 'foo: bar' or '"foo bar": baz' |
107
|
|
|
* |
108
|
|
|
* @param string $value |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
7 |
|
public static function isKeyInStartOfString(string $value): bool |
112
|
|
|
{ |
113
|
7 |
|
return (bool)preg_match('~^(' . Inline::REGEX_QUOTED_STRING . '|[^ \'"{\[].*?) *:(\s|$)~u', $value); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* line possibly opening an array, e.g. 'foo:' or '- foo:' |
118
|
|
|
* |
119
|
|
|
* @param string $trimmedLine |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
9 |
|
public static function isLineOpeningAnArray(string $trimmedLine): bool |
123
|
|
|
{ |
124
|
9 |
|
return (bool)preg_match('~^(- +)*(' . Inline::REGEX_QUOTED_STRING . '|[^ \'"{\[].*?) *:$~u', $trimmedLine); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $line |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
27 |
|
public static function rowIndentsOf(string $line): int |
132
|
|
|
{ |
133
|
27 |
|
return strlen($line) - strlen(ltrim($line)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $line |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
3 |
|
public static function keyIndentsOf(string $line): int |
141
|
|
|
{ |
142
|
3 |
|
return strlen($line) - strlen(ltrim($line, '- ')); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param int $countOfIndents |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
11 |
|
public static function createCorrectIndentsByCountOfIndents(int $countOfIndents): string |
150
|
|
|
{ |
151
|
11 |
|
return str_repeat(' ', $countOfIndents); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $line |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
6 |
|
public static function hasLineValue(string $line): bool |
159
|
|
|
{ |
160
|
6 |
|
$explodedLine = explode(':', $line); |
161
|
|
|
|
162
|
6 |
|
return array_key_exists(1, $explodedLine) && self::isLineNotBlank($explodedLine[1]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $line |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
4 |
|
public static function hasLineColon(string $line): bool |
170
|
|
|
{ |
171
|
4 |
|
return strpos($line, ':') !== false; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|