1 | <?php |
||
14 | class FormatterManager |
||
15 | { |
||
16 | 28 | protected $formatters = []; |
|
17 | protected $arraySimplifiers = []; |
||
18 | 28 | ||
19 | 28 | public function __construct() |
|
42 | |||
43 | 28 | /** |
|
44 | * Add a formatter |
||
45 | 28 | * |
|
46 | 27 | * @param string $key the identifier of the formatter to add |
|
47 | 24 | * @param string $formatterClassname the class name of the formatter to add |
|
48 | 24 | * @return FormatterManager |
|
49 | */ |
||
50 | 27 | public function addFormatter($key, $formatterClassname) |
|
55 | 27 | ||
56 | 4 | /** |
|
57 | * Add a simplifier |
||
58 | * |
||
59 | * @param SimplifyToArrayInterface $simplifier the array simplifier to add |
||
60 | 27 | * @return FormatterManager |
|
61 | */ |
||
62 | public function addSimplifier(SimplifyToArrayInterface $simplifier) |
||
67 | 24 | ||
68 | /** |
||
69 | 24 | * Return the identifiers for all valid data types that have been registered. |
|
70 | * |
||
71 | * @param mixed $dataType \ReflectionObject or other description of the produced data type |
||
72 | * @return array |
||
73 | */ |
||
74 | public function validFormats($dataType) |
||
86 | 27 | ||
87 | 9 | public function isValidFormat(FormatterInterface $formatter, $dataType) |
|
108 | |||
109 | 24 | /** |
|
110 | 14 | * Format and write output |
|
111 | * |
||
112 | 14 | * @param OutputInterface $output Output stream to write to |
|
113 | * @param string $format Data format to output in |
||
114 | * @param mixed $structuredOutput Data to output |
||
115 | * @param FormatterOptions $options Formatting options |
||
116 | */ |
||
117 | public function write(OutputInterface $output, $format, $structuredOutput, FormatterOptions $options) |
||
125 | |||
126 | 27 | protected function validateAndRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
|
147 | 27 | ||
148 | /** |
||
149 | 27 | * Fetch the requested formatter. |
|
150 | 7 | * |
|
151 | * @param string $format Identifier for requested formatter |
||
152 | 20 | * @return FormatterInterface |
|
153 | */ |
||
154 | public function getFormatter($format) |
||
162 | |||
163 | /** |
||
164 | * Test to see if the stipulated format exists |
||
165 | */ |
||
166 | public function hasFormatter($format) |
||
170 | 27 | ||
171 | 5 | /** |
|
172 | * Render the data as necessary (e.g. to select or reorder fields). |
||
173 | 26 | * |
|
174 | * @param FormatterInterface $formatter |
||
175 | * @param mixed $originalData |
||
176 | * @param mixed $restructuredData |
||
177 | * @param FormatterOptions $options Formatting options |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function renderData(FormatterInterface $formatter, $originalData, $restructuredData, FormatterOptions $options) |
||
187 | |||
188 | /** |
||
189 | * Determine if the provided data is compatible with the formatter being used. |
||
190 | * |
||
191 | * @param FormatterInterface $formatter Formatter being used |
||
192 | * @param mixed $structuredOutput Data to validate |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public function validateData(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
218 | |||
219 | protected function simplifyToArray($structuredOutput, FormatterOptions $options) |
||
232 | |||
233 | /** |
||
234 | * Restructure the data as necessary (e.g. to select or reorder fields). |
||
235 | * |
||
236 | * @param mixed $structuredOutput |
||
237 | * @param FormatterOptions $options |
||
238 | * @return mixed |
||
239 | */ |
||
240 | public function restructureData($structuredOutput, FormatterOptions $options) |
||
247 | |||
248 | /** |
||
249 | * Allow the formatter access to the raw structured data prior |
||
250 | * to restructuring. For example, the 'list' formatter may wish |
||
251 | * to display the row keys when provided table output. If this |
||
252 | * function returns a result that does not evaluate to 'false', |
||
253 | * then that result will be used as-is, and restructuring and |
||
254 | * validation will not occur. |
||
255 | * |
||
256 | * @param mixed $structuredOutput |
||
257 | * @param FormatterOptions $options |
||
258 | * @return mixed |
||
259 | */ |
||
260 | public function overrideRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
266 | |||
267 | /** |
||
268 | * Allow the formatter to mess with the configuration options before any |
||
269 | * transformations et. al. get underway. |
||
270 | * @param FormatterInterface $formatter |
||
271 | * @param mixed $structuredOutput |
||
272 | * @param FormatterOptions $options |
||
273 | * @return FormatterOptions |
||
274 | */ |
||
275 | public function overrideOptions(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options) |
||
282 | } |
||
283 |