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.
Via composer:
composer require niko9911/json-to-entity
<?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"
// }
//}
Licensed under the MIT license.
Classes of Niko | > | PHP JSON to Object Mapper | > | Download .zip .tar.gz | > | Support forum | > | Blog | > | Latest changes |
|
|
Groups | Applications | Files |
Groups |
Language | Constructs to assist in the language control | View top rated classes |
PHP 7 | Classes using PHP 7 specific features | View top rated classes |
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Files |
File | Role | Description | ||
---|---|---|---|---|
src (1 file, 1 directory) | ||||
tests (1 file, 2 directories) | ||||
.php_cs.dist | Example | Example script | ||
composer.json | Data | Auxiliary data | ||
composer.lock | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml.dist | Data | Auxiliary data | ||
README.md | Doc. | Read me |
Files | / | src | / | Domain |
File | Role | Description | ||
---|---|---|---|---|
Exception (5 files) | ||||
Property (1 file) | ||||
EntityBuilder.php | Class | Class source | ||
Strings.php | Class | Class source |
Files | / | src | / | Domain | / | Exception |
File | Role | Description |
---|---|---|
Exception.php | Class | Class source |
MappingException.php | Class | Class source |
PropertyNotNullableException.php | Class | Class source |
PropertyUndefinedException.php | Class | Class source |
ValueCannotBeCaste...edTypeException.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
Stubs (1 directory) | ||||
Unit (2 files) | ||||
UnitTestCase.php | Class | Class source |
Files | / | tests | / | Stubs | / | Unit |
File | Role | Description | ||
---|---|---|---|---|
BasicUnitTest (5 files) | ||||
ComplexUnitTest (10 files) |
Files | / | tests | / | Stubs | / | Unit | / | BasicUnitTest |
File | Role | Description |
---|---|---|
BasicUnitTestEntity.php | Class | Class source |
BasicUnitTestEntityBarMissing.php | Class | Class source |
BasicUnitTestEntityNoGetters.php | Class | Class source |
success.json | Data | Auxiliary data |
successHyphens.json | Data | Auxiliary data |
Files | / | tests | / | Stubs | / | Unit | / | ComplexUnitTest |
File | Role | Description |
---|---|---|
ComplexMainEntity.php | Class | Class source |
ComplexSubEntity.php | Class | Class source |
ComplexSubSubEntity.php | Class | Class source |
failureCastStringToInt.json | Data | Auxiliary data |
failureCastTooBigInt.json | Data | Auxiliary data |
failurePropertyCannotBeNull.json | Data | Auxiliary data |
failurePropertyNotNullable.json | Data | Auxiliary data |
failureSubSubNotNullable.json | Data | Auxiliary data |
success.json | Data | Auxiliary data |
successHyphens.json | Data | Auxiliary data |
Files | / | tests | / | Unit |
File | Role | Description |
---|---|---|
BasicUnitTest.php | Class | Class source |
ComplexUnitTest.php | Class | Class source |
Download all files: php-json-to-entity.tar.gz php-json-to-entity.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|