Version 1
[yaffs-website] / vendor / consolidation / output-formatters / src / Formatters / XmlFormatter.php
1 <?php
2 namespace Consolidation\OutputFormatters\Formatters;
3
4 use Symfony\Component\Console\Output\OutputInterface;
5 use Symfony\Component\Console\Helper\Table;
6 use Symfony\Component\Console\Helper\TableStyle;
7
8 use Consolidation\OutputFormatters\Validate\ValidDataTypesInterface;
9 use Consolidation\OutputFormatters\Options\FormatterOptions;
10 use Consolidation\OutputFormatters\Validate\ValidDataTypesTrait;
11 use Consolidation\OutputFormatters\StructuredData\TableDataInterface;
12 use Consolidation\OutputFormatters\Transformations\ReorderFields;
13 use Consolidation\OutputFormatters\Exception\IncompatibleDataException;
14 use Consolidation\OutputFormatters\StructuredData\Xml\DomDataInterface;
15
16 /**
17  * Display a table of data with the Symfony Table class.
18  *
19  * This formatter takes data of either the RowsOfFields or
20  * PropertyList data type.  Tables can be rendered with the
21  * rows running either vertically (the normal orientation) or
22  * horizontally.  By default, associative lists will be displayed
23  * as two columns, with the key in the first column and the
24  * value in the second column.
25  */
26 class XmlFormatter implements FormatterInterface, ValidDataTypesInterface
27 {
28     use ValidDataTypesTrait;
29
30     public function __construct()
31     {
32     }
33
34     public function validDataTypes()
35     {
36         return
37             [
38                 new \ReflectionClass('\DOMDocument'),
39                 new \ReflectionClass('\ArrayObject'),
40             ];
41     }
42
43     /**
44      * @inheritdoc
45      */
46     public function validate($structuredData)
47     {
48         if ($structuredData instanceof \DOMDocument) {
49             return $structuredData;
50         }
51         if ($structuredData instanceof DomDataInterface) {
52             return $structuredData->getDomData();
53         }
54         if (!is_array($structuredData)) {
55             throw new IncompatibleDataException(
56                 $this,
57                 $structuredData,
58                 $this->validDataTypes()
59             );
60         }
61         return $structuredData;
62     }
63
64     /**
65      * @inheritdoc
66      */
67     public function write(OutputInterface $output, $dom, FormatterOptions $options)
68     {
69         if (is_array($dom)) {
70             $schema = $options->getXmlSchema();
71             $dom = $schema->arrayToXML($dom);
72         }
73         $dom->formatOutput = true;
74         $output->writeln($dom->saveXML());
75     }
76 }