fattureincloud/api-filter
Composer 安装命令:
composer require fattureincloud/api-filter
包简介
A PHP parser for a sql-like filter
README 文档
README
This PHP library makes possible to parse a string parameter and use it to initialize a Filter that can be used on REST APIs; the input string is based on a simplified SQL-like WHERE clause, making it easy to understand and use.
The library returns a class-based representation of the filter, that can be used to generate the actual filter to apply to your API requests.
Disclaimer
This library is currently focused on parsing and building the representation of the query, it does not validate the query itself. For example, it doesn't check if a field exists or if it is supported by your filters, and it doesn't enforce permissions checks on the query. You should take care of it on your own.
Even if this filter uses a grammar that should reduce the risk of SQL Injection, you should adopt some strategy to validate the filters and avoid executing unsafe queries.
Install
Via Composer
$ composer require fattureincloud/api-filter
Usage
To parse a filter string, you can use the FattureInCloud\ApiFilter\FilterFactory class:
use FattureInCloud\ApiFilter\FilterFactory $str = "id = 5"; $factory = new FilterFactory(); $filter = $factory->initFilter($str);
The returned filter will be a composition of the classes contained in the FattureInCloud\ApiFilter\Filter package.
The string is based on triplets:
field op value
The field is a lowercase string, with dots and underscores.
The op is one of the following (unquoted):
- Equal: '='
- Greater than: '>'
- Greater than or equal to: '>='
- Less than: '<'
- Less than or equal to: '<='
- Not equal: '<>', '!='
The value can be one of:
- String: 'value'
- Booleam: true, false
- Int: 46
- Double: 12.34
You can escape the single quote in a string value by doubling it. The filter will take care of returning the expected string.
It is also possible to match a String against a Pattern using one of those operators:
- Like: 'like', 'LIKE'
- Contains: 'contains', 'CONTAINS'
- Starts With: 'starts with', 'STARTS WITH'
- Ends With: 'ends with', 'ENDS WITH'
Like and Contains can also be negated:
- Not Like: 'not like', 'NOT LIKE'
- Not Contains: 'not contains', 'NOT CONTAINS'
Additionally, it is possible to check if field has a value or not, using NULL:
- Null field: 'IS NULL', 'is null', '= null', '= NULL'
- Not Null field: 'IS NOT NULL', 'is not null', '<> null', '!= null', '<> NULL', '!= NULL'
It is possible to use the following operators:
- Conjunction: 'and', 'AND'
- Disjunction: 'or', 'OR'
Parenthesis can be used to compose complex expressions.
For example:
city = 'Bergamo' and (age < 30 or (dev = true and (name = 'Giorgio' and surname is not null) or employer starts with 'Fatture'))
Testing
$ composer test
Parser generation
The parser was generated automatically using ANTLR and the PHP target; it is placed in the FattureInCloud\ApiFilter\Parser repository. Usually you should not directly manage the parser: the FilterFactory class is a wrapper that manages it for you.
The grammar is placed under the /grammar folder, if needed you can trigger the parser generation using docker and composer:
$ composer generate
If needed, you can generate a parser in another language changing the ANTLR target in the entrypoint.sh file.
License
The MIT License (MIT).
fattureincloud/api-filter 适用场景与选型建议
fattureincloud/api-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.36k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「filter」 「sql-like」 「fattureincloud」 「api-filter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fattureincloud/api-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fattureincloud/api-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fattureincloud/api-filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
Symfony DataGridBundle
A PSR-7 compatible library for making CRUD API endpoints
Data provider for yii2
This Laravel Nova package allows you to detach filters from the filter dropdown
Laravel filter elequent
统计信息
- 总下载量: 29.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-26