orka / json-decodable
There is no license information available for the latest version (v1.0.1) of this package.
PHP - lib : convert json to model
v1.0.1
2021-12-28 11:41 UTC
Requires
- php: >=8.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9
- symfony/var-dumper: 5.3
README
Library to convert JSON to Model.
Implement your model.
<?php
namespace Model;
use Traits\JsonDecodable; # +Add
class Color
{
use JsonDecodable; # +Add
private string $name;
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
# optionnal: You can define the required keys
public function getKeyMandatory(): array
{
return [
"name"
];
}
# optionnal: You can map api keys
public function getKeyMapping(): array
{
return [
"colorName" => "name", # colorName is the key receipted from API
];
}
}
Convert: Api => Model.
$color = new Color(); # create a new instance of your model.
# you can assign from StdClass
$color->setFromObject($stdClass, Color::class);
# or an array
$color->setFromArray($array, Color::class);