-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathResultFormatterHelper.php
185 lines (157 loc) · 5.15 KB
/
ResultFormatterHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace PHPCR\Shell\Console\Helper;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\TableHelper;
use PHPCR\Query\QueryResultInterface;
use Symfony\Component\Console\Output\OutputInterface;
use PHPCR\PropertyType;
use PHPCR\NodeInterface;
use PHPCR\PropertyInterface;
/**
* Provide methods for formatting PHPCR objects
*
* @TODO: Rename this to PhpcrFormatterHelper
*/
class ResultFormatterHelper extends Helper
{
/**
* {@inheritDoc}
*/
public function getName()
{
return 'result_formatter';
}
/**
* Return the name of a property from its enumeration (i.e.
* the value of its CONSTANT)
*
* @return string
*/
public function getPropertyTypeName($typeInteger)
{
$refl = new \ReflectionClass('PHPCR\PropertyType');
foreach ($refl->getConstants() as $key => $value) {
if ($typeInteger == $value) {
return $key;
}
}
}
/**
* Render a table with the results of the given QueryResultInterface
*/
public function formatQueryResult(QueryResultInterface $result, OutputInterface $output, $elapsed)
{
$selectorNames = $result->getSelectorNames();
$table = new TableHelper;
$table->setHeaders(array_merge(array(
'Path',
), $result->getColumnNames()));
foreach ($result->getRows() as $i => $row) {
$values = array_merge(array(
$row->getPath(),
), $row->getValues());
foreach ($values as $columnName => &$value) {
$value = $this->normalizeValue($value);
}
$table->addRow($values);
}
$table->render($output);
$output->writeln(sprintf('%s rows in set (%s sec)', count($result->getRows()), number_format($elapsed, 2)));
}
public function normalizeValue($value)
{
if (is_array($value)) {
if (empty($value)) {
return '';
}
$array = $value;
$values = array();
foreach ($array as $i => $value) {
if ($value instanceof NodeInterface) {
$value = $value->getPath();
} elseif (is_object($value)) {
$value = '<UNKNOWN OBJECT>';
} else {
$value = $value;
}
$value = '[' . $i . '] ' . $value;
$values[] = $value;
}
return implode("\n", $values);
}
if ($value instanceof \DateTime) {
return $value->format('c');
}
return $value;
}
public function formatValue(PropertyInterface $value, $showBinary = false)
{
$v = $value->getValue();
if (is_array($v)) {
return $this->normalizeValue($v);
}
switch (intval($value->getType())) {
case PropertyType::UNDEFINED :
return '#UNDEFINED#';
case PropertyType::BINARY :
if ($showBinary) {
$lines = array();
$pointer = $value->getValue();
while (($line = fgets($pointer)) !== false) {
$lines[] = $line;
}
return implode('', $lines);
}
return '(binary data)';
case PropertyType::BOOLEAN :
return $value->getValue() ? 'true' : 'false';
case PropertyType::DATE :
return $value->getValue()->format('c');
case PropertyType::REFERENCE :
case PropertyType::WEAKREFERENCE :
return $value->getValue()->getIdentifier();
case PropertyType::URI :
case PropertyType::STRING :
case PropertyType::NAME :
case PropertyType::LONG :
case PropertyType::DOUBLE :
case PropertyType::DECIMAL :
case PropertyType::PATH :
return $value->getValue();
default:
throw new \RuntimeException('Unknown type ' . $value->getType());
}
}
public function formatNodePropertiesInline(NodeInterface $node)
{
$out = array();
foreach ($node->getProperties() as $property) {
$out[] = sprintf('%s: %s',
$property->getName(),
$this->formatValue($property)
);
}
return implode(', ', $out);
}
public function formatNodeName(NodeInterface $node)
{
return sprintf('%s%s', $node->getName(), $node->hasNodes() ? '/' : '');
}
public function formatException(\Exception $e)
{
if ($e instanceof \Jackalope\NotImplementedException) {
return '[ERROR] Not implemented by jackalope';
}
return sprintf('[%s] %s', get_class($e), $e->getMessage());
}
public function formatXml($xmlString)
{
// format output
$dom = new \DOMDocument('1.0');
$dom->loadXml($xmlString);
$dom->preserveWhitespace = true;
$dom->formatOutput = true;
$out = $dom->saveXml();
return $out;
}
}