marco-fiset/flute
Composer 安装命令:
composer require marco-fiset/flute
包简介
Highly extensible validation framework for PHP
README 文档
README
An utterly awesome and easy-to-use, lightweight and highly-extensible fluent validation framework for PHP.
Flute's fluent interface is heavily inspired by FluentValidation for .NET.
Here is a simple example to get you started :
require 'path/to/flute.php'; $validator = new Validator(); $validator->rule_for('first_name')->max_length(100); $p = new Person('John'); $result = $validator->validate($p); if ($result->valid()) { echo 'Valid!'; }
These validation rules are supported out of the box :
- NotNull
- NotEmpty
- Required
- MaxLength
- ... (and many more. You can also add rules yourself)
Creating custom validation rules
It's very easy to create custom rules, and you don't even have to alter the Validator class. Everything is done with magic naming conventions! For example, here is how the NotEmpty rule is built :
// The naming is very important here. More on that later. class NotEmptyRule extends Rule // We must extend the Rule abstract class { //We override the condition function to run our own validation logic public function condition($value) { // The $value variable contains the value we need to validate. // For this particular case, it is considered valid if it is // not equal to the empty string. return $value !== ''; } }
Once your rule is defined, it must be discoverable by any registered auto-loader in order the make it available to the Validator class. Here is how you invoke it :
// Create an instance of the validator $v = new Validator(); $v->rule_for('first_name')->not_empty();
See what I did there? No need to add the not_empty() function to the validator. Any unknown function invoked on a validator will create a rule named with the following conventions :
- Every word delimited by underscores is capitalized.
- The underscores are removed.
- 'Rule' is appended to the resulting string.
So not_empty becomes NotEmptyRule. The validator will instantiate a NotEmptyRule class and associate it with the property name defined when we called rule_for. Easy, huh?
What if I want to use parameters?
Very simple. Here is a brief look at the MaxLengthRule implementation.
class MaxLengthRule extends Rule { public function condition($value) { return strlen($value) <= $this->max_length; //W-w-w-wait, what!? Where does max_length come from? } } $v = new Validator(); $v->rule_for('first_name')->max_length(100);
Any parameters passed to the function definition will be registered in the args array in the same order that they were passed to the validator rule. You can also access args with the magic __get method, like I did here with max_length. I you were to call max_length again, it will send back the same value as the first time it was called. When using multiple arguments, they are handed out in the order they were passed, and each name gets its own argument.
Extending existing rules
Your custom rule can extend the behaviour of existing rules. Let's take a look at the RequiredRule implementation :
class RequiredRule extends Rule { public function extend() { return array( new NotNullRule(), new NotEmptyRule() ); } }
As you can see, the RequiredRule is only a combination of the NotNullRule and the NotEmptyRule. The extend function returns an array of instantiated rules which you want to extend. The condition function will be called on both rules and combined with a logical and. That is, if any of the condition fails, it is considered invalid.
That's it for the moment! Stay tuned for more awesome features!
marco-fiset/flute 适用场景与选型建议
marco-fiset/flute 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 13, 最近一次更新时间为 2013 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「fluent」 「validation」 「extensible」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marco-fiset/flute 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marco-fiset/flute 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marco-fiset/flute 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Presentation and Formatting helper with nice fluent interface.
An extensible DTO library that allows map incoming API data to any of your entities/models by using a simple filed mapping language with filters and functions.
Extensible search for Elemental.
Traits gathering fluent syntax common methods
A modern, fluent HTTP client. Forked from Mashape, reimagined by Nidux, and open-sourced for the PHP community.
A fluent PHP CURL wrapper
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: DBAD
- 更新时间: 2013-01-24