Type-safe data mapping between JSON and a PHP class object.
- Transforms JSON to PHP class object.
- Transforms PHP object to JSON.
Marshaller analyze all private properties and convert them into JSON. Unmarshaller analyze the signature of the constructor of the given class and convert JSON into an object.
$ composer require suin/marshaller
This is the simplest use case of Marshaller.
use Suin\Marshaller\JsonMarshaller;
use Suin\Marshaller\StandardProtocol;
// Transform an object to JSON.
$marshaller = new JsonMarshaller(new StandardProtocol);
$json = $marshaller->marshal(new Cat('Oliver'));
var_dump($json);
// Output:
// string(17) "{"name":"Oliver"}"
// Transform JSON to an object.
$cat = $marshaller->unmarshal($json, Cat::class);
var_dump($cat);
// Output:
// object(Cat)#%d (1) {
// ["name":"Cat":private]=>
// string(6) "Oliver"
// }
Please see example#01 for details.
Sometimes you would want to decode a JSON value in special transform way. In such a case, you can also define transforming rules between a PHP object and a JSON object by using Protocol
s and Format
s.
A Format
describes how a single class or type becomes JSON and vice versa. All Format
s must follow the interface below:
interface Format<A, B> {
public function read(A $jsonValue): B
public function write(B $object): A
}
For example, a Format
is implemented like this:
class HealthFormat // naming rule: class name + "Format"
{
// Define how transform a JSON value to a PHP object.
public function read(string $health): Health
{
return new Health($health === 'healthy');
}
// Define how transform a PHP object to a JSON value.
public function write(Health $health): string
{
return $health->isHealthy() ? 'healthy' : 'sick';
}
}
Also, a protocol is a class that have a collection of Format
s. The following example has only one format, but some more formats would be included here in real use case.
class HealthProtocol extends StandardProtocol
{
public function __construct()
{
parent::__construct(
new HealthFormat
);
}
}
To see complete example code of protocols and formats, please see example#02. Also a complex example is seen in ./tests/ExampleModel/StudentProtocol.php
Please see CHANGELOG for more details.
Please see CONTRIBUTING for more details.