Skip to content

Added node:list sort order #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dev-master

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

### Bug fixes

Expand Down
16 changes: 16 additions & 0 deletions features/all/phpcr_node_list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,19 @@ Feature: List properites and chidren of current nodeA
"""
5 nodes in set
"""

Scenario: List with sort order ascending
When I execute the "node:list --sort=asc" command
Then the command should not fail

Scenario: List with sort order descending
When I execute the "node:list --sort=desc" command
Then the command should not fail

Scenario: List with no sorting
When I execute the "node:list --sort=none" command
Then the command should not fail

Scenario: List with invalid sorting
When I execute the "node:list --sort=foobar" command
Then the command should fail
33 changes: 32 additions & 1 deletion src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

class NodeListCommand extends BasePhpcrCommand
{
protected $sortOptions = array('none', 'asc', 'desc');

protected $formatter;
protected $textHelper;
protected $maxLevel;
Expand All @@ -38,6 +40,10 @@ protected function configure()
$this->addOption('properties', null, InputOption::VALUE_NONE, 'List only the properties of this node');
$this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show');
$this->addOption('template', 't', InputOption::VALUE_NONE, 'Show template nodes and properties');
$this->addOption('sort', 's', InputOption::VALUE_REQUIRED, sprintf(
'Sort properties, one of: <comment>%s</comment>',
implode('</comment>, <comment>', $this->sortOptions)
), 'asc');
$this->setHelp(<<<HERE
List both or one of the children and properties of this node.

Expand All @@ -61,6 +67,15 @@ public function execute(InputInterface $input, OutputInterface $output)
$this->formatter = $this->get('helper.result_formatter');
$this->textHelper = $this->get('helper.text');
$this->maxLevel = $input->getOption('level');
$this->sort = strtolower($input->getOption('sort'));

if (!in_array($this->sort, $this->sortOptions)) {
throw new \InvalidArgumentException(sprintf(
'Sort must be one of "%s". "%s" given',
implode('", "', $this->sortOptions),
$this->sort
));
}

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

private function renderProperties($currentNode, $table, $spacers, $filter = null)
{
$properties = $currentNode->getProperties($filter ?: null);
$properties = (array) $currentNode->getProperties($filter ?: null);
$properties = $this->sort($properties);

try {
$primaryItem = $currentNode->getPrimaryItem();
Expand Down Expand Up @@ -248,4 +264,19 @@ private function renderProperties($currentNode, $table, $spacers, $filter = null
}
}
}

private function sort($array)
{
switch ($this->sort) {
case 'asc':
ksort($array);
return $array;
case 'desc':
ksort($array);
$array = array_reverse($array);
return $array;
}

return $array;
}
}