Download the PHP package kafoso/type-formatter without Composer

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

PHP Type Formatter

Minimalistic, lightweight library for converting PHP data types to readable strings. Great for type-safe outputs, exception messages, transparency during debugging, and similar things. Also helps avoiding innate problems such as printing recursive objects and large arrays.

Requirements

For more information, see the composer.json file.

License & Disclaimer

See LICENSE file. Basically: Use this library at your own risk.

Installation

Via Composer (https://packagist.org/packages/kafoso/type-formatter):

composer install kafoso/type-formatter

Via GitHub:

git clone [email protected]:kafoso/type-formatter.git

Fundamentals

Type conversions to string

The data types are converted as illustrated in the table below.

Type Conversion logic Example(s) Note
Null As is. null
Booleans As is. true
false
Float numbers As is. 3.14 Standard float-to-string conversion rounding will occur, as produced by strval(3.14).
Integers As is. 42
Strings As is or as a sample (substring). "foo"
"bar ..." (sample)
If you wish to control how strings are presented or apply conditions, you may do so by providing an instance of \Kafoso\TypeFormatter\Type\StringFormatterInterface. More on this interface and implementation further down.
Arrays As is or as a sample. [0 => "foo"]

[0 => "bar" ... and 9 more elements]
Sub-arrays
By default, no sub-arrays are displayed; i.e. the depth is zero. However, a custom depth may be specified.
Sub-arrays with depth 0 (zero) may appear as such: [0 => (array(1)) [...]]
Sub-arrays with depth 1 may appear as such: [0 => (array(1)) ["foo"]]

Sampling and sample size
By default, a maximum of 3 elements are displayed, before the " ... and X more elements" message is displayed. This number is also customizible.

Custom array-to-string conversion
If you wish to customize how arrays are being converted to a string, you may do so by providing an instance of \Kafoso\TypeFormatter\Type\ArrayFormatterInterface. More on this interface and implementation further down.
Objects Class namespace with leading backslash. \stdClass

\class@anonymous/foo/bar/baz.php0x11038bd57
Objects are rather complex types. As such, something sensible besides its class name cannot be reliably displayed. Not even using __toString or similar methods.

Custom object-to-string conversion
If you wish to customize how objects are being converted to a string, you may do so by providing an instance of \Kafoso\TypeFormatter\Type\ObjectFormatterInterface. More on this interface and implementation Doctrine ORM entities.
Resource A text and the resource's ID. #Resource id #2 Resources can be many different things. A file pointer, database connection, image canvas, etc. As such, only the bare minimum of information is displayed.

Custom resource-to-string conversion
If you wish to customize how resources are being converted to a string, you may do so by providing an instance of \Kafoso\TypeFormatter\Type\ResourceFormatterInterface. More on this interface and implementation further down.

Output examples

Echo

Exception

Usage

\Kafoso\TypeFormatter\TypeFormatter is immutable. As such, it may only be configured upon construction.

The standard formatter

By default, Kafoso\TypeFormatter\TypeFormatter::create() returns a new instance every time. If you wish to re-use the same instance over and over, you have two options.

Option 1: Store it in a variable and use that. As such:

Option 2: Use a dependency container; see below.

Dependency container (default & variations)

For ease-of-use, you may store formatters statically in Kafoso\TypeFormatter\TypeFormatter.

You may specify 2 types of dependencies.

Default:

Variations:

Use a real Dependency Injection Container

Alternatively, use an actual Dependency Injection Container (DIC) such as Pimple. However, this means you will have to pass around the dependencies everywhere you need them, which - from a SOLID perspective - is nice, but not always very practical.

A custom basic formatter

You may customize the formatter to your specific needs, e.g. changing string sample size, array depth, or providing custom array and/or object formatters. Afterwards, you may store it as the default or a variation for later re-use.

Type specific formatters

The following type specific formatters exists, which may help providing additional information. Especially useful for printing relevant information relating to an object.

These formatters are injected into the desired instance of \Kafoso\TypeFormatter\TypeFormatter using the with* methods. Do however notice, that \Kafoso\TypeFormatter\TypeFormatter is immutable.

Data type \Kafoso\TypeFormatter\TypeFormatter method Interface Note
array withCustomArrayFormatterCollection \Kafoso\TypeFormatter\Type\ArrayFormatterInterface See usage example in Custom array formatter further down.
object withCustomObjectFormatterCollection \Kafoso\TypeFormatter\Type\ObjectFormatterInterface See usage example in below.
resource withCustomResourceFormatterCollection \Kafoso\TypeFormatter\Type\ResourceFormatterInterface See usage example in Custom resource formatter further down.
string withCustomStringFormatterCollection \Kafoso\TypeFormatter\Type\StringFormatterInterface See usage example in Custom string formatter further down.

Multiple custom formatters can be provided, such that they each handle only specific cases. Order is significant.

Ultimately, all custom formatters fall back to their respective standard formatters.

Included object formatters

The following object formatters are readily available. You may use them as-is or extend them, providing your own custom logic. Everything is very Open-closed Principle.

Namespace: \Kafoso\TypeFormatter\Type\Objects

Class name Description Output example(s)
DateTimeInterfaceFormatter Formats \DateTimeInterface objects, appending ISO 8601 time in parenthesis. \DateTimeImmutable ("2019-01-01T00:00:00+00:00")
DirectoryFormatter Formats \Directory objects, as produced by dir(__DIR__). \Directory ($path = "/foo.php")
DoctrineEntityFormatter Formats Doctrine ORM entities using the provided \Doctrine\ORM\EntityManager. \User {$id = 1}

\Message {$uuid = "ad39f689-1070-41cd-9e0f-17112abdfc85"}
PublicVariableFormatter Formats any object which has publicly accessible variables. \stdClass {$foo = "bar"}
TextuallyIdentifiableInterfaceFormatter Formats objects, which implement the interface \Kafoso\TypeFormatter\Contract\TextuallyIdentifiableInterface. \MyUserClass (USER.ID = 22)
ThrowableFormatter Formats instances of \Throwable.
Notice: The output is greatly simplified compared to properly dumping a \Throwable with stack trace and everything else.
\RuntimeException {$code = 0, $file = "/foo.php", $line = 22, $message = "bar", $previous = null}

Custom array formatter

Custom object formatter

In this example, \DateTimeInterface, \Throwable, and the Doctrine ORM EntityManager is utilized to supply good real-world use cases.

Custom resource formatter

Custom string formatter

Tests

Unit tests (requirements.

Running tests

For all tests, first follow these steps:

Unit tests will run on most systems.

Credits

Authors


All versions of type-formatter with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2 <8.4
ext-mbstring Version >=7.2 <8.4
doctrine/collections Version ^1
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 kafoso/type-formatter contains the following files

Loading the files please wait ....