tklein/php-combine-conditions
Composer 安装命令:
composer require tklein/php-combine-conditions
包简介
Generic representation of conditional structure/tree library for PHP.
README 文档
README
Generic representation of conditional structure/tree library for PHP.
Installation
Composer (recommended)
This package is available through Composer/Packagist:
composer require tklein/php-combine-conditions
Getting Started
The combine conditions library for PHP is a tool which helps you to build dynamically structured (as tree or graph) conditions.
These conditions are able to be combined and customized operators can be provided.
This library can be used in many context, to execute advanced combination of conditions, to generate some complex SQL
conditions, or to be exported to multiple formats as JSON, XML, FULLTEXT...
An other case to use this library is to execute and process frontend conditional tree (Eg: a user build his conditional
tree and would like to process the result).
The library has been written in order to allows you to implement it to the right way you want. So, if you implement the interfaces from the public API, you can build your own combine conditions structure and continue to execute it via the public API services.
The library is written to follow the PSR12 coding standards.
Main Library Entrance
For a simply use, you'll only need to use the following class: \LogicTree\LogicTreeFacade.
This class allows you to use the mainly features of the library, as to build a combined conditions, to execute some, and
exports them.
OperatorPool
What's an OperatorPool? It's a class which regroup the whole operator instances used in the library.
You can register or retrieve OperatorInterface from this class. The goal is to use them in your combination of conditions.
The operators are divided in two large groups:
- The comparator operator.
- The logical operator.
Comparator Operator
The comparator operator is used to determine the result of a boolean expression.
Eg:
Does 'A' and 'B' are the same thing? I can test with an equality operator: 'A' == 'B').
The default comparator operators are available in: src/Operator/Comparator.
It's used in the ConditionInterface definition, known as the Condition object.
Logical Operator
The logical operator is used to commute many boolean expression (at least two).
Eg:
'A' != 'B' result should be the same as 'C' != 'D'.
I can check with the following expression 'A' != 'B' AND 'C' != 'D'.
The default logical operators are available in: src/Operator/Logical.
It's used in the CombineInterface definition, known as the Combine object.
Override/Add new Operators
The library allows you to override the default operators and/or to provide new ones:
- You should instantiate the
OperatorPoolclass. - Create your operators. They must implement the interface
OperatorInterface. - Add your operators (two available types:
logicalandcomparator) to theOperatorPoolobject. - Finally, pass the
OperatorPoolobject in the construct of theConditionManagerclass.
Eg: In the following example, we override the default comparators with ours and add a new one.
$operatorPool = new \LogicTree\Operator\OperatorPool(); $operatorPool->addOperator( \LogicTree\Operator\OperatorType::Logical, \LogicTree\Operator\Logical\LogicalOperatorConverter::CODE, new \My\Class\AndOperator() ); $operatorPool->addOperator( \LogicTree\Operator\OperatorType::Comparator, \LogicTree\Operator\Comparator\EqOperator::CODE, new \My\Class\EqOperator() ); $operatorPool->addOperator( \LogicTree\Operator\OperatorType::Comparator, 'my_custom_operator', new \My\Class\MyCustomOperator() ); $conditionManager = new \LogicTree\Service\ConditionManager($operatorPool);
Now we are able to use these operators in our code and are available everywhere in the library.
Condition and Combine
These class always implement the NodeInterface. It represent the base model of the library.
Because of the composition pattern, you may want to know if the current object is the root or a node, you
should use the following methods:
hasParent(): check if the current object has parent or not.getParent(): retrieve the current object's parent.ConditionInterface: is an instance of, represent the last/final node.CombineInterface: is an instance of, represent the container node.
Condition
ConditionInterface: it represents an expression with a comparator operator.
The interface is: \LogicTree\Model\ConditionInterface.
The concrete default class is: \LogicTree\Model\Condition.
It requires a mixed value to compared by an operator to a value from the data source.
The operator is a string which is the code of the wanted operator.
The first value is a string which is the identifier of the value from the DataSource object.
The value to compare must be a mixed value.
Combine
Combine: it represents an expression with a logical operator.
The interface is: \LogicTree\Model\CombineInterface.
The concrete default class is: \LogicTree\Model\Combine.
It requires at least two conditions and/or combines to commute with a logical operator.
The operator is a string which is the code of the wanted operator.
The conditions/combine must be an instance of ConditionInterface.
You can add a NodeInterface to the CombineInterface with the following methods:
addChild(NodeInterface $node): add a new node child to the parent node object.setChildren(array $nodes): replace the children by the new ones.
If you want to invert the result of the combination of conditions, you can specify to the CombineInterface
object that the result should actually be inverted:
setIsInvert(bool $isInvert).
Execute the Combine Conditions
In order to execute the combined conditions, you must use the \LogicTree\Service\ConditionManager class.
This service allows to perform many actions on your NodeInterface object.
Actually only the execute method is available from the public API.
Anyway, the execute method allows you to determine the final result of your combination of conditions.
Export the Combine Conditions to Format
No documentation available yet!
Examples
Examples of code usage cases are available in this file.
Running the tests
The tests are placed in the tests directory, at the root of the library.
No tests available yet!
Contributing
Any help is welcome, feel free to raise issues or to open pull requests. Further details in the CONTRIBUTING.md.
TODO List
Implement the following comparator operators:
- array("from" => $fromValue, "to" => $toValue)
- array("like" => $likeValue)
- array("moreq" => $moreOrEqualValue)
- array("finset" => $valueInSet)
- array("seq" => $stringValue)
- array("sneq" => $stringValue)
ToString methods, in order to render the combine conditions in full text / different formats.
Have to: AdapterPool (toFormat) - ConverterPool (getSymbol, getExpression, getFullExpression).
Possibility to add custom type node and process them in the services.
Study Namespace and Lib Name: trends
Todo Builders (setter... getter... then reset)
-> possibility of immutable implementation (do not break it!)
Add tests and examples: from scratch, with and without new operators, and execution (with expected results).
Authors
- Thomas Klein - Initial work - It's me!
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.
tklein/php-combine-conditions 适用场景与选型建议
tklein/php-combine-conditions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 12, 最近一次更新时间为 2018 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「operator」 「logic」 「condition」 「combine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tklein/php-combine-conditions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tklein/php-combine-conditions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tklein/php-combine-conditions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Empty Coalesce adds the ??? operator to Twig that will return the first thing that is defined, not null, and not empty.
FullTextSearchQueryLike is a PHP class which transforms search strings into clever SQL conditions with the LIKE operator
Addons to the display-logic module by UncleCheese
Routes HTTP requests to your code.
Extension provide very simply use enum for models (and others) in yii2
Tests of exceptions hierarchy
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-16