Recommend this page to a friend! |
Download .zip |
Info | Example | View files (43) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2020-08-10 (1 month ago) | Not yet rated by the users | Total: 84 This week: 2 | All time: 9,553 This week: 169 |
Version | License | PHP version | Categories | |||
json-to-popo 1.0.0 | Custom (specified... | 5 | PHP 5, Data types |
Fill Plain Old PHP Object with JSON content.
You can install this library with Composer:
Include composer autoloader in your main file (Ex.: index.php)
Consider that you have example.json
with the following content:
{
"firstName": "Temuri",
"lastName": "Takalandze",
"active": true,
"position": {
"title": "Developer",
"department": {
"title": "IT"
}
}
}
and several POPO classes to represent this JSON data:
Department.php
<?php
class Department
{
/
* @var string
*/
private $title;
// Getters and Setters here...
}
Position.php
<?php
class Position
{
/
* @var string
*/
private $title;
/
* @var \ABGEO\POPO\Example\Department
*/
private $department;
// Getters and Setters here...
}
Person.php
<?php
class Person
{
/
* @var string
*/
private $firstName;
/
* @var string
*/
private $lastName;
/
* @var bool
*/
private $active;
/
* @var \ABGEO\POPO\Example\Position
*/
private $position;
// Getters and Setters here...
}
Note: All POPO properties must have full qualified @var
annotation with the correct data type.
Now you want to convert this JSON to POPO with relations. This package gives you this ability.
Let's create new ABGEO\POPO\Composer
object and read example.json
content:
$composer = new Composer();
$jsonContent = file_get_contents(__DIR__ . '/example.json');
Time for magic! Call composeObject()
with the contents of JSON and the main class, and this will give you POPO:
$resultObject = $composer->composeObject($jsonContent, Person::class);
Print $resultObject
:
var_dump($resultObject);
//class ABGEO\POPO\Example\Person#2 (4) {
// private $firstName =>
// string(6) "Temuri"
// private $lastName =>
// string(10) "Takalandze"
// private $active =>
// bool(true)
// private $position =>
// class ABGEO\POPO\Example\Position#4 (2) {
// private $title =>
// string(9) "Developer"
// private $department =>
// class ABGEO\POPO\Example\Department#7 (1) {
// private $title =>
// string(2) "IT"
// }
// }
//}
See full example here.
Please see CHANGELOG for details.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Copyright © 2020 Temuri Takalandze. Released under the MIT license.
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 file) | ||||
examples (2 files, 1 directory) | ||||
src (1 file, 1 directory) | ||||
tests (4 files, 1 directory) | ||||
.coveralls.yml | Data | Auxiliary data | ||
.travis.yml | Data | Auxiliary data | ||
CHANGELOG.md | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml.dist | Data | Auxiliary data | ||
README.md | Doc. | Read me |
Files | / | examples |
File | Role | Description | ||
---|---|---|---|---|
POPO (3 files) | ||||
example.json | Data | Auxiliary data | ||
example.php | Example | Example script |
Files | / | examples | / | POPO |
File | Role | Description |
---|---|---|
Department.php | Class | Class source |
Person.php | Class | Class source |
Position.php | Class | Class source |
Files | / | src | / | Util |
File | Role | Description |
---|---|---|
AnnotationParser.php | Class | Class source |
Normalizer.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
Meta (2 directories) | ||||
AnnotationParserTest.php | Class | Class source | ||
bootstrap.php | Aux. | Auxiliary script | ||
ComposerTest.php | Class | Class source | ||
NormalizerTest.php | Class | Class source |
Files | / | tests | / | Meta | / | Classes |
File | Role | Description |
---|---|---|
Class1.php | Class | Class source |
Class10.php | Class | Class source |
Class11.php | Class | Class source |
Class2.php | Class | Class source |
Class3.php | Class | Class source |
Class4.php | Class | Class source |
Class5.php | Class | Class source |
Class6.php | Class | Class source |
Class7.php | Class | Class source |
Class8.php | Class | Class source |
Class9.php | Class | Class source |
Files | / | tests | / | Meta | / | JSON |
File | Role | Description |
---|---|---|
1.json | Data | Auxiliary data |
10.json | Data | Auxiliary data |
11.json | Data | Auxiliary data |
12.json | Data | Auxiliary data |
2.json | Data | Auxiliary data |
3.json | Data | Auxiliary data |
4.json | Data | Auxiliary data |
5.json | Data | Auxiliary data |
6.json | Data | Auxiliary data |
7.json | Data | Auxiliary data |
8.json | Data | Auxiliary data |
9.json | Data | Auxiliary data |
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.