PHP JSON property helper trait

v0.1.0 2020-06-07 18:44 UTC

This package is auto-updated.

Last update: 2022-05-18 23:25:23 UTC


CircleCI Maintainability Test Coverage Latest Stable Version Latest Unstable Version


This is a helper to add attributes to an array. This will help,
if you need to represent JSON entities / classes and resolve recursively when JSON encoding.


composer require probiers-us/php-json-property


You can use JsonPropertyInterface and JsonPropertyTrait in the classes
you want to add this. Calling json_encode will recursively resolve all embedded
classes in the JSON properties array, leveraging JsonSerializable.

Add a property

$jsonPropertyClass->addJsonProperty('key', 'value');

Add multiple properties

$jsonPropertyClass->addJsonProperties(['key', 'value']);

Remove property


Set properties array of class

$jsonPropertyClass->setJsonProperties(['key', 'value']);

Get properties

Get the array without resolving embedded properties


Get the array of properties and resolve embedded


Add typed properties

To add typed JSON properties, add a wrapper around e.g. addJsonProperty

public function addName(string $name) {
    $this->addJsonProperty('name', $name);