PHP code example of godjarvis / conversion

1. Go to this page and download the library: Download godjarvis/conversion library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

godjarvis / conversion example snippets


class UserInfoConversion extends PathAdapter
{
    public $targetConversion = [
        'department.*'         => 'string',
        'mobile'               => 'integer',
        'extattr.attrs.*.age'  => 'int',
        'extattr.attrs.*.male' => 'boolean',
    ];
}

//转换处理
$newData = (new UserInfoConversion($oldData))->convert();

//使用jsonSchema压缩后的json字符串
class UserInfoConversion extends JsonSchemaAdapter
{
    public $targetConversion = '{"type":"object","properties":{"name":{"type":"string"},"department":{"type":"array","items":{"type":"string"}},"mobile":{"type":"number"},"extattr":{"type":"object","properties":{"attrs":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"number"},"male":{"type":"boolean"}}}}}}}}';

}

//或者使用jsonSchema格式的数组
class UserInfoConversion extends JsonSchemaAdapter
{
    public $targetConversion = [
        'type'         => 'object',
        'properties'   => [
            .......
        ],
        ........
    ];
}

//转换处理
$newData = (new UserInfoConversion($oldData))->convert();

class UserInfo
{
    /** @var string */
    public $name;
    /** @var string[] */
    public $department;
    /** @var int */
    public $mobile;
    /** @var ExtattrInfo */
    public $extattr;
}

class ExtattrInfo
{
    /** @var array<AttrInfo> */
    public $attrs;
}

class AttrInfo
{
    /** @var string */
    public $name;
    /** @var int */
    public $age;
    /** @var bool */
    public $male;
}

class UserInfoConversion extends ObjectAdapter
{
    public $targetConversion = UserInfo::class;
}

//转换处理
$newData = (new UserInfoConversion($oldData))->convert();