danack/convert

轻松地在对象和标量之间转换。

0.1.0 2020-03-20 00:53 UTC

This package is auto-updated.

Last update: 2024-09-21 20:48:57 UTC


README

一个轻量级库,简化对象与数组、JSON之间的转换。

Actions Status

设计目标

该库旨在使数据简单转换为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 在容器中运行。

拉取请求应具有完整的单元测试覆盖率。