承接 baethon/union 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

baethon/union

Composer 安装命令:

composer require baethon/union

包简介

Union type for PHP

README 文档

README

Provides utilities to define tagged unions.

Tagged unions?

I'm not good with words, yet folks from Folktale describe it nicely:

Modelling data is important for a range of reasons. From performance to correctness to safety. Tagged unions give you a way of modelling choices that forces the correct handling of them, unlike predicate-based branching, such as the one used by if statements and other common control flow structures.

Installation

composer require baethon/union

Usage

To create a tag union it's required to create a class which extends Baethon\Union\AbstractUnion.

class Maybe extends \Baethon\Union\AbstractUnion
{
}

Also you need to define tags which will represent state of the union.

class Maybe extends \Baethon\Union\AbstractUnion
{
	const SOME = 'Some:x';

	const NONE = 'None';
}

Signatures

Each tag has a definition (called signature), which can be defined using following syntax:

{Name}[:[param1[, param2[, ...[, paramN]]]]]

Parameters define whether a tag will hold any value(s) (called arguments). They're optional.

Working with unions

Union can be invoked using static constructor:

$some = Maybe::Some(1);

To work with the state of the union you should use matchWith(). It will return the value returned by the matching callback:

function addTen(Maybe $maybe) {
	return $maybe->matchWith([
		'Some' => function ($x) {
			return $x + 10;
		},
		'None' => function () {
			throw new \Exception('Sorry, can\'t add a number to nothing');
		}
	]);
}

addTen($some); // 11

matchWith() will check if all possible branches are mapped:

  • if a tag is not covered by map UnderflowException will be thrown
  • if map covers more tags than defined ones it will throw 'OverflowException'.

It's possible to use wildcard map to cover all other cases:

$some->matchWith([
	'*' => function () {
		return 100;
	}
]); // 100

You can use defined const values in mapping:

$some->matchWith([
	Maybe::SOME => function () {},
	Maybe::NONE => function () {}
]);

Testing

./vendor/bin/phpunit

baethon/union 适用场景与选型建议

baethon/union 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 baethon/union 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-17