Skip to content

Commit 2ecf2b5

Browse files
committed
Query result formatting
1 parent 2c01188 commit 2ecf2b5

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPCR\Shell\Console\Command\Workspace\NodeTypeListCommand;
1212
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
1313
use PHPCR\Shell\Console\Command\Workspace\NodeDumpCommand;
14+
use PHPCR\Shell\Console\Helper\ResultFormatterHelper;
1415

1516
class ShellApplication extends Application
1617
{
@@ -23,6 +24,7 @@ public function __construct(SessionInterface $session)
2324
$this->add(new NodeDumpCommand());
2425

2526
$this->getHelperSet()->set(new PhpcrConsoleDumperHelper());
27+
$this->getHelperSet()->set(new ResultFormatterHelper());
2628

2729
foreach ($this->all() as $command) {
2830
if ($command instanceof AbstractSessionCommand) {

src/PHPCR/Shell/Console/Command/Workspace/SelectCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ public function execute(InputInterface $input, OutputInterface $output)
4343
$start = microtime(true);
4444
$result = $query->execute();
4545
$elapsed = microtime(true) - $start;
46+
47+
$this->getHelper('result_formatter')->format($result, $output, $elapsed);
4648
}
4749
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Helper;
4+
5+
use Symfony\Component\Console\Helper\Helper;
6+
use PHPCR\Query\QueryResultInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use PHPCR\PropertyType;
9+
10+
class ResultFormatterHelper extends Helper
11+
{
12+
public function getName()
13+
{
14+
return 'result_formatter';
15+
}
16+
17+
public function format(QueryResultInterface $result, OutputInterface $output)
18+
{
19+
$selectorNames = $result->getSelectorNames();
20+
foreach ($result->getRows() as $i => $row) {
21+
$output->writeln($i);
22+
foreach ($selectorNames as $selectorName) {
23+
$node = $row->getNode($selectorName);
24+
$properties = $node->getProperties();
25+
$output->writeln(' ' . $selectorName);
26+
$output->writeln(' <comment>' . $node->getPath().'</comment>');
27+
28+
foreach ($properties as $key => $value) {
29+
$output->writeln(sprintf(' <info>%s</info> %s%s: %s',
30+
$key,
31+
PropertyType::nameFromValue($value->getType()),
32+
$value->isMultiple() ? '[]' : '',
33+
$this->formatValue($value)
34+
));
35+
}
36+
}
37+
}
38+
}
39+
40+
protected function formatValue($value)
41+
{
42+
if (is_array($value->getValue())) {
43+
if (empty($value->getValue())) {
44+
return '';
45+
}
46+
47+
return "\n - " . implode("\n - ", $value->getvalue());
48+
}
49+
50+
switch (intval($value->getType())) {
51+
case PropertyType::UNDEFINED :
52+
return '#UNDEFINED#';
53+
case PropertyType::BINARY :
54+
return '(binary data)';
55+
case PropertyType::BOOLEAN :
56+
return $value->getValue() ? 'true' : 'false';
57+
case PropertyType::DATE :
58+
return $value->getValue()->format('c');
59+
case PropertyType::REFERENCE :
60+
case PropertyType::WEAKREFERENCE :
61+
return $value->getValue()->getPath();
62+
case PropertyType::URI :
63+
case PropertyType::STRING :
64+
case PropertyType::NAME :
65+
case PropertyType::LONG :
66+
case PropertyType::DOUBLE :
67+
case PropertyType::DECIMAL :
68+
case PropertyType::PATH :
69+
return $value->getValue();
70+
default:
71+
throw new \RuntimeException('Unknown type ' . $value->getType());
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)