Skip to content

Commit bca46d3

Browse files
committed
Added node:list sort order
1 parent cf0154f commit bca46d3

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dev-master
88
### Features
99

1010
- [profile:show] Added command to display current profile
11+
- [node:list] Sort properties by default, added option to specify
1112

1213
### Bug fixes
1314

features/all/phpcr_node_list.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,19 @@ Feature: List properites and chidren of current nodeA
143143
"""
144144
5 nodes in set
145145
"""
146+
147+
Scenario: List with sort order ascending
148+
When I execute the "node:list --sort=asc" command
149+
Then the command should not fail
150+
151+
Scenario: List with sort order descending
152+
When I execute the "node:list --sort=desc" command
153+
Then the command should not fail
154+
155+
Scenario: List with no sorting
156+
When I execute the "node:list --sort=none" command
157+
Then the command should not fail
158+
159+
Scenario: List with invalid sorting
160+
When I execute the "node:list --sort=foobar" command
161+
Then the command should fail

src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
class NodeListCommand extends BasePhpcrCommand
2525
{
26+
const SORT_OPTIONS = array('none', 'asc', 'desc');
27+
2628
protected $formatter;
2729
protected $textHelper;
2830
protected $maxLevel;
@@ -38,6 +40,7 @@ protected function configure()
3840
$this->addOption('properties', null, InputOption::VALUE_NONE, 'List only the properties of this node');
3941
$this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show');
4042
$this->addOption('template', 't', InputOption::VALUE_NONE, 'Show template nodes and properties');
43+
$this->addOption('sort', 's', InputOption::VALUE_REQUIRED, 'Sort properties, one of <comment>asc</comment>, <comment>desc</comment>, or <comment>none</comment>', 'asc');
4144
$this->setHelp(<<<HERE
4245
List both or one of the children and properties of this node.
4346
@@ -61,6 +64,15 @@ public function execute(InputInterface $input, OutputInterface $output)
6164
$this->formatter = $this->get('helper.result_formatter');
6265
$this->textHelper = $this->get('helper.text');
6366
$this->maxLevel = $input->getOption('level');
67+
$this->sort = strtolower($input->getOption('sort'));
68+
69+
if (!in_array($this->sort, self::SORT_OPTIONS)) {
70+
throw new \InvalidArgumentException(sprintf(
71+
'Sort must be one of "%s". "%s" given',
72+
implode('", "', self::SORT_OPTIONS),
73+
$this->sort
74+
));
75+
}
6476

6577
$this->showChildren = $input->getOption('children');
6678
$this->showProperties = $input->getOption('properties');
@@ -198,7 +210,8 @@ private function renderChildren($currentNode, $table, $spacers, $filter = null)
198210

199211
private function renderProperties($currentNode, $table, $spacers, $filter = null)
200212
{
201-
$properties = $currentNode->getProperties($filter ?: null);
213+
$properties = (array) $currentNode->getProperties($filter ?: null);
214+
$properties = $this->sort($properties);
202215

203216
try {
204217
$primaryItem = $currentNode->getPrimaryItem();
@@ -248,4 +261,19 @@ private function renderProperties($currentNode, $table, $spacers, $filter = null
248261
}
249262
}
250263
}
264+
265+
private function sort($array)
266+
{
267+
switch ($this->sort) {
268+
case 'asc':
269+
ksort($array);
270+
return $array;
271+
case 'desc':
272+
ksort($array);
273+
$array = array_reverse($array);
274+
return $array;
275+
}
276+
277+
return $array;
278+
}
251279
}

0 commit comments

Comments
 (0)