cupcoffee/reify 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cupcoffee/reify

Composer 安装命令:

composer require cupcoffee/reify

包简介

A mapping library

README 文档

README

Reify is a small package used to hydrate objects. This can be useful when implementing API clients or creating abstraction layers in your application.

Installation

Insert warning about production usage here

composer require reify/reify:"dev-master"

Getting started

Let's say you have a dataset containing information about a person.

{
	"name": "Leroy",
	"profession": "Developer",
	"colleagues": [
		{
			"name": "Peter",
			"profession": "Developer"
		},
		{
			"name": "Sandra",
			"profession": "Developer"
		}
	],
	"spouse": null
}

You want to use this data in your application, however just using json_decode would result in a plain object with no typing and no way to add some functionality to them.

Reify can automatically map the data to any class you want. Let's try to map our data by using our Person class which i defined below.

use Reify\Reify;

$person = Reify::json($data)->to(Person::class);
use Reify\Attributes\Construct;
use Reify\Attributes\Type;

class Person {
    public string $name;
    
    #[Construct]
    public Profession $profession;
    
    #[Type(Person::class)]
    public array $colleagues;
    
    public ?Person $spouse;
    
    public object $meta;
}

Neat right? Let's dissect what's happening here.

class Person {
    /**
     * Using PHP 8 syntax we can define a type for each property
     * Reify supports all scalar values and custom types defined by you
     */
    public string $name;
    
    
    /**
     * If you have a single value that needs to be mapped to a type
     * You can use the Construct attribute. This will call the constructor with the value instead of mapping it.
     */
    #[Construct]
    public Profession $profession;
    
    /**
     * Unfortunately PHP does not have Generics.
     * We can however still define a type using the Type attribute.
 *   * Reify will try to map each value in the list to the given value
     */
    #[Type(Person::class)]
    public array $colleagues;

    /**
    * Not sure if the data is available? 
    * Reify will take your nullables in to account. 
    */    
    public ?Person $spouse;
    
    /**
    * You don't know what the data is going to look like? 
    * Reify supports mapping to plain objects aswell. 
    */
    public object $meta;
}

Conclusion

Reify is a concept I've been tinkering with over the years. It's a simple concept that I use very often in the projects I am working on.

This package is build with PHP 8 features for me to learn about them and fiddling with the new Attributes. Turns out I love metaprogramming and creating developer tools with them.

Building Reify i also had a chance to meet PestPHP and learn more about writing tests for my code.

Running tests

vendor/bin/pest

Alternatives

If you're looking for production worthy recommendations that I use myself please see below:

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-01-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固