colybri/criteria
Composer 安装命令:
composer require colybri/criteria
包简介
Helper classes for implement filter/criteria pattern
README 文档
README
This package allows build customized criteria and any kind of filters and also include an adapter for use with Doctrine Database Abstraction Layer.
Domain Driven Design series
This package is the first of a series of high level building blocks to build your applications with Domain Driven Design approach.
Table of contents
Installation
Via Composer
$ composer require colybri/criteria
Usage
Create a criteria
new Criteria( Filters::from([ Filter::from( FilterField::from(CountryName::class), FilterOperator::Contains, FilterValue::from('Samoa') ), Filter::from( FilterField::from(CountryAlpha2Code::class), FilterOperator::Contains, FilterValue::from('WS') ) ... ]), Order::from(OrderBy::from(CountryName::class), OrderType::Desc), 0, 100 );
A filter is composed by three objects. First is field key to match with your key/value map of field's names of your columns on database. Second parameter is the operator you want to use on your condition. The last one is the value you want to match.
Specify order
First parameter is the key of the field for order result. Secondly, order type.
Order::from(OrderBy::from(CountryName::class), OrderType::Desc),
Limit results
If you don't want to limit your results simply set null to the two last parameters of Criteria. Otherwise set offset and limit.
Nested filters
In order use logic operator OR or nested conditions.
new Criteria( Filters::from( Conjunction::fromfilters( Disjunction::fromfilters( Filter::from( FilterField::from(CountryAlpha2Code::class), FilterOperator::Equal, FilterValue::from('SW') ), Filter::from( FilterField::from(CountryAlpha2Code::class), FilterOperator::Equal, FilterValue::from('WS') ) ... ), Filter::from( FilterField::from(CountryName::class), FilterOperator::Contains, FilterValue::from('Samoa') ), ... ) ), Order::none(), null, null );
Adapter
Dbal adapter
In your repository:
$query = $this->dbalConnection->createQueryBuilder() ->select('*')->from('countries'); (new CriteriaDbalAdapter($query, new CountryMap()))->build($criteria); $countries = $query->executeQuery()->fetchAllAssociative();
Map database fields
In the above example CountryMap is a simple class that must implement EntityMap interface. As show bellow. The attribute FIELDS is a key/value array where the key is the semantic name that you use on your domain and the value is the column name that match on database.
final class CountryMap implements EntityMap { private const FIELDS = [ CountryName::class => 'en_short_name', CountryAlpha2Code::class => 'alpha_2_code' ]; private const TABLE = 'countries'; public function map(string $attribute): string { return self::FIELDS[$attribute]; } public static function table(): string { return self::TABLE; } }
Credits
License
colybri/criteria 适用场景与选型建议
colybri/criteria 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 colybri/criteria 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 colybri/criteria 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 60
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-04