forked from kane-menicou/certification-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat.php
52 lines (38 loc) · 1.42 KB
/
reformat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
require_once 'vendor/autoload.php';
$finder = new Finder();
$files = $finder->in(__DIR__ . '/data')->files()->name('/.*\.[yml|yaml]/');
foreach ($files as $file) {
$content = Yaml::parse($file->getContents());
foreach ($content['questions'] ?? [] as $question) {
$oneIsCorrect = false;
$questionText = $question['question'];
foreach ($question['answers'] as $answer) {
if ( ! array_key_exists('correct', $answer)) {
echo "No 'correct' index for: " . $questionText . PHP_EOL;
continue;
}
if ($answer['correct'] === true) {
$oneIsCorrect = true;
}
if ( ! array_key_exists('value', $answer)) {
echo "No 'value' index for: " . $questionText . PHP_EOL;
continue;
}
if ( ! is_string($answer['value']) && ! is_int($answer['value']) && ! is_float($answer['value'])) {
echo "'value' index incorrect type: " . $questionText . PHP_EOL;
continue;
}
}
if ( ! $oneIsCorrect) {
echo 'no correct answer for: ' . $questionText . PHP_EOL;
}
}
file_put_contents(
$file->getRealPath(),
Yaml::dump($content, 5, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)
);
}