Download the PHP package inspirum/arrayable without Composer
On this page you can find all versions of the php package inspirum/arrayable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download inspirum/arrayable
More information about inspirum/arrayable
Files in inspirum/arrayable
Package arrayable
Short Description PHP arrayable interface for handling casting to an array
License MIT
Homepage https://github.com/inspirum/arrayable-php
Informations about the package arrayable
Arrayable
Motivation
Unfortunately PHP does not have a nice way how to typecast objects to array
.
There is the __toString()
magic method for \Stringable
interface (since PHP 8.0) and the jsonSerialize()
method for \JsonSerializable
interface (since PHP 5.4), but __toArray()
method is not (and will not) be supported – there are just several rejected draft RFC (object_cast_to_types, to array, ...) that suggests some kind of object to scalar type casting.
But so far (at least) there is no way to implement some (not even magic) method to be called when cast to array
.
Ideally, something like this would work:
but actually it cast to array like this:
Usage example
All the code snippets shown here are modified for clarity, so they may not be executable.
This package implements simple \Arrayable
(or \Inspirum\Arrayable\Arrayable
) interface.
There is \is_arrayable()
function (or \Inspirum\Arrayable\Convertor::isArrayable()
method) to check if given data are able to type cast itself to array
.
Then there is \to_array()
function (or \Inspirum\Arrayable\Convertor::toArray()
method) to recursively cast data to array
.
There is also helper abstract classes for common use for DAO (BaseCollection
) objects.
System requirements
Installation
Run composer require command
Testing
To run unit tests, run:
To show coverage, run:
Contributing
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of arrayable with dependencies
ext-json Version *