PHPackages                             eboreum/caster - 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. eboreum/caster

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

eboreum/caster
==============

Cast any PHP value to a sensible, human readable string. Great for type-safe outputs, exception messages, transparency during debugging, and similar things. Also helps avoiding innate problems such as printing endless, circular referencing objects (endless recursion), limits the output for large arrays and long strings, and prevents (opt-in) the outputting of sensitive strings like passwords.

2.1.2(1y ago)07.1k↓32.6%[1 issues](https://github.com/eboreum/caster/issues)4MITPHPPHP ^8.3CI passing

Since Aug 24Pushed 1y ago2 watchersCompare

[ Source](https://github.com/eboreum/caster)[ Packagist](https://packagist.org/packages/eboreum/caster)[ RSS](/packages/eboreum-caster/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (15)Used By (4)

Eboreum/Caster: A PHP type formatter
====================================

[](#eboreumcaster-a-php-type-formatter)

[![license](https://camo.githubusercontent.com/fbbb8f0e35ab07b837b20abaaef855d03d39cf0b9beca1099307a4754d2236af/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f65626f7265756d2f6361737465723f6c6162656c3d6c6963656e7365)](https://camo.githubusercontent.com/fbbb8f0e35ab07b837b20abaaef855d03d39cf0b9beca1099307a4754d2236af/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f65626f7265756d2f6361737465723f6c6162656c3d6c6963656e7365)[![build](https://github.com/eboreum/caster/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/eboreum/caster/actions/workflows/build.yml/badge.svg?branch=main)[![Code Coverage](https://camo.githubusercontent.com/6a4fcf79810573fa541327ae6684bca980fd01051950ef459b590f8b9132dfb0/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f75726c3d68747470733a2f2f676973742e67697468756275736572636f6e74656e742e636f6d2f6b61666f736f2f33353962346135623438643230373636646164343538333530666431313236392f7261772f746573742d636f7665726167655f5f6d61696e2e6a736f6e)](https://github.com/eboreum/caster/actions)[![PHPStan Level](https://camo.githubusercontent.com/04b0136ed31f003559c029036169d30fb51fa2dcc805c3be4f2d58eb7ce6141d/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f75726c3d68747470733a2f2f676973742e67697468756275736572636f6e74656e742e636f6d2f6b61666f736f2f33353962346135623438643230373636646164343538333530666431313236392f7261772f7068707374616e2d6c6576656c5f5f6d61696e2e6a736f6e)](https://github.com/eboreum/caster/actions)

Cast any PHP value to a sensible, human readable string. Great for type-safe outputs, exception messages, transparency during debugging, and similar things. Also helps avoiding innate problems such as printing endless, circular referencing objects (endless recursion), limits the output for large arrays and long strings, and prevents (opt-in) the outputting of sensitive strings like passwords.

**Why use Eboreum/Caster instead things like [XDebug](https://xdebug.org/), [`symfony/var-dumper`](https://packagist.org/packages/symfony/var-dumper), and similar libraries?**

[XDebug](https://xdebug.org/), [`symfony/var-dumper`](https://packagist.org/packages/symfony/var-dumper), and similar libraries are meant for **development environments**.

**Eboreum/Caster** is meant for any and **all environments** (development, test, staging, production).

With Eboreum/Caster, you will be able to provide excellent information about all PHP values, which is great in both **debugging** and **failure scenarios**. Are the exception messages in your application lacklustre? Expand your options and displayed values greatly with Eboreum/Caster!

This package can be thought of as an extended version of the magic method `__debugInfo` (). However, contrary to `__debugInfo`, where only the internals of the implementing class is used for building sensible debug information – with the occasional (often abominable) static method calls to other classes – Caster allows for much more variety, including custom formatters utilizing proper dependency injection.

Eboreum/Caster **gives you -- the developer -- the ultimate power** to control how output is handled, parsed and presented through opt-in utilization of custom formatters.

Lastly, you may provide a series of **sensitive text strings** like passwords, authentication tokens, social security numbers, and similar, preventing these from being output inside strings. Wouldn't want these to show up in error logs, emails, and what have you. Upon encountering sensitive strings, said sensitive substrings will be masked, instead showing a static length string replacement (like `******`).

Requirements
============

[](#requirements)

```
"php": "^8.3",
"ext-mbstring": "*",
"ext-openssl": "*"
```

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

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

[](#installation)

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

```
composer install eboreum/caster

```

Via GitHub:

```
git clone git@github.com:eboreum/caster.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 `\Eboreum\Caster\Contract\Formatter\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 `\Eboreum\Caster\Contract\Formatter\ArrayFormatterInterface`. More on this interface and implementation [further down](#usage--type-specific-formatters--custom-array-formatter).ObjectsClass namespace with leading backslash.`\stdClass`

`class@anonymous/in/foo/bar/baz.php:22`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 `\Eboreum\Caster\Contract\Formatter\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).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 `\Eboreum\Caster\Contract\Formatter\ResourceFormatterInterface`. More on this interface and implementation [further down](#usage--type-specific-formatters--custom-resource-formatter).Output examples
===============

[](#output-examples)

Echo
----

[](#echo)

**Example:**

```
