lukasss93/smatch
Composer 安装命令:
composer require lukasss93/smatch
包简介
Match for PHP 7.3 and PHP 7.4
README 文档
README
Smatch
Match for PHP 7.3 and PHP 7.4
🚀 Installation
You can install the package using composer:
composer require lukasss93/smatch
👓 Usage
Structure of a smatch function
$result = smatch('apple') ->case('pear', 'tasty') ->case('apple', 'delicious') ->case('banana', 'yellow') ->get(); // $result = 'delicious'
Case methods value can accept a closure too.
$result = smatch('apple') ->case('pear', fn () => 'tasty') ->case('apple', fn () => 'delicious') ->case('banana', fn () => 'yellow') ->get(); // $result = 'delicious'
Case method may contain an array of values
$result = smatch('chair') ->case(['apple', 'pear', 'banana'], 'fruit') ->case(['table', 'chair'], 'furniture') ->get(); // $result = 'furniture'
A special case is the fallback pattern. This pattern matches anything that wasn't previously matched.
$result = smatch('strawberry') ->case('pear', 'tasty') ->case('apple', 'delicious') ->case('banana', 'yellow') ->fallback('invalid') ->get(); // $result = 'invalid'
Fallback method can accept a closure too.
$result = smatch('strawberry') ->case('pear', 'tasty') ->case('apple', 'delicious') ->case('banana', 'yellow') ->fallback(fn () => 'invalid') ->get(); // $result = 'invalid'
Note: Multiple fallback methods will override the last one.
A smatch function must be exhaustive. If the subject function is not handled by any case method an UnhandledSmatchException is thrown.
Example of an unhandled smatch function
try { $result = smatch('strawberry') ->case('pear', 'tasty') ->case('apple', 'delicious') ->case('banana', 'yellow') ->get(); } catch (UnhandledSmatchException $e){ echo $e->getMessage(); } // $e->getMessage() = Unhandled smatch value of type string
Using getOr method to handle missing fallback method
$result = smatch('car') ->case('pear', 'tasty') ->case('apple', 'delicious') ->case('banana', 'yellow') ->getOr(fn () => 'complex logic'); // $result = 'complex logic'
Using smatch function to handle non identity checks
It is possible to use a smatch function to handle non-identity conditional cases by using true as the subject function.
Using a generalized smatch function to branch on integer ranges
$age = 23; $result = smatch(true) ->case($age >= 65, 'senior') ->case($age >= 25, 'adult') ->case($age >= 18, 'young adult') ->fallback('kid') ->get(); // $result = 'young adult'
Using a generalized smatch function to branch on string content
$text = 'Bienvenue chez nous'; $result = smatch(true) ->case(str_contains($text, 'Welcome') || str_contains($text, 'Hello'), 'en') ->case(str_contains($text, 'Bienvenue') || str_contains($text, 'Bonjour'), 'fr') ->get(); // $result = 'fr'
⚗️ Testing
composer test
📃 Changelog
Please see the CHANGELOG.md for more information on what has changed recently.
🏅 Credits
📖 License
Please see the LICENSE.md file for more information.
lukasss93/smatch 适用场景与选型建议
lukasss93/smatch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 04 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 lukasss93/smatch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lukasss93/smatch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-30