23
23
24
24
class NodeListCommand extends BasePhpcrCommand
25
25
{
26
+ protected $ sortOptions = array ('none ' , 'asc ' , 'desc ' );
27
+
26
28
protected $ formatter ;
27
29
protected $ textHelper ;
28
30
protected $ maxLevel ;
@@ -38,6 +40,10 @@ protected function configure()
38
40
$ this ->addOption ('properties ' , null , InputOption::VALUE_NONE , 'List only the properties of this node ' );
39
41
$ this ->addOption ('level ' , 'L ' , InputOption::VALUE_REQUIRED , 'Depth of tree to show ' );
40
42
$ this ->addOption ('template ' , 't ' , InputOption::VALUE_NONE , 'Show template nodes and properties ' );
43
+ $ this ->addOption ('sort ' , 's ' , InputOption::VALUE_REQUIRED , sprintf (
44
+ 'Sort properties, one of: <comment>%s</comment> ' ,
45
+ implode ('</comment>, <comment> ' , $ this ->sortOptions )
46
+ ), 'asc ' );
41
47
$ this ->setHelp (<<<HERE
42
48
List both or one of the children and properties of this node.
43
49
@@ -61,6 +67,15 @@ public function execute(InputInterface $input, OutputInterface $output)
61
67
$ this ->formatter = $ this ->get ('helper.result_formatter ' );
62
68
$ this ->textHelper = $ this ->get ('helper.text ' );
63
69
$ this ->maxLevel = $ input ->getOption ('level ' );
70
+ $ this ->sort = strtolower ($ input ->getOption ('sort ' ));
71
+
72
+ if (!in_array ($ this ->sort , $ this ->sortOptions )) {
73
+ throw new \InvalidArgumentException (sprintf (
74
+ 'Sort must be one of "%s". "%s" given ' ,
75
+ implode ('", " ' , $ this ->sortOptions ),
76
+ $ this ->sort
77
+ ));
78
+ }
64
79
65
80
$ this ->showChildren = $ input ->getOption ('children ' );
66
81
$ this ->showProperties = $ input ->getOption ('properties ' );
@@ -198,7 +213,8 @@ private function renderChildren($currentNode, $table, $spacers, $filter = null)
198
213
199
214
private function renderProperties ($ currentNode , $ table , $ spacers , $ filter = null )
200
215
{
201
- $ properties = $ currentNode ->getProperties ($ filter ?: null );
216
+ $ properties = (array ) $ currentNode ->getProperties ($ filter ?: null );
217
+ $ properties = $ this ->sort ($ properties );
202
218
203
219
try {
204
220
$ primaryItem = $ currentNode ->getPrimaryItem ();
@@ -248,4 +264,19 @@ private function renderProperties($currentNode, $table, $spacers, $filter = null
248
264
}
249
265
}
250
266
}
267
+
268
+ private function sort ($ array )
269
+ {
270
+ switch ($ this ->sort ) {
271
+ case 'asc ' :
272
+ ksort ($ array );
273
+ return $ array ;
274
+ case 'desc ' :
275
+ ksort ($ array );
276
+ $ array = array_reverse ($ array );
277
+ return $ array ;
278
+ }
279
+
280
+ return $ array ;
281
+ }
251
282
}
0 commit comments