PHPackages                             darsyn/unboxer - 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. darsyn/unboxer

ActiveLibrary

darsyn/unboxer
==============

Simple utility to unbox complex data structures (objects) to native data types, suitable for encoding into formats such as JSON, YAML, etc.

0.3.1(5y ago)015MITPHPPHP &gt;=7.1

Since Aug 23Pushed 5y agoCompare

[ Source](https://github.com/darsyn/unboxer)[ Packagist](https://packagist.org/packages/darsyn/unboxer)[ Docs](https://github.com/darsyn/unboxer)[ RSS](/packages/darsyn-unboxer/feed)WikiDiscussions develop Synced today

READMEChangelogDependencies (1)Versions (7)Used By (0)

Simple utility to unbox complex data structures (objects) to native data types, suitable for encoding (for example, JSON).

Documentation
=============

[](#documentation)

Code of Conduct
---------------

[](#code-of-conduct)

This project includes and adheres to the [Contributor Covenant as a Code of Conduct](CODE_OF_CONDUCT.md).

Supported Types
---------------

[](#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
-------------

[](#brief-example)

```
