定制 danbettles/morf 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

danbettles/morf

最新稳定版本:v2.0.0

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

  1. 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.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: ISC
  • 更新时间: 2023-11-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固