Details
JSON to Entity
![coverage report](https://gitlab.com/niko9911/php-json-to-entity/badges/master/coverage.svg)
Map json into entity. This allows you to easily validate payload json
and map it automatically into entity class. This class can be
for example ORM class, when you can directly save it into the DB.
This is pretty nice, if you're lazy and need just to develop fast,
but if you need high performance application, please map json and
validate json manually. Comes with performance strike, but saves time.
Install
Via composer:
composer require niko9911/json-to-entity
Usage
<?php
declare(strict_types=1);
// Declare entity where to map.
final class Basic
{
/
* @var string
*/
private $bar;
/
* @var int|null
*/
private $foo;
/
* @var array
*/
private $fooBar;
/
* BasicUnitTestEntity constructor.
*
* @param string $bar
* @param int $foo
* @param array $fooBar
*/
public function __construct(string $bar, ?int $foo, array $fooBar)
{
$this->bar = $bar;
$this->foo = $foo;
$this->fooBar = $fooBar;
}
/
* @return string
*/
public function getBar(): string
{
return $this->bar;
}
/
* @return int|null
*/
public function getFoo(): ?int
{
return $this->foo;
}
/
* @return array
*/
public function getFooBar(): array
{
return $this->fooBar;
}
}
// JSON
$json = <<<JSON
{
"bar": "Some_Bar",
"foo": 10,
"fooBar": ["a", "b", "c"]
}
JSON;
$mapper = new \Niko9911\JsonToEntity\Mapper();
$entity = $mapper->map(\json_decode($json), Basic::class);
var_dump($entity);
//class Basic#25 (3) {
// private $bar =>
// string(8) "Some_Bar"
// private $foo =>
// int(10)
// private $fooBar =>
// array(3) {
// [0] =>
// string(1) "a"
// [1] =>
// string(1) "b"
// [2] =>
// string(1) "c"
// }
//}
License
Licensed under the MIT license.
|
Name: |
PHP JSON to Object Mapper |
Base name: |
php-json-to-entity |
Description: |
Map JSON decoded data into entity objects |
Version: |
- |
PHP version: |
7 |
License: |
Custom (specified in a license file) |
|
|
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.
|
Files |
|
File |
Role |
Description |
Domain (2 files, 2 directories) |
Mapper.php |
Class |
Class source |
File |
Role |
Description |
Unit (2 directories) |