Recommend this page to a friend! |
Download |
Info | Documentation | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 136 | All time: 9,271 This week: 660 |
Version | License | PHP version | Categories | |||
php-json-to-entity 1.0.0 | Custom (specified... | 7 | Language, PHP 7 |
Description | Author | |
This package can map JSON decoded data into entity objects. |
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.
Files (33) |
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 (33) | / | src |
File | Role | Description | ||
---|---|---|---|---|
Domain (2 files, 2 directories) | ||||
Mapper.php | Class | Class source |
Files (33) | / | src | / | Domain |
File | Role | Description | ||
---|---|---|---|---|
Exception (5 files) | ||||
Property (1 file) | ||||
EntityBuilder.php | Class | Class source | ||
Strings.php | Class | Class source |
Files (33) | / | 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 (33) | / | tests |
File | Role | Description | ||
---|---|---|---|---|
Stubs (1 directory) | ||||
Unit (2 files) | ||||
UnitTestCase.php | Class | Class source |
Files (33) | / | tests | / | Stubs | / | Unit |
File | Role | Description | ||
---|---|---|---|---|
BasicUnitTest (5 files) | ||||
ComplexUnitTest (10 files) |
Files (33) | / | 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 (33) | / | 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 (33) | / | tests | / | Unit |
File | Role | Description |
---|---|---|
BasicUnitTest.php | Class | Class source |
ComplexUnitTest.php | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.