Transform a class into different types.
You can install the package via Composer:
composer require zerotoprod/transformable
- DataModel: Transform data into a class.
- DataModelHelper: Helpers for a
DataModel
. - DataModelFactory: A factory helper to set the value of your
DataModel
.
toArray(): array
Converts the object’s properties into an associative array.toJson(): string
Converts the object’s properties into a JSON string.
To use the Zerotoprod\Transformable\Transformable
trait in your class, simply include it:
use Zerotoprod\Transformable\Transformable;
class YourDataModel
{
use Transformable;
public $name;
public $email;
}
$model = new YourDataModel();
$model->name = 'John Doe';
$model->email = 'john.doe@example.com';
$array = $model->toArray();
$json = $model->toJson();