danack / convert
轻松地在对象和标量之间转换。
0.1.0
2020-03-20 00:53 UTC
Requires
- ext-json: *
- seld/jsonlint: ^1.7.2
Requires (Dev)
- danack/coding-standard: 0.2.0
- infection/infection: ^0.15.3
- phpunit/phpunit: 9.0.1
- slevomat/coding-standard: ^4.8.4
- squizlabs/php_codesniffer: ^3.3.1
- yoast/yoastcs: 1.0
This package is auto-updated.
Last update: 2024-09-21 20:48:57 UTC
README
一个轻量级库,简化对象与数组、JSON之间的转换。
设计目标
该库旨在使数据简单转换为JSON,并便于定制对象的高级转换。
示例
将 Convert\ToJson
特性添加到对象后,您可以使用 toJson
将对象转换为JSON。
<?php
$article = new Article(
1,
"Example",
"This is an example of how to make an object convertible to Json."
);
echo $article->toJson();
// Output is:
// {"id":1,"title":"Example","text":"This is an example of how to make an object convertible to Json."}
将 Convert\FromJson
特性添加到类后,您可以使用静态方法 fromJson
创建实例。
<?php
$json = '{"id":1,"title":"Example","text":"This is an example of how to make an object convertible to Json."}';
$article = Article::fromJson($json);
echo "Id is: " . $article->getId() . "\n";
echo "Title is: " . $article->getTitle() . "\n";
// output is:
// Id is: 1
// Title is: Example
安装
composer require danack/convert
测试
我们有一些工具用于提高代码质量。请运行 sh runTests.sh
在本地运行所有测试,或使用 sh runTestsInContainer.sh
在容器中运行。
拉取请求应具有完整的单元测试覆盖率。