gmulti/morphism

安装: 17

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:项目

v1.0.1 2024-02-14 23:01 UTC

This package is auto-updated.

Last update: 2024-09-15 00:13:26 UTC


README

Build Status

帮助您将任何对象结构转换为另一种结构。

此库受Yann Renaudin的 Morphism库 的启发。

贡献

入门 🚀

使用composer安装 morphism-php : composer require gmulti/morphism-php

use Morphism\Morphism;

它做什么? 🤔

Morphism使用语义配置遍历您要处理的图对象集合。然后从指定的路径(s)提取并计算值。最后,它将此值设置为模式中的目标属性。

使用方法 🍔

Morphism是允许使用语义配置进行部分应用的柯里化函数。您可以用很多种方式使用它

示例

// Target type you want to have
class User {
    public function __construct($firstName, $lastName, $phoneNumber){
        $this->firstName   = $firstName;
        $this->lastName    = $lastName;
        $this->phoneNumber = $phoneNumber;
        $this->city = null;
   }
}

// Data source you want to map
$data = array(
    "name"      => "Iron Man",
    "firstName" => "Tony",
    "lastName"  => "Stark",
    "address" => array(
        "city"    => "New York City",
        "country" => "USA"
    ),
    "phoneNumber" => array(
        array(
            "type"   => "home",
            "number" => "212 555-1234"
        ),
        array(
            "type"   => "mobile",
            "number" => "646 555-4567"
        )
    )
);

// Mapping Schema ( see more examples below )
$schema = array(
    "city" => "address.city",
    "name" => function($data){
        return strtoupper($data["name"]);
    }
);

Morphism::setMapper("User", $schema);

// Map using the registered type and the registry
$result = Morphism::map("User", $data);

/// *** OUTPUT *** ///

class User {
    public $city // string(13) "New York City"
    public $name  // string(8) "iron man"
}

多维数组

// Target type you want to have
class User {
}

// Data source you want to map
$data = array(
    array(
        "name"      => "Iron Man",
        "firstName" => "Tony",
        "lastName"  => "Stark",
        "address" => array(
            "city"    => "New York City",
            "country" => "USA"
        ),
        "phoneNumber" => array(
            array(
                "type"   => "home",
                "number" => "212 555-1234"
            ),
            array(
                "type"   => "mobile",
                "number" => "646 555-4567"
            )
        )
    ),
    array(
        "name"      => "Spiderman",
        "firstName" => "Peter",
        "lastName"  => "Parker",
        "address" => array(
            "city"    => "New York City",
            "country" => "USA"
        ),
        "phoneNumber" => array(
            array(
                "type"   => "home",
                "number" => "999 999-9999"
            )
        )
    )
);

// Mapping Schema ( see more examples below )
$schema = array(
    "city" => "address.city",
    "name" => function($data){
        return strtoupper($data["name"]);
    }
);

Morphism::setMapper("User", $schema);

// Map using the registered type and the registry
$result = Morphism::map("User", $data);

/// *** OUTPUT *** ///

array(
    class User {
        public $city // string(13) "New York City"
        public $name  // string(8) "iron man"
    },
    class User {
        public $city // string(13) "New York City"
        public $name  // string(8) "spiderman"
    }
)

模式示例

数据集样本

$data = array(
    "name"      => "Iron Man",
    "firstName" => "Tony",
    "lastName"  => "Stark",
    "address" => array(
        "city"    => "New York City",
        "country" => "USA"
    ),
    "phoneNumber" => array(
        array(
            "type"   => "home",
            "number" => "212 555-1234"
        ),
        array(
            "type"   => "mobile",
            "number" => "646 555-4567"
        )
    )
);

// Target type you want to have
class User {
}

聚合器

// Schema
$schema = array(
    "fullName" => array("firstName", "lastName")
);

Morphism::setMapper("User", $schema);

// Map using the registered type and the registry
$result = Morphism::map("User", $data);

/// *** OUTPUT *** ///

class User {
    public $fullName // "Tony Stark"
}

在展平/投影上进行计算

// Schema
$schema = array(
    "city" => (object) array(
        "path" => "address.city",
        "fn"   => function($city) {
            return strtolower($city);
        }
    ),
    "nbContacts" => function($data){
        return count($data["phoneNumber"]);
    }
);

Morphism::setMapper("User", $schema);

// Map using the registered type and the registry
$result = Morphism::map("User", $data);

/// *** OUTPUT *** ///

class User {
    public $city // "new york city" <= strtolower
    public $nbContacts // 2 <= computed from the object
}

许可证

MIT © Thomas Deneulin