macellan/filter
Composer 安装命令:
composer require macellan/filter
包简介
Makes input and output filtering Eloquent models easy
README 文档
README
Aims to help make filtering input to your Eloquent models easier.
Simplifies code like this:
class Address extends Model {
public function setPostcodeAttribute($value) {
$this->attributes['postcode'] = strtoupper(trim($value));
}
public function setCityAttribute($value) {
$this->attributes['city'] = trim($value);
}
public function getCityAttribute($value) {
return strtoupper($value);
}
}
Into this:
class Address extends Model {
use Filter\HasFilters
protected $input = [
'postcode' => 'upper|trim',
'city' => 'trim'
];
protected $output = [
'city' => 'upper'
];
}
Can also be used standalone:
$clean = Filter::filter(['city' => 'London'], ['city' => 'trim|upper']);
Installation
Installable via composer:
"bcalik/filter": "dev-master",
Laravel 4
To use the model trait and service for Laravel 4, add the following lines to
config/app.php:
'providers' => array(
// ...
'Filter\FilterServiceProvider',
'aliases' => array(
// ...
'Filter' => 'Filter\Facades\Filter',
Usage
Examples below use the Facade style (
Filter::filter()) for brevity - standalone users should expand this to$filter->filter().
The standalone class is similar to Laravel's validator component:
$filtered = Filter::filter(['name' => 'Ross'], ['name' => 'trim']);
$value = Filter::filterOne('Ross', 'trim');
Rules are also constructed similarly to Validator:
Filter::filterOne('test', 'trim|upper');
Filter::filterOne('test...', 'rtrim:.');
Filter::filterOne('test', ['trim', 'upper']);
Filters are run sequentially from left to right. Arguments are parsed by
str_getcsv - e.g. to trim commas use trim:",".
Registering filters
A filter is a callable that accepts the input string and an array of arguments:
Filter::registerFilter('slugify', function($str, array $args) {
return preg_replace('/[^a-z0-9]+/', '-', strtolower($str));
});
Other callable values are classes that define an __invoke method and function
names. For example, Zend Framework's filters all implement __invoke, so
'Zend\I18n\Filter\Alnum' is a valid callable.
Filters can be unregistered using Filter::unregisterFilter('slugify').
Default filters
By default the following filters are registered:
trim trim($str)
trim:|,/ trim($str, '|/');
ltrim ltrim($str)
ltrim:|,/ ltrim($str, '|/');
rtrim rtrim($str)
rtrim:|,/ rtrim($str, '|/');
upper strtoupper($str)
lower strtolower($str)
capfirst ucfirst($str)
lowerfirst lcfirst($str)
slug Str::slug($str)
null empty($str) ? null : $str
Laravel 4
A trait, HasFilters is available that modifies getAttribute (accessor) and
setAttribute (mutator) to apply filters to the input or output value.
These filter rules are specified in properties on the model, $input and
$output for mutators and accessors respectively.
class Address extends Model {
use Filter\HasFilters;
public $fillable = ['line1', 'line2', 'line3', 'city', 'postcode'];
public $input = [
'line1' => 'trim',
'line2' => 'trim',
'line3' => 'trim',
'city' => 'trim',
'postcode' => 'upper|trim',
];
public $output = [
'city' => 'upper', // Uppercase only for display
];
}
The filter instance is available using App::make('filter'), or via the facade
Filter depending on your setup in config/app.php.
Call chain
You can still write your own accessors or mutators which will be applied as well as any filters that have been set. The following chains happen:
- Mutator:
$model->name = 'Ross'(filters applied before your mutator)Filter\HasFilters::setAttributeEloquent\Model::setAttributeYour\Model::setNameAttribute(if defined)
- Accessor:
echo $model->name(filters applied after your accessor)Eloquent\Model::getAttributeYour\Model::getNameAttributeFilter\HasFilters::getAttribute
You should not need to modify your mutators (they should still store the value
in $this->attributes[$name].
License
Released under the MIT license.
macellan/filter 适用场景与选型建议
macellan/filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.17k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 macellan/filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 macellan/filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 macellan/filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Symfony DataGridBundle
统计信息
- 总下载量: 1.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-07