|
| 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