Download the PHP package darsyn/unboxer without Composer
On this page you can find all versions of the php package darsyn/unboxer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package unboxer
Simple utility to unbox complex data structures (objects) to native data types, suitable for encoding (for example, JSON).
Documentation
Code of Conduct
This project includes and adheres to the Contributor Covenant as a Code of Conduct.
Supported Types
This library returns all scalar and null values as-is, plus recursively
processing all array (and stdClass
) types.
When this library encounters an object that is an instance of a known type, it will attempt to convert it by using the return value of a specific method. Object types supported by this library out-of-the-box include:
- Dates (objects implementing
DateTimeInterface
) which are converted to strings according to RFC3339 (eg,2019-02-05T12:15:32+00:00
). - Timezones (objects implementing
DateTimeZone
) which result in a string containing the timezone name (eg,America/Vancouver
). - Exceptions and errors (objects implementing
Throwable
) which result in a string containing the exception message. - JSON (objects implementing
JsonSerializable
) which result in the library recursively iterating over the JSON data returned. - Doctrine collections (objects implementing
Collection
interface) which result in the library iterating over each of the items inside the collection.
Additionally, any user-land object can implement UnboxableInterface
. Similar
to JsonSerializable::jsonSerialize()
method, the __unbox
method can return
anything as a representation of its internal state.
It is recommended to return unboxable objects as-is, as everything returned from
UnboxableInterface::__unbox
is recursively iterated over anyway.
Brief Example
var_dump
ing the variable $output
results in:
Note that returning multiple nested unboxable objects will result in the output collapsing down into a single value:
Extending
Additional known object types can be added by extending Unboxer
and overriding
the getKnownDataTypes
method. For each known object type, either a closure or
an array specifying which method to call on the object may can specified:
The unboxer will, by default, convert any objects with the __toString()
magic
method to a string. To turn this functionality off, extend Unboxer
and
override the class constant STRINGIFY_OBJECTS
.
License
Please see the separate license file included in this repository for a full copy of the MIT license, which this project is licensed under.
Authors
If you make a contribution (submit a pull request), don't forget to add your name here!