danbettles/morf
Composer 安装命令:
composer require danbettles/morf
包简介
Morf was designed to filter request-parameters *en masse* but it is also useful in many other situations. A Morf *Filter* is created from an array of definitions that describes each parameter you're interested in, and serves-up valid, type-cast values; it'll spit-out an exception when something's o
README 文档
README
Morf was designed to filter request-parameters en masse but it is also useful in many other situations.
A Morf Filter is created from an array of definitions that describes each parameter you're interested in, and serves-up valid, type-cast values; it'll spit-out an exception when something's overtly wrong.
For safety's sake, Morf is strict and opinionated, and uses PHP built-ins whenever possible.
Example
It's pretty straightforward:
use DanBettles\Morf\Filter; $definitions = [ [ 'name' => 'anything', // N.B. `name` is the only required element // 'type' => 'string', // The default output-type ], [ 'name' => 'show_retirement', 'type' => 'bool', // Or use `"boolean"` // 'default' => false, // The built-in default value of a Boolean ], [ 'name' => 'show_under_offer', 'type' => 'bool', 'default' => true, // N.B. the same type as named in `type` ], [ 'name' => 'location_id', 'type' => 'int', // Or use `"integer"` 'default' => 7, 'validator' => 'positiveInteger', // See Built-In Validators, below ], [ 'name' => 'num_rooms', 'type' => 'int', 'default' => -1, 'validValues' => [-1, 1, 2, 3], // `validValues` takes precedence over `validator` ], [ 'name' => 'a_floating_point_number', 'type' => 'float', // Or use `"double"` ], [ 'name' => 'an_array', 'type' => 'array', ], ]; // Empty request, so defaults applied: $actual = Filter::create($definitions)->filter([]); $expected = [ 'anything' => '', 'show_retirement' => false, 'show_under_offer' => true, 'location_id' => 7, 'num_rooms' => -1, 'a_floating_point_number' => 0.0, 'an_array' => [], ]; assert($expected === $actual); // Form submitted, say: $actual = Filter::create($definitions)->filter([ 'anything' => 'Hello, World!', 'show_retirement' => '0', 'show_under_offer' => '0', 'location_id' => '2', 'num_rooms' => '2', 'a_floating_point_number' => '3.142', 'an_array' => ['foo', 'bar', 'baz'], ]); $expected = [ 'anything' => 'Hello, World!', 'show_retirement' => false, 'show_under_offer' => false, 'location_id' => 2, 'num_rooms' => 2, 'a_floating_point_number' => 3.142, 'an_array' => ['foo', 'bar', 'baz'], ]; assert($expected === $actual);
Valid Inputs
| Type | Valid Input |
|---|---|
bool, boolean |
"1", "0"1 |
int, integer |
Any integer in string form |
float, double |
Any float/integer in string form |
string |
Any string |
array |
Any array |
Default Values
The built-in default values for each output-type—the values returned when parameters aren't set—are the same as those used in Symfony\Component\HttpFoundation\ParameterBag, which we think are intuitive. No problem if you disagree, or if your application requires something different, because you can specify parameter-level default values—see the example above.
Built-In Validators
| Name | Valid Value |
|---|---|
positiveInteger |
Integer > 0 |
nonNegativeInteger |
Integer >= 0 |
Footnotes
-
We take the view that being explicit is better (safer) in the long run. So, for example, a Boolean parameter with a blank value is treated as invalid because you can easily represent a Boolean value using
"1"or"0"—which we think is more intuitive in any case. ↩
danbettles/morf 适用场景与选型建议
danbettles/morf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 950 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 danbettles/morf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 danbettles/morf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 950
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: ISC
- 更新时间: 2023-11-13