Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81ee94d

Browse files
committed
Yaml alphabetical: sort array by prioritized keys
1 parent 0b8b7c8 commit 81ee94d

4 files changed

Lines changed: 52 additions & 12 deletions

File tree

src/Model/YamlAlphabetical/YamlAlphabeticalChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function check(string $pathToYamlFile, StandardParametersData $standardPa
2424
$fileContent = file_get_contents($pathToYamlFile);
2525
$fileContent = str_replace("\r", '', $fileContent); // remove carriage returns
2626

27-
$rightFileLines = YamlAlphabeticalDataFactory::getCorrectYamlLines($pathToYamlFile, $standardParametersData->getDepth());
27+
$rightFileLines = YamlAlphabeticalDataFactory::getCorrectYamlLines($pathToYamlFile, $standardParametersData->getDepth(), $standardParametersData->getAlphabeticalPrioritizedKeys());
2828

2929
$rightFileContent = implode("\n", $rightFileLines);
3030

src/Model/YamlAlphabetical/YamlAlphabeticalDataFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ class YamlAlphabeticalDataFactory
2727
/**
2828
* @param string $pathToYamlFile
2929
* @param int $depth
30+
* @param string[] $prioritizedKeys
3031
* @return string[]
3132
*/
32-
public static function getCorrectYamlLines(string $pathToYamlFile, int $depth): array
33+
public static function getCorrectYamlLines(string $pathToYamlFile, int $depth, array $prioritizedKeys): array
3334
{
3435
self::$index = 0; // start from 0 in every file
3536

3637
$yamlArrayData = YamlParser::getYamlParsedDataFromFile($pathToYamlFile);
37-
$yamlArrayDataSorted = YamlSortService::sortArray($yamlArrayData, $depth);
38+
$yamlArrayDataSorted = YamlSortService::sortArray($yamlArrayData, $depth, $prioritizedKeys);
3839

3940
return self::createRightSortedYamlLines($yamlArrayDataSorted);
4041
}

src/Model/YamlAlphabetical/YamlAlphabeticalFixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function fix(string $pathToYamlFile, string $pathToDumpFixedFile, Standar
2424
$fileContent = file_get_contents($pathToYamlFile);
2525
$fileContent = str_replace("\r", '', $fileContent); // remove carriage returns
2626

27-
$rightFileLines = YamlAlphabeticalDataFactory::getCorrectYamlLines($pathToYamlFile, $standardParametersData->getDepth());
27+
$rightFileLines = YamlAlphabeticalDataFactory::getCorrectYamlLines($pathToYamlFile, $standardParametersData->getDepth(), $standardParametersData->getAlphabeticalPrioritizedKeys());
2828

2929
$rightFileContent = implode("\n", $rightFileLines);
3030

src/Model/YamlAlphabetical/YamlSortService.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ class YamlSortService
1111
/**
1212
* @param string[] $yamlArrayData
1313
* @param int $depth
14+
* @param string[] $prioritizedKeys
1415
* @return string[]
1516
*/
16-
public static function sortArray(array $yamlArrayData, int $depth): array
17+
public static function sortArray(array $yamlArrayData, int $depth, array $prioritizedKeys): array
1718
{
1819
if ($depth > 0) {
19-
$yamlArrayData = self::sortArrayKeyWithUnderscoresAsFirst($yamlArrayData);
20+
$yamlArrayData = self::sortArrayKeyWithUnderscoresAsFirst($yamlArrayData, $prioritizedKeys);
2021

2122
if ($depth > 1) {
2223
foreach ($yamlArrayData as $key => $value) {
2324
if (is_array($value)) {
24-
$yamlArrayData[$key] = self::recursiveKsort($value, $depth);
25+
$yamlArrayData[$key] = self::recursiveKsort($value, $depth, $prioritizedKeys);
2526
}
2627
}
2728
}
@@ -33,16 +34,17 @@ public static function sortArray(array $yamlArrayData, int $depth): array
3334
/**
3435
* @param string[] $yamlArrayData
3536
* @param int $depth
37+
* @param string[] $prioritizedKeys
3638
* @param int $currentDepth
3739
* @return string[]
3840
*/
39-
private static function recursiveKsort(array $yamlArrayData, int $depth, int $currentDepth = 1): array
41+
private static function recursiveKsort(array $yamlArrayData, int $depth, array $prioritizedKeys, int $currentDepth = 1): array
4042
{
41-
$yamlArrayData = self::sortArrayKeyWithUnderscoresAsFirst($yamlArrayData);
43+
$yamlArrayData = self::sortArrayKeyWithUnderscoresAsFirst($yamlArrayData, $prioritizedKeys);
4244
foreach ($yamlArrayData as $key => $value) {
4345
if (is_array($value)) {
4446
if ($currentDepth <= $depth) {
45-
$yamlArrayData[$key] = self::recursiveKsort($value, $depth, $currentDepth + 1);
47+
$yamlArrayData[$key] = self::recursiveKsort($value, $depth, $prioritizedKeys, $currentDepth + 1);
4648
}
4749
}
4850
}
@@ -52,17 +54,20 @@ private static function recursiveKsort(array $yamlArrayData, int $depth, int $cu
5254

5355
/**
5456
* @param string[] $yamlArrayData
57+
* @param string[] $prioritizedKeys
5558
* @return string[]|string[][]
5659
*/
57-
private static function sortArrayKeyWithUnderscoresAsFirst(array $yamlArrayData): array
60+
private static function sortArrayKeyWithUnderscoresAsFirst(array $yamlArrayData, array $prioritizedKeys): array
5861
{
5962
$arrayWithUnderscoreKeys = array_filter($yamlArrayData, [YamlService::class, 'hasArrayKeyUnderscoreAsFirstCharacter'], ARRAY_FILTER_USE_KEY);
6063
$arrayWithOtherKeys = array_filter($yamlArrayData, [YamlService::class, 'hasNotArrayKeyUnderscoreAsFirstCharacter'], ARRAY_FILTER_USE_KEY);
6164

6265
uksort($arrayWithUnderscoreKeys, ['self', 'sortArrayAlphabetical']);
6366
uksort($arrayWithOtherKeys, ['self', 'sortArrayAlphabetical']);
6467

65-
return array_merge($arrayWithUnderscoreKeys, $arrayWithOtherKeys);
68+
$arrayData = array_merge($arrayWithUnderscoreKeys, $arrayWithOtherKeys);
69+
70+
return self::sortArrayElementsByPrioritizedKeys($prioritizedKeys, $arrayData);
6671
}
6772

6873
/**
@@ -106,4 +111,38 @@ private static function sortArrayAlphabetical(string $key1, string $key2): int
106111

107112
return strnatcmp(trim($key1WithoutNumberAtEnd), trim($key2WithoutNumberAtEnd));
108113
}
114+
115+
/**
116+
* @param string[] $prioritizedKeys
117+
* @param string[] $arrayData
118+
* @return string[]
119+
*/
120+
private static function sortArrayElementsByPrioritizedKeys(array $prioritizedKeys, array $arrayData): array
121+
{
122+
$positionTo = 0;
123+
foreach ($prioritizedKeys as $prioritizedKey) {
124+
$foundKeys = preg_grep('/' . $prioritizedKey . '/', array_keys($arrayData));
125+
foreach ($foundKeys as $foundKey) {
126+
$positionFrom = (int)array_search($foundKey, array_keys($arrayData), true);
127+
self::changeElementPositionInArray($arrayData, $positionFrom, $positionTo);
128+
$positionTo++;
129+
}
130+
}
131+
132+
return $arrayData;
133+
}
134+
135+
/**
136+
* @param string[] $array
137+
* @param int $positionFrom
138+
* @param int $positionTo
139+
*
140+
* @link https://stackoverflow.com/a/28831998
141+
*/
142+
private static function changeElementPositionInArray(array &$array, int $positionFrom, int $positionTo): void
143+
{
144+
$p1 = array_splice($array, $positionFrom, 1);
145+
$p2 = array_splice($array, 0, $positionTo);
146+
$array = array_merge($p2, $p1, $array);
147+
}
109148
}

0 commit comments

Comments
 (0)