saasformation/magic-field 问题修复 & 功能扩展

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

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

saasformation/magic-field

Composer 安装命令:

composer require saasformation/magic-field

包简介

MagicField is a library to convert mixed values into desired typed values -when doable.

README 文档

README

FieldMagic is a library to convert mixed values into desired typed values -when doable.

It is very useful, for instance, when reading JSON body requests in controllers.

Installation

Use composer to require the library:

composer require saasformation/field-magic

Getting started

Let's imagine you have the following array with data (maybe from a json_decode call):

$data = [
    'name' => 'John',
    'birthdate' => '1990-02-03',
    'height' => 177,
    'alive' => true,
    'highSchoolQualificationsAverage' => 4.56,
    'professions' => [
        'computing engineer',
        'soldier'
    ]
];

Now, you can do the following:

$name = (new StandardField($data['name']))->string();
$birthdate = (new StandardField($data['birthdate']))->datetime();
$height = (new StandardField($data['height']))->integer();
$alive = (new StandardField($data['alive']))->boolean();
$highSchoolQualificationsAverage = (new StandardField($data['highSchoolQualificationsAverage']))->float();
$professions = (new StandardField($data['professions']))->array();

Two advantages:

  1. You now have the value typed.
  2. If any of the source values were not compatible with type changing, an InvalidConversionException would be thrown.

If you were going to get data from a JSON body request in a controller, you could do something as cool as this:

abstract class BaseController {
    protected function field(string $path): StandardField {
        return new StandardField($this->getFromBodyRequestByPath($path));
    }
}

class Controller extends BaseController {
    public function doSomething(): Response {
        $this->commandBus->handle(
            new CreatePersonCommand(
                $this->field('data.attributes.name')->string(),
                $this->field('data.attributes.birthdate')->datetime(),
                $this->field('data.attributes.height')->integer(),
                $this->field('data.attributes.alive')->boolean(),
                $this->field('data.attributes.highSchoolQualificationsAverage')->float(),
                $this->field('data.attributes.professions')->array(),
            )
        )
    }
}

Adding a new converter

Sometimes you don't have enough with the StandardField converters available.

To add a new one, you have to create a new trait:

trait MyConverterCapable {
    public function addOneToInteger(): integer {
        return (int)$this->value + 1;
    }
}

Bear in mind $this->value is a mixed value, so for production proposes you may validate data better than in this example.

Now, you need to create a new Field class. If you want to have all the StandardField converters also available, you can inherit from StandardField. If not, directly from Field:

class MyConverterField extends StandardField
{
    use MyConverterCapable;
}

Now you can just use the new Field based class the same way as the StandardField one:

$addedInteger = (new MyConverterField(1)->addOneToInteger(); // 2

Users of stratdes/vo

Users of stratdes/vo may find saasformation/vo-magic-field-bridge useful to add converters for single VO's and collections.

Issues

If you find some issue in the library, please feel free to open an issue here on Github.

saasformation/magic-field 适用场景与选型建议

saasformation/magic-field 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 213 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 saasformation/magic-field 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 saasformation/magic-field 我们能提供哪些服务?
定制开发 / 二次开发

基于 saasformation/magic-field 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-03