@@ -31,42 +31,33 @@ class SortingService
31
31
protected $ logger ;
32
32
33
33
/**
34
- * @param NodeInterface $parentNode
34
+ * @param NodeInterface $node
35
35
* @param string $eelOrProperty
36
36
* @param string $nodeTypeFilter
37
37
*/
38
- public function sortChildren (NodeInterface $ parentNode , string $ eelOrProperty , $ nodeTypeFilter )
38
+ public function sortChildren (NodeInterface $ node , string $ eelOrProperty , $ nodeTypeFilter )
39
39
{
40
40
if ($ this ->eelEvaluationService ->isValidExpression ($ eelOrProperty )) {
41
41
$ eelExpression = $ eelOrProperty ;
42
42
} else {
43
43
$ eelExpression = sprintf ('${String.toLowerCase(q(a).property("%s")) < String.toLowerCase(q(b).property("%s"))} ' , $ eelOrProperty , $ eelOrProperty );
44
44
}
45
- $ this ->sortChildNodesByEelExpression ($ parentNode , $ eelExpression , $ nodeTypeFilter );
45
+
46
+ $ this ->moveNodeToCorrectPosition ($ node , $ eelExpression , $ nodeTypeFilter );
46
47
}
47
48
48
49
/**
49
- * @param NodeInterface $parenNode
50
+ * @param NodeInterface $nodeToBeSorted
50
51
* @param string $eelExpression
51
- * @param string $nodeTypeFilter
52
- * @return void
52
+ * @param $nodeTypeFilter
53
53
*/
54
- protected function sortChildNodesByEelExpression (NodeInterface $ parenNode , string $ eelExpression , $ nodeTypeFilter )
55
- {
56
- $ nodes = $ parenNode ->getChildNodes ($ nodeTypeFilter );
57
-
58
- foreach ($ nodes as $ nodeA ) {
59
-
60
- /** @var NodeInterface $nodeB */
61
- foreach ($ nodes as $ nodeB ) {
62
- if ($ this ->eelEvaluationService ->evaluate ($ eelExpression , ['a ' => $ nodeA , 'b ' => $ nodeB ])) {
54
+ protected function moveNodeToCorrectPosition (NodeInterface $ nodeToBeSorted , string $ eelExpression , $ nodeTypeFilter ) {
55
+ $ nodes = $ nodeToBeSorted ->getParent ()->getChildNodes ($ nodeTypeFilter );
63
56
64
- if ($ nodeB !== $ nodeA ) {
65
- $ this ->logger ->log (sprintf ('Moving node %s before %s ' , $ nodeA ->getPath (), $ nodeB ->getPath ()), LOG_DEBUG );
66
- $ nodeA ->moveBefore ($ nodeB );
67
- break ;
68
- }
69
- }
57
+ foreach ($ nodes as $ node ) {
58
+ if ($ this ->eelEvaluationService ->evaluate ($ eelExpression , ['a ' => $ nodeToBeSorted , 'b ' => $ node ])) {
59
+ $ nodeToBeSorted ->moveBefore ($ node );
60
+ break ;
70
61
}
71
62
}
72
63
}
0 commit comments