|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlAlphabetical; |
|
6
|
|
|
|
|
7
|
|
|
use SebastianBergmann\Diff\Differ; |
|
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
9
|
|
|
use YamlStandards\Command\ProcessOutput; |
|
10
|
|
|
use YamlStandards\Model\CheckerInterface; |
|
11
|
|
|
use YamlStandards\Model\Component\YamlService; |
|
12
|
|
|
use YamlStandards\Model\Config\StandardParametersData; |
|
13
|
|
|
use YamlStandards\Result\Result; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Check yaml file is alphabetical sorted |
|
17
|
|
|
*/ |
|
18
|
|
|
class YamlAlphabeticalChecker implements CheckerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritDoc |
|
22
|
|
|
*/ |
|
23
|
3 |
|
public function check(string $pathToYamlFile, StandardParametersData $standardParametersData): Result |
|
24
|
|
|
{ |
|
25
|
3 |
|
$yamlArrayData = YamlService::getYamlData($pathToYamlFile); |
|
26
|
3 |
|
$yamlArrayDataSorted = $this->sortArray($yamlArrayData, $standardParametersData->getDepth()); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
3 |
|
$yamlStringData = Yaml::dump($yamlArrayData, PHP_INT_MAX); |
|
29
|
3 |
|
$yamlStringDataSorted = Yaml::dump($yamlArrayDataSorted, PHP_INT_MAX); |
|
30
|
|
|
|
|
31
|
3 |
|
if ($yamlStringData === $yamlStringDataSorted) { |
|
32
|
3 |
|
return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$differ = new Differ(); |
|
36
|
|
|
$diffBetweenStrings = $differ->diff($yamlStringData, $yamlStringDataSorted); |
|
37
|
|
|
|
|
38
|
|
|
return new Result($pathToYamlFile, Result::RESULT_CODE_INVALID_FILE_SYNTAX, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string[] $yamlArrayData |
|
43
|
|
|
* @param int $depth |
|
44
|
|
|
* @return string[] |
|
45
|
|
|
*/ |
|
46
|
3 |
|
private function sortArray(array $yamlArrayData, int $depth): array |
|
47
|
|
|
{ |
|
48
|
3 |
|
if ($depth > 0) { |
|
49
|
3 |
|
$yamlArrayData = $this->sortArrayKeyWithUnderscoresAsFirst($yamlArrayData); |
|
50
|
|
|
|
|
51
|
3 |
|
if ($depth > 1) { |
|
52
|
3 |
|
foreach ($yamlArrayData as $key => $value) { |
|
53
|
3 |
|
if (is_array($value)) { |
|
54
|
3 |
|
$yamlArrayData[$key] = $this->recursiveKsort($value, $depth); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
return $yamlArrayData; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string[] $yamlArrayData |
|
65
|
|
|
* @param int $depth |
|
66
|
|
|
* @param int $currentDepth |
|
67
|
|
|
* @return string[] |
|
68
|
|
|
*/ |
|
69
|
3 |
|
private function recursiveKsort(array $yamlArrayData, int $depth, int $currentDepth = 1): array |
|
70
|
|
|
{ |
|
71
|
3 |
|
$yamlArrayData = $this->sortArrayKeyWithUnderscoresAsFirst($yamlArrayData); |
|
72
|
3 |
|
foreach ($yamlArrayData as $key => $value) { |
|
73
|
3 |
|
if (is_array($value)) { |
|
74
|
3 |
|
$currentDepth++; |
|
75
|
3 |
|
if ($currentDepth < $depth) { |
|
76
|
3 |
|
$yamlArrayData[$key] = $this->recursiveKsort($value, $depth, $currentDepth); |
|
77
|
|
|
} |
|
78
|
3 |
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
return $yamlArrayData; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string[] $yamlArrayData |
|
87
|
|
|
* @return string[]|string[][] |
|
88
|
|
|
*/ |
|
89
|
3 |
|
private function sortArrayKeyWithUnderscoresAsFirst(array $yamlArrayData): array |
|
90
|
|
|
{ |
|
91
|
3 |
|
$arrayWithUnderscoreKeys = array_filter($yamlArrayData, [YamlService::class, 'hasArrayKeyUnderscoreAsFirstCharacter'], ARRAY_FILTER_USE_KEY); |
|
92
|
3 |
|
$arrayWithOtherKeys = array_filter($yamlArrayData, [YamlService::class, 'hasNotArrayKeyUnderscoreAsFirstCharacter'], ARRAY_FILTER_USE_KEY); |
|
93
|
|
|
|
|
94
|
3 |
|
ksort($arrayWithUnderscoreKeys); |
|
95
|
3 |
|
ksort($arrayWithOtherKeys); |
|
96
|
|
|
|
|
97
|
3 |
|
return array_merge($arrayWithUnderscoreKeys, $arrayWithOtherKeys); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|