meklis/array-to-object-mapper

轻松将数组映射到嵌套对象

0.1 2022-02-20 12:07 UTC

This package is auto-updated.

Last update: 2024-09-11 20:11:43 UTC


README

功能

  • 映射嵌套结构
  • 与文档注释一起工作
  • 从 @var 声明检测变量类型
  • 与私有和受保护的属性一起工作
  • 严格模式允许检查变量类型(仅适用于基本类型)
  • 与 map 或对象数组一起工作
  • 自动检测并映射 camelCase 和 snake_case 属性

安装

composer require meklis/array-to-object-mapper

使用示例

在 src/example 中查看使用示例的完整示例

ParentClass.php

class ParentClass
{
    /**
     * @var int
     */
    protected $id;

    /**
     * String with camelCases
     * @var string
     */
    protected $testString;

    /**
     * @var string
     */
    protected $variable_with_snake_case;

    /**
     * @var Child
     */
    protected $child;

    /**
     * @var Child[]
     */
    protected $childs;
}

Child.php

class Child
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var Child | null
     */
    protected ?Child $child;

    /**
     * @var array
     */
    protected $params = [];
}

classes.json

{
  "id": 144,
  "test_string": "This is test string",
  "variable_with_snake_case": "Variable with snake case",
  "child": {
    "id": 1,
    "child": null,
    "params": ["param1", "param2", "param3"]
  },
  "childs": [
    {
      "id": 1,
      "child": {
        "id": 6,
        "params": ["param1", "param2", "param3"]
      },
      "params": ["param1", "param2", "param3"]
    },
    {
      "id": 3,
      "child": null,
      "params": ["param1", "param2", "param3"]
    },
    {
      "id": 4,
      "child": null,
      "params": ["param1", "param2", "param3"]
    }
  ]
}

test.php - 测试文件

require __DIR__ . '/../../vendor/autoload.php';

use \Meklis\ArrToObjectMapper\Mapper;
use \Meklis\ArrToObjectMapper\example\Classes\ParentClass;

$mapper = new Mapper();
$mapper->setStrict(true);
$data = json_decode(file_get_contents(__DIR__ . '/classes.json'),true);

$mapped = $mapper->map($data, ParentClass::class);

print_r($mapped);

// OUTPUT
/*
Meklis\ArrToObjectMapper\example\Classes\ParentClass Object
(
    [id:protected] => 144
    [testString:protected] => This is test string
    [variable_with_snake_case:protected] => Variable with snake case
    [child:protected] => Meklis\ArrToObjectMapper\example\Classes\Child Object
        (
            [id:protected] => 1
            [child:protected] => 
            [params:protected] => Array
                (
                    [0] => param1
                    [1] => param2
                    [2] => param3
                )

        )

    [childs:protected] => Array
        (
            [0] => Meklis\ArrToObjectMapper\example\Classes\Child Object
                (
                    [id:protected] => 1
                    [child:protected] => Meklis\ArrToObjectMapper\example\Classes\Child Object
                        (
                            [id:protected] => 6
                            [params:protected] => Array
                                (
                                    [0] => param1
                                    [1] => param2
                                    [2] => param3
                                )

                        )

                    [params:protected] => Array
                        (
                            [0] => param1
                            [1] => param2
                            [2] => param3
                        )

                )

            [1] => Meklis\ArrToObjectMapper\example\Classes\Child Object
                (
                    [id:protected] => 3
                    [child:protected] => 
                    [params:protected] => Array
                        (
                            [0] => param1
                            [1] => param2
                            [2] => param3
                        )

                )

            [2] => Meklis\ArrToObjectMapper\example\Classes\Child Object
                (
                    [id:protected] => 4
                    [child:protected] => 
                    [params:protected] => Array
                        (
                            [0] => param1
                            [1] => param2
                            [2] => param3
                        )

                )

        )

)
 */