icecave/repr

A library for generating string representations of any value, inspired by Python's reprlib library.

4.0.0 2020-08-25 02:05 UTC

This package is auto-updated.

Last update: 2024-09-25 10:16:24 UTC


README

Build Status Code Coverage Latest Version

Repr provides a way to generate informational string representations of any value, inspired by Python's reprlib library.

composer require icecave/repr

Example

Use the Repr::repr() method to obtain a string representation for any type.

use Icecave\Repr\Repr;

echo Repr::repr([1, 2, 3]);

The output from the example above is:

[1, 2, 3]

Arrays

Arrays are represented using PHP 5.4 style short array notation. By default a maximum of 3 elements are shown along with a count of any additional elements. Nested arrays are represented up to 3 levels deep by default, with any arrays nested deeper than this showing only the element count.

Numeric Values

Numbers are represented naturally, floating point values will always display a decimal point even if representing a whole number.

Strings

Strings are represented enclosed in double quotes up to a default maximum length of 50 characters. Any control characters are shown as escape sequences.

Objects

Objects are represented as a class name and SPL object hash enclosed in angle brackets. If the object has a __toString method, the result of this is shown after the class name according to the rules of string representations specified above.

If an object implements RepresentableInterface, the result of its stringRepresentation() method is used instead.

Resources

Resources are represented as a resource type and ID enclosed in angle brackets. Stream resources will also display the stream mode.

Other Types

All other types are represented by the result of var_export() in lowercase.