X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fconsolidation%2Foutput-formatters%2Fsrc%2FFormatterManager.php;fp=vendor%2Fconsolidation%2Foutput-formatters%2Fsrc%2FFormatterManager.php;h=da198c7ea304abff577be1fe9d69f443de0bd718;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=02de79fff7ae40d7af8c64fe11b05166432b6bca;hpb=74df008bdbb3a11eeea356744f39b802369bda3c;p=yaffs-website diff --git a/vendor/consolidation/output-formatters/src/FormatterManager.php b/vendor/consolidation/output-formatters/src/FormatterManager.php index 02de79fff..da198c7ea 100644 --- a/vendor/consolidation/output-formatters/src/FormatterManager.php +++ b/vendor/consolidation/output-formatters/src/FormatterManager.php @@ -19,6 +19,8 @@ use Consolidation\OutputFormatters\Validate\ValidationInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Consolidation\OutputFormatters\StructuredData\OriginalDataInterface; +use Consolidation\OutputFormatters\StructuredData\ListDataFromKeys; +use Consolidation\OutputFormatters\StructuredData\ConversionInterface; /** * Manage a collection of formatters; return one on request. @@ -37,6 +39,7 @@ class FormatterManager public function addDefaultFormatters() { $defaultFormatters = [ + 'null' => '\Consolidation\OutputFormatters\Formatters\NoOutputFormatter', 'string' => '\Consolidation\OutputFormatters\Formatters\StringFormatter', 'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter', 'xml' => '\Consolidation\OutputFormatters\Formatters\XmlFormatter', @@ -121,10 +124,17 @@ class FormatterManager $automaticOptions[FormatterOptions::FORMAT] = new InputOption(FormatterOptions::FORMAT, '', InputOption::VALUE_REQUIRED, $description, $defaultFormat); } + $dataTypeClass = ($dataType instanceof \ReflectionClass) ? $dataType : new \ReflectionClass($dataType); + if ($availableFields) { $defaultFields = $options->get(FormatterOptions::DEFAULT_FIELDS, [], ''); $description = 'Available fields: ' . implode(', ', $this->availableFieldsList($availableFields)); $automaticOptions[FormatterOptions::FIELDS] = new InputOption(FormatterOptions::FIELDS, '', InputOption::VALUE_REQUIRED, $description, $defaultFields); + } elseif ($dataTypeClass->implementsInterface('Consolidation\OutputFormatters\StructuredData\RestructureInterface')) { + $automaticOptions[FormatterOptions::FIELDS] = new InputOption(FormatterOptions::FIELDS, '', InputOption::VALUE_REQUIRED, 'Limit output to only the listed elements. Name top-level elements by key, e.g. "--fields=name,date", or use dot notation to select a nested element, e.g. "--fields=a.b.c as example".', []); + } + + if (isset($automaticOptions[FormatterOptions::FIELDS])) { $automaticOptions[FormatterOptions::FIELD] = new InputOption(FormatterOptions::FIELD, '', InputOption::VALUE_REQUIRED, "Select just one field, and force format to 'string'.", ''); } @@ -203,7 +213,17 @@ class FormatterManager */ public function write(OutputInterface $output, $format, $structuredOutput, FormatterOptions $options) { + // Convert the data to another format (e.g. converting from RowsOfFields to + // UnstructuredListData when the fields indicate an unstructured transformation + // is requested). + $structuredOutput = $this->convertData($structuredOutput, $options); + + // TODO: If the $format is the default format (not selected by the user), and + // if `convertData` switched us to unstructured data, then select a new default + // format (e.g. yaml) if the selected format cannot render the converted data. $formatter = $this->getFormatter((string)$format); + + // If the data format is not applicable for the selected formatter, throw an error. if (!is_string($structuredOutput) && !$this->isValidFormat($formatter, $structuredOutput)) { $validFormats = $this->validFormats($structuredOutput); throw new InvalidFormatException((string)$format, $structuredOutput, $validFormats); @@ -355,6 +375,17 @@ class FormatterManager return false; } + /** + * Convert from one format to another if necessary prior to restructuring. + */ + public function convertData($structuredOutput, FormatterOptions $options) + { + if ($structuredOutput instanceof ConversionInterface) { + return $structuredOutput->convert($options); + } + return $structuredOutput; + } + /** * Restructure the data as necessary (e.g. to select or reorder fields). *