PHPackages                             kafoso/type-formatter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. kafoso/type-formatter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

kafoso/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.

2.2.0(2y ago)05.6k↓68.8%MITPHPPHP &gt;=7.2 &lt;8.4

Since Mar 7Pushed 2y ago1 watchersCompare

[ Source](https://github.com/kafoso/type-formatter)[ Packagist](https://packagist.org/packages/kafoso/type-formatter)[ RSS](/packages/kafoso-type-formatter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (9)Used By (0)

PHP 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
============

[](#requirements)

```
"php": ">=7.2",
"ext-mbstring": ">=7.2",
"doctrine/collections": "^1"
```

For more information, see the [`composer.json`](composer.json) file.

License &amp; Disclaimer
========================

[](#license--disclaimer)

See [`LICENSE`](LICENSE) file. Basically: Use this library at your own risk.

Installation
============

[](#installation)

Via [Composer](https://getcomposer.org/) ():

```
composer install kafoso/type-formatter

```

Via GitHub:

```
git clone git@github.com:kafoso/type-formatter.git

```

Fundamentals
============

[](#fundamentals)

Type conversions to string
--------------------------

[](#type-conversions-to-string)

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

TypeConversion logicExample(s)NoteNullAs is.`null`BooleansAs is.`true`
`false`Float numbersAs is.`3.14`Standard float-to-string conversion rounding will occur, as produced by `strval(3.14)`.IntegersAs is.`42`StringsAs 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](#usage--type-specific-formatters--custom-string-formatter).ArraysAs 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](#usage--type-specific-formatters--custom-array-formatter).ObjectsClass 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 [further down](#usage--type-specific-formatters--custom-object-formatter).
This is especially useful for displaying relevant information in classes such as IDs in [Doctrine ORM entities](https://github.com/doctrine/orm).ResourceA 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](#usage--type-specific-formatters--custom-resource-formatter).Output examples
===============

[](#output-examples)

Echo
----

[](#echo)

```
