@@ -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