abgeo/json-to-popo
Composer 安装命令:
composer require abgeo/json-to-popo
包简介
Fill Plain Old PHP Object with JSON content.
README 文档
README
Fill Plain Old PHP Object with JSON content.
Installation
You can install this library with Composer:
composer require abgeo/json-to-popo
Usage
Include composer autoloader in your main file (Ex.: index.php)
require __DIR__.'/../vendor/autoload.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.
Changelog
Please see CHANGELOG for details.
Contributing
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.
Authors
- Temuri Takalandze - Initial work
License
Copyright © 2020 Temuri Takalandze.
Released under the MIT license.
abgeo/json-to-popo 适用场景与选型建议
abgeo/json-to-popo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.31k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2020 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「popo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 abgeo/json-to-popo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 abgeo/json-to-popo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 abgeo/json-to-popo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
Knit ties your PHP objects with your database of choice in a simple way.
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
PHP library that allows you to define your class' getters, setters and constructor with docblock annotations.
A bundle to easily use Accessible in Symfony projects.
统计信息
- 总下载量: 3.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-03