Completed
Pull Request — master (#16)
by Greg
02:48
created

FormatterManager::restructureData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
crap 2
1
<?php
2
namespace Consolidation\OutputFormatters;
3
4
use Symfony\Component\Console\Output\OutputInterface;
5
use Consolidation\OutputFormatters\FormatterInterface;
6
use Consolidation\OutputFormatters\Exception\UnknownFormatException;
7
use Consolidation\OutputFormatters\Formatters\RenderDataInterface;
8
9
/**
10
 * Manage a collection of formatters; return one on request.
11
 */
12
class FormatterManager
13
{
14
    protected $formatters = [];
15
16 28
    public function __construct()
17
    {
18 28
        $this->formatters = [
19 28
            'string' => '\Consolidation\OutputFormatters\Formatters\StringFormatter',
20 28
            'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter',
21 28
            'json' => '\Consolidation\OutputFormatters\Formatters\JsonFormatter',
22 28
            'print-r' => '\Consolidation\OutputFormatters\Formatters\PrintRFormatter',
23 28
            'php' => '\Consolidation\OutputFormatters\Formatters\SerializeFormatter',
24 28
            'var_export' => '\Consolidation\OutputFormatters\Formatters\VarExportFormatter',
25 28
            'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter',
26 28
            'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter',
27 28
            'table' => '\Consolidation\OutputFormatters\Formatters\TableFormatter',
28
        ];
29
30
        // Make the empty format an alias for the 'string' formatter.
31 28
        $this->formatters[''] = $this->formatters['string'];
32 28
    }
33
34
    /**
35
     * Format and write output
36
     *
37
     * @param OutputInterface $output Output stream to write to
38
     * @param string $format Data format to output in
39
     * @param mixed $structuredOutput Data to output
40
     * @param array $configurationData Configuration information for formatter
41
     * @param array $options User options
42
     */
43 28
    public function write(OutputInterface $output, $format, $structuredOutput, $configurationData = [], $options = [])
44
    {
45 28
        $formatter = $this->getFormatter((string)$format, $configurationData);
46 27
        $structuredOutput = $this->validateAndRestructure($formatter, $structuredOutput, $configurationData, $options);
47 24
        $formatter->write($output, $structuredOutput, $options);
48 24
    }
49
50 27
    protected function validateAndRestructure(FormatterInterface $formatter, $structuredOutput, $configurationData, $options)
51
    {
52
        // Give the formatter a chance to do something with the
53
        // raw data before it is restructured.
54 27
        $overrideRestructure = $this->overrideRestructure($formatter, $structuredOutput, $configurationData, $options);
55 27
        if ($overrideRestructure) {
56 4
            return $overrideRestructure;
57
        }
58
59
        // Restructure the output data (e.g. select fields to display, etc.).
60 27
        $restructuredOutput = $this->restructureData($structuredOutput, $configurationData, $options);
61
62
        // Make sure that the provided data is in the correct format for the selected formatter.
63 27
        $restructuredOutput = $this->validateData($formatter, $restructuredOutput);
64
65
        // Give the original data a chance to re-render the structured
66
        // output after it has been restructured and validated.
67 24
        $restructuredOutput = $this->renderData($formatter, $structuredOutput, $restructuredOutput, $configurationData, $options);
68
69 24
        return $restructuredOutput;
70
    }
71
72
    /**
73
     * Fetch the requested formatter.
74
     *
75
     * @param string $format Identifier for requested formatter
76
     * @param array $configurationData Configuration data for formatter
77
     * @return FormatterInterface
78
     */
79 28
    public function getFormatter($format, $configurationData = [])
80
    {
81 28
        if (!$this->hasFormatter($format)) {
82 1
            throw new UnknownFormatException($format);
83
        }
84
85 27
        $formatter = new $this->formatters[$format];
86 27
        if ($formatter instanceof ConfigureInterface) {
87 9
            $formatter->configure($configurationData);
88 9
        }
89 27
        return $formatter;
90
    }
91
92 28
    public function hasFormatter($format)
93
    {
94 28
        return array_key_exists($format, $this->formatters);
95
    }
96
97
    /**
98
     * Render the data as necessary (e.g. to select or reorder fields).
99
     *
100
     * @param mixed $structuredOutput
0 ignored issues
show
Bug introduced by
There is no parameter named $structuredOutput. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
101
     * @param array $configurationData
102
     * @param array $options
103
     * @return mixed
104
     */
105 24
    public function renderData(FormatterInterface $formatter, $originalData, $restructuredData, $configurationData, $options)
106
    {
107 24
        if ($formatter instanceof RenderDataInterface) {
108 14
            return $formatter->renderData($originalData, $restructuredData, $configurationData, $options);
109
        }
110 14
        return $restructuredData;
111
    }
112
113
    /**
114
     * Determine if the provided data is compatible with the formatter being used.
115
     *
116
     * @param FormatterInterface $formatter Formatter being used
117
     * @param mixed $structuredOutput Data to validate
118
     * @return mixed
119
     */
120 27
    public function validateData(FormatterInterface $formatter, $structuredOutput)
121
    {
122
        // If the formatter implements ValidationInterface, then let it
123
        // test the data and throw or return an error
124 27
        if ($formatter instanceof ValidationInterface) {
125 16
            return $formatter->validate($structuredOutput);
126
        }
127
        // If the formatter does not implement ValidationInterface, then
128
        // it will never be passed an ArrayObject; we will always give
129
        // it a simple array.
130 15
        if ($structuredOutput instanceof \ArrayObject) {
131 4
            return $structuredOutput->getArrayCopy();
132
        }
133
134 11
        return $structuredOutput;
135
    }
136
137
    /**
138
     * Restructure the data as necessary (e.g. to select or reorder fields).
139
     *
140
     * @param mixed $structuredOutput
141
     * @param array $configurationData
142
     * @param array $options
143
     * @return mixed
144
     */
145 27
    public function restructureData($structuredOutput, $configurationData, $options)
146
    {
147 27
        if ($structuredOutput instanceof RestructureInterface) {
148 7
            return $structuredOutput->restructure($configurationData, $options);
149
        }
150 20
        return $structuredOutput;
151
    }
152
153
    /**
154
     * Allow the formatter access to the raw structured data prior
155
     * to restructuring.  For example, the 'list' formatter may wish
156
     * to display the row keys when provided table output.  If this
157
     * function returns a result that does not evaluate to 'false',
158
     * then that result will be used as-is, and restructuring and
159
     * validation will not occur.
160
     *
161
     * @param mixed $structuredOutput
162
     * @param array $configurationData
163
     * @param array $options
164
     * @return mixed
165
     */
166 27
    public function overrideRestructure(FormatterInterface $formatter, $structuredOutput, $configurationData, $options)
167
    {
168 27
        if ($formatter instanceof OverrideRestructureInterface) {
169 5
            return $formatter->overrideRestructure($structuredOutput, $configurationData, $options);
170
        }
171 26
    }
172
}
173