XmlFormatter::validate()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 1
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 ($structuredData instanceof \ArrayObject) {
55
            return $structuredData->getArrayCopy();
56
        }
57
        if (!is_array($structuredData)) {
58
            throw new IncompatibleDataException(
59
                $this,
60
                $structuredData,
61
                $this->validDataTypes()
62
            );
63
        }
64
        return $structuredData;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function write(OutputInterface $output, $dom, FormatterOptions $options)
71
    {
72
        if (is_array($dom)) {
73
            $schema = $options->getXmlSchema();
74
            $dom = $schema->arrayToXML($dom);
75
        }
76
        $dom->formatOutput = true;
77
        $output->writeln($dom->saveXML());
78
    }
79
}
80