Skip to content

Commit 6e1d805

Browse files
committed
Merge pull request #143 from phpcr/node_list_order
Added node:list sort order
2 parents cf56769 + a0c69b5 commit 6e1d805

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

CHANGELOG.md

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

1010
- [profile:show] Added command to display current profile
1111
- [query:update] Introduced `expr()` function to allow setting poperty values using expression language.
12+
- [node:list] Sort properties by default, added option to specify
1213

1314
### Bug fixes
1415

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: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
class NodeListCommand extends BasePhpcrCommand
2525
{
26+
protected $sortOptions = array('none', 'asc', 'desc');
27+
2628
protected $formatter;
2729
protected $textHelper;
2830
protected $maxLevel;
@@ -38,6 +40,10 @@ 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, sprintf(
44+
'Sort properties, one of: <comment>%s</comment>',
45+
implode('</comment>, <comment>', $this->sortOptions)
46+
), 'asc');
4147
$this->setHelp(<<<HERE
4248
List both or one of the children and properties of this node.
4349
@@ -61,6 +67,15 @@ public function execute(InputInterface $input, OutputInterface $output)
6167
$this->formatter = $this->get('helper.result_formatter');
6268
$this->textHelper = $this->get('helper.text');
6369
$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+
}
6479

6580
$this->showChildren = $input->getOption('children');
6681
$this->showProperties = $input->getOption('properties');
@@ -198,7 +213,8 @@ private function renderChildren($currentNode, $table, $spacers, $filter = null)
198213

199214
private function renderProperties($currentNode, $table, $spacers, $filter = null)
200215
{
201-
$properties = $currentNode->getProperties($filter ?: null);
216+
$properties = (array) $currentNode->getProperties($filter ?: null);
217+
$properties = $this->sort($properties);
202218

203219
try {
204220
$primaryItem = $currentNode->getPrimaryItem();
@@ -248,4 +264,19 @@ private function renderProperties($currentNode, $table, $spacers, $filter = null
248264
}
249265
}
250266
}
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+
}
251282
}

0 commit comments

Comments
 (0)