Download the PHP package fleshgrinder/format without Composer

On this page you can find all versions of the php package fleshgrinder/format. 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 format

Latest Stable Version License Travis CI build status AppVeyor CI build status

Dependency Status Coveralls branch Scrutinizer Code Climate: GPA Total Downloads

Format

The format library provides the functionality to format special string patterns. The implementation is similar to sprintf and msgfmt_format_message but with various unique and very useful features.

Installation

Open a terminal, enter your project directory and execute the following command to add this library to your dependencies:

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Usage

This library features a single static method to format string patterns. Some examples of the format method are:

Placeholders

Each placeholder can specify which argument value it references, and if omitted it is assumed to be “the next argument”. For example, the pattern {} {} {} would take three arguments, and they would be formatted in the same order as they are given. The pattern {2} {1} {0}, however, would format arguments in reverse order.

Things can get a little tricky once you start intermingling the two types of positional placeholders. The “next argument” specifier can be thought of as an iterator over the arguments. Each time a “next argument” specifier is seen, the iterator advances. This leads to behavior like this:

The internal iterator over the arguments has not been advanced by the time the first {} is seen, so it prints the first argument. Then upon reaching the second {}, the iterator has advanced forward to the second argument. Essentially, placeholders which explicitly specify their argument do not affect placeholders which do not specify an argument in terms of positional placeholders.

Placeholders are not limited to numbers, it is also possible to access them by names. Names are limited to A-Za-z0-9_- characters, this ensures highest compatibility and should match 99 % of all associative array keys in the PHP world:

A pattern is required to use all placeholders, but not all arguments. A MissingPlaceholderException is thrown if a placeholder is missing from the arguments:

You may refer to the same argument more than once in the pattern, including different modifiers (which are explained in the following sections).

Type Modifier

The type modifier can be used to print the type of an argument value instead of attempting to format the value itself:

Combination with positional and named placeholders is of course possible:

The fleshgrinder/value library is used to get the type of the value, this means in effect that type names are consistent in naming and case. However, the output for resources is slightly extended by including the type of the resource:

IMPORTANT

The type modifier cannot be combined with any other modifier and always overwrites anything else!

String Formatting and Modifiers

The method is binary safe and strings are printed as is.

Since version 1.1.0 empty strings and objects that can be converted to strings are formatted as empty {:?} instead of resulting in no output at all:

Also available since 1.1.0 are the caret notation modifier c and the printable Unicode replacement modifier p for ASCII control characters:

This is useful while constructing error messages where a string that violated some constraint should be included in the error message, but might lead to undesired side-effects due to the nature of control characters. Both string modifiers help to mitigate this problem. Note that recreation of the original string might be impossible with the caret notation, the printable Unicode replacements should be used in such cases. However, those characters are UTF-8 characters and not ASCII characters, which might not be acceptable.

Number Formatting and Modifiers

Numbers are formatted with number_format and its default values for decimal and thousand separators, this ensures best readability and consistent formatting. The amount of decimals defaults to zero, but may be configured by inserting a natural number separated by a dot (.) within the braces after the placeholder:

The output format of numbers can be changed with format modifiers. A format modifier is added by inserting the desired modifier after a hash symbol (#) within the braces after the placeholder. Available format modifiers are:

Note that there are no #E or #X modifiers, this is on purpose to force consistent output.

Iterable Listings and Modifiers

Iterable data structures are formatted as comma-separated lists. An optional conjunction may be configured by adding the desired word after a colon (:) inside the braces after the placeholder:

This is perfect for the construction of error messages:

Other modifiers, except for type, are of course combinable with iterable listings:

Note well that expansion of iterable data structures is recursive, this might lead to unexpected output, but it might also result in infinite loops in case the data structure contains circular references. Ensure that your iterable argument values are sane, you have been warned:

Optional Parts

Another unique feature are optional parts, which are included only if a marked argument is present and contains a non-empty value (according to PHP’s empty rules). Optional parts are specified by enclosing some text in brackets:

A placeholder is marked by appending a question mark (?) after the closing brace of the placeholder. All marked placeholders within an optional part must be non-empty in order to be included. Note that no exception is thrown if a marked placeholder within an optional part is missing from the arguments, as this counts as being empty. Note further that nesting of optional parts is not possible, however, if you think that this would be greater feature feel free to open an issue.

IMPORTANT

Only fixed positional and named placeholders can be marked, the internal argument iterator is not taken into account. This means in effect that a pattern like the following never includes the optional part because there is no fixed positional or named placeholder present:

The reason for this limitation is simple: optional parts are evaluated separately before the full pattern is being evaluated. Hence, any internal iterator would start counting inside the optional part, which would lead to hard to understand and weird behavior; if supported.

Escaping

The special characters [, ], {, and } can be escaped by doubling them, [[, ]], {{, or }} respectively.

Errors and Exceptions

Any error or exception that might be thrown by an argument that is being formatted is not caught and bubbles up. The method itself throws the already explained MissingPlaceholderException. It might throw an InvalidArgumentException as well, if the type modifier is not present and an argument is:

Testing

Open a terminal, enter the project directory and execute the following commands to run the PHPUnit tests with your locally installed PHP executable. This requires that you have at least make 4.0 installed:

You can also execute the following two commands, in case make is not available on your system, or too old:


All versions of format with dependencies

PHP Build Version
Package Version
Requires php Version ^7.0
ext-pcre Version *
fleshgrinder/core Version ^1.0
fleshgrinder/value Version ^1.0
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 fleshgrinder/format contains the following files

Loading the files please wait ....