Download the PHP package consolidation/output-formatters without Composer

On this page you can find all versions of the php package consolidation/output-formatters. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package output-formatters

Consolidation\OutputFormatters

Apply transformations to structured data to write output in different formats.

ci scrutinizer codecov license

Motivation

Formatters are used to allow simple commandline tool commands to be implemented in a manner that is completely independent from the Symfony Console output interfaces. A command receives its input via its method parameters, and returns its result as structured data (e.g. a php standard object or array). The structured data is then formatted by a formatter, and the result is printed.

This process is managed by the Consolidation/AnnotatedCommand project.

Library Usage

This is a library intended to be used in some other project. Require from your composer.json file:

Example Formatter

Simple formatters are very easy to write.

The formatter is passed the set of $options that the user provided on the command line. These may optionally be examined to alter the behavior of the formatter, if needed.

Formatters may also implement different interfaces to alter the behavior of the rendering engine.

Configuring Formats for a Command

Commands declare what type of data they return using a @return annotation, as usual:

The output-formatters library determines which output formats are applicable to the command by querying all available formats, and selecting any that are able to process the data type that is returned. Thus, if a new format is added to a program, it will automatically be available via any command that it works with. It is not necessary to hand-select the available formats on every command individually.

Structured Data

Most formatters will operate on any array or ArrayObject data. Some formatters require that specific data types be used. The following data types, all of which are subclasses of ArrayObject, are available for use:

Commands that need to produce XML output should return a DOMDocument as its return type. The formatter manager will do its best to convert from an array to a DOMDocument, or from a DOMDocument to an array, as needed. It is important to note that a DOMDocument does not have a 1-to-1 mapping with a PHP array. DOM elements contain both attributes and elements; a simple string property 'foo' may be represented either as or value. Also, there may be multiple XML elements with the same name, whereas php associative arrays must always have unique keys. When converting from an array to a DOM document, the XML formatter will default to representing the string properties of an array as attributes of the element. Sets of elements with the same name may be used only if they are wrapped in a containing parent element--e.g. onetwo. The XMLSchema class may be used to provide control over whether a property is rendered as an attribute or an element; however, in instances where the schema of the XML output is important, it is best for a function to return its result as a DOMDocument rather than an array.

A function may also define its own structured data type to return, usually by extending one of the types mentioned above. If a custom structured data class implements an appropriate interface, then it can provide its own conversion function to one of the other data types:

Additionally, structured data may be simplified to arrays via an array simplification object. To provide an array simplifier, implement SimplifyToArrayInterface, and register the simplifier via FormatterManager::addSimplifier().

Fields

Some commands produce output that contain fields. A field may be either the key in a key/value pair, or it may be the label used in tabular output and so on.

Declaring Default Fields

If a command declares a very large number of fields, it is possible to display only a subset of the available options by way of the @default-fields annotation. The following example comes from Drush:

All of the available fields will be listed in the help output for the command, and may be selected by listing the desired fields explicitly via the --fields option.

To include all avalable fields, use --fields=*.

Note that using the @default-fields annotation will reduce the number of fields included in the output for all formats, including unstructured formats such as json and yaml. To specify a reduced set of fields to display only when using a human-readable output format (e.g. table), use the @default-table-fields annotation instead.

Reordering Fields

Commands that return table structured data with fields can be filtered and/or re-ordered by using the --fields option. These structured data types can also be formatted into a more generic type such as yaml or json, even after being filtered. This capabilities are not available if the data is returned in a bare php array. One of RowsOfFields, PropertyList or UnstructuredListData (or similar) must be used.

When the --fields option is provided, the user may stipulate the exact fields to list on each row, and what order they should appear in. For example, if a command usually produces output using the RowsOfFields data type, as shown below:

Then the third and first fields may be selected as follows:

To select a single column and strip away all formatting, use the --field option:

Commands that produce deeply-nested data structures using the UnstructuredData and UnstructuredListData data type may also be manipulated using the --fields and --field options. It is possible to address items deep in the heirarchy using dot notation.

The UnstructuredData type represents a single nested array with no requirements for uniform structure. The UnstructuredListData type is similar; it represents a list of UnstructuredData types. It is not required for the different elements in the list to have all of the same fields or structure, although it is expected that there will be a certain degree of similarity.

In the example below, a command returns a list of stores of different kinds. Each store has common top-level elements such as name, products and sale-items. Each store might have different sorts of products with different attributes:

Just as is the case with tabular output, it is possible to select only a certain set of fields to display with each output item:

With unstructured data, it is also possible to remap the name of the field to something else:

The field name . is special, though: it indicates that the named element should be omitted, and its value or children should be applied directly to the result row:

Finally, it is also possible to reach down into nested data structures and pull out information about an element or elements identified using "dot" notation:

Commands that use RowsOfFields or PropertyList return type will be automatically converted to UnstructuredListData or UnstructuredData, respectively, whenever any field remapping is done. This will only work for data types such as yaml or json that can render unstructured data types. It is not possible to render unstructured data in a table, even if the resulting data happens to be uniform.

Filtering Specific Rows

A command may allow the user to filter specific rows of data using simple boolean logic and/or regular expressions. For details, see the external library consolidation/filter-via-dot-access-data that provides this capability.

Rendering Table Cells

By default, both the RowsOfFields and PropertyList data types presume that the contents of each cell is a simple string. To render more complicated cell contents, create a custom structured data class by extending either RowsOfFields or PropertyList, as desired, and implement RenderCellInterface. The renderCell() method of your class will then be called for each cell, and you may act on it as appropriate.

Note that if your data structure is printed with a formatter other than one such as the table formatter, it will still be reordered per the selected fields, but cell rendering will not be done.

The RowsOfFields and PropertyList data types also allow objects that implement RenderCellInterface, as well as anonymous functions to be added directly to the data structure object itself. If this is done, then the renderer will be called for each cell in the table. An example of an attached renderer implemented as an anonymous function is shown below.

This project also provides a built-in cell renderer, NumericCellRenderer, that adds commas at the thousands place and right-justifies columns identified as numeric. An example of a numeric renderer attached to two columns of a data set is shown below.

API Usage

It is recommended to use Consolidation/AnnotatedCommand to manage commands and formatters. See the AnnotatedCommand API Usage for details.

The FormatterManager may also be used directly, if desired:

The FormatterOptions class is used to hold the configuration for the command output--things such as the default field list for tabular output, and so on--and also the current user-selected options to use during rendering, which may be provided using a Symfony InputInterface object:

Comparison to Existing Solutions

Formatters have been in use in Drush since version 5. Drush allows formatters to be defined using simple classes, some of which may be configured using metadata. Furthermore, nested formatters are also allowed; for example, a list formatter may be given another formatter to use to format each of its rows. Nested formatters also require nested metadata, causing the code that constructed formatters to become very complicated and unweildy.

Consolidation/OutputFormatters maintains the simplicity of use provided by Drush formatters, but abandons nested metadata configuration in favor of using code in the formatter to configure itself, in order to keep the code simpler.


All versions of output-formatters with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.3
dflydev/dot-access-data Version ^1.1.0 || ^2 || ^3
symfony/console Version ^4 || ^5 || ^6 || ^7
symfony/finder Version ^4 || ^5 || ^6 || ^7
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package consolidation/output-formatters contains the following files

Loading the files please wait ....