{"id":16881941,"url":"https://github.com/thomas-kl1/php-combine-conditions","last_synced_at":"2025-03-23T16:32:59.629Z","repository":{"id":44952989,"uuid":"121737321","full_name":"thomas-kl1/php-combine-conditions","owner":"thomas-kl1","description":"Generic representation of conditional structure/tree library for PHP.","archived":false,"fork":false,"pushed_at":"2024-11-06T18:32:18.000Z","size":190,"stargazers_count":11,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-02T02:11:28.143Z","etag":null,"topics":["combined","comparator-operators","condition","conditional","conditionals","conditions","filter","group","group-filter","php","tree-library"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/tklein/php-combine-conditions","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomas-kl1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://paypal.me/thomaskl1"]}},"created_at":"2018-02-16T10:11:27.000Z","updated_at":"2022-03-29T18:37:08.000Z","dependencies_parsed_at":"2024-10-28T21:49:06.906Z","dependency_job_id":null,"html_url":"https://github.com/thomas-kl1/php-combine-conditions","commit_stats":{"total_commits":68,"total_committers":5,"mean_commits":13.6,"dds":"0.38235294117647056","last_synced_commit":"cfe7a8754e7ba6ca4ee3f3835d16565e2b63ede7"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomas-kl1%2Fphp-combine-conditions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomas-kl1%2Fphp-combine-conditions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomas-kl1%2Fphp-combine-conditions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomas-kl1%2Fphp-combine-conditions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomas-kl1","download_url":"https://codeload.github.com/thomas-kl1/php-combine-conditions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244313909,"owners_count":20433012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["combined","comparator-operators","condition","conditional","conditionals","conditions","filter","group","group-filter","php","tree-library"],"created_at":"2024-10-13T16:05:35.742Z","updated_at":"2025-03-23T16:32:59.236Z","avatar_url":"https://github.com/thomas-kl1.png","language":"PHP","funding_links":["https://paypal.me/thomaskl1"],"categories":[],"sub_categories":[],"readme":"# PHP Combine Conditions\n\n[![Codacy Badge](https://img.shields.io/codacy/grade/47d3b2e42bdb41f0a1b713a313dea870/master.svg?style=flat-square)](https://app.codacy.com/app/thomas-klein/php-combine-conditions)\n[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/thomas-kl1/php-combine-conditions/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/thomas-kl1/php-combine-conditions/?branch=master)\n[![Code Climate](https://img.shields.io/codeclimate/maintainability/thomas-kl1/php-combine-conditions.svg?style=flat-square)](https://codeclimate.com/github/thomas-kl1/php-combine-conditions/maintainability)\n[![Latest Stable Version](https://img.shields.io/packagist/v/tklein/php-combine-conditions.svg?style=flat-square)](https://packagist.org/packages/tklein/php-combine-conditions)\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/tklein/php-combine-conditions.svg?style=flat-square)](https://packagist.org/packages/tklein/php-combine-conditions)\n[![License: MIT](https://img.shields.io/github/license/thomas-kl1/php-combine-conditions.svg?style=flat-square)](./LICENSE)\n\nGeneric representation of conditional structure/tree library for PHP.\n\n - [Installation](#installation)\n - [Getting Started](#getting-started)\n   - [Main Library Entrance](#main-library-entrance)\n   - [OperatorPool](#operatorpool)\n     - [Comparator Operator](#comparator-operator)\n     - [Logical Operator](#logical-operator)\n     - [Override/Add new Operators](#override/add-new-operators)\n   - [Condition and Combine](#condition-and-combine)\n     - [Condition](#condition)\n     - [Combine](#combine)\n   - [Execute the Combine Conditions](#execute-the-combine-conditions)\n   - [Export the Combine Conditions to Format](#export-the-combine-conditions-to-format)\n - [Examples](#examples)\n - [Running the tests](#running-the-tests)\n - [Contributing](#contributing)\n - [Authors](#authors)\n - [License](#license)\n\n## Installation\n\n### Composer (recommended)\n\nThis package is available through Composer/Packagist:\n\n```\ncomposer require tklein/php-combine-conditions\n```\n\n## Getting Started\n\nThe combine conditions library for PHP is a tool which helps you to build dynamically structured (as tree or graph) conditions.  \nThese conditions are able to be combined and customized operators can be provided.  \nThis library can be used in many context, to execute advanced combination of conditions, to generate some complex SQL\nconditions, or to be exported to multiple formats as JSON, XML, FULLTEXT...  \nAn other case to use this library is to execute and process frontend conditional tree (Eg: a user build his conditional\ntree and would like to process the result).\n\nThe library has been written in order to allows you to implement it to the right way you want. So, if you implement the\ninterfaces from the public API, you can build your own combine conditions structure and continue to execute it via the public\nAPI services.\n\nThe library is written to follow the PSR12 coding standards.\n\n### Main Library Entrance\n\nFor a simply use, you'll only need to use the following class: `\\LogicTree\\LogicTreeFacade`.  \nThis class allows you to use the mainly features of the library, as to build a combined conditions, to execute some, and\nexports them.\n\n### OperatorPool\n\nWhat's an `OperatorPool`? It's a class which regroup the whole operator instances used in the library.  \nYou can register or retrieve `OperatorInterface` from this class. The goal is to use them in your combination of conditions.  \nThe operators are divided in two large groups:\n\n- The comparator operator.\n- The logical operator.\n\n#### Comparator Operator\n\nThe comparator operator is used to determine the result of a boolean expression.  \n*Eg:  \nDoes `'A'` and `'B'` are the same thing? I can test with an equality operator: `'A' == 'B'`).*\n\nThe default comparator operators are available in: `src/Operator/Comparator`.  \nIt's used in the `ConditionInterface` definition, known as the `Condition` object.\n\n#### Logical Operator\n\nThe logical operator is used to commute many boolean expression (at least two).  \n*Eg:  \n`'A' != 'B'` result should be the same as `'C' != 'D'`.  \nI can check with the following expression `'A' != 'B' AND 'C' != 'D'`.*\n\nThe default logical operators are available in: `src/Operator/Logical`.  \nIt's used in the `CombineInterface` definition, known as the `Combine` object.\n\n#### Override/Add new Operators\n\nThe library allows you to override the default operators and/or to provide new ones:  \n\n- You should instantiate the `OperatorPool` class.\n- Create your operators. They must implement the interface `OperatorInterface`.\n- Add your operators (two available types: `logical` and `comparator`) to the `OperatorPool` object. \n- Finally, pass the `OperatorPool` object in the construct of the `ConditionManager` class.\n\n*Eg: In the following example, we override the default comparators with ours and add a new one.*\n\n```php\n$operatorPool = new \\LogicTree\\Operator\\OperatorPool();\n  \n$operatorPool-\u003eaddOperator(\n    \\LogicTree\\Operator\\OperatorType::Logical,\n    \\LogicTree\\Operator\\Logical\\LogicalOperatorConverter::CODE,\n    new \\My\\Class\\AndOperator()\n);\n$operatorPool-\u003eaddOperator(\n    \\LogicTree\\Operator\\OperatorType::Comparator,\n    \\LogicTree\\Operator\\Comparator\\EqOperator::CODE,\n    new \\My\\Class\\EqOperator()\n);\n$operatorPool-\u003eaddOperator(\n    \\LogicTree\\Operator\\OperatorType::Comparator,\n    'my_custom_operator',\n    new \\My\\Class\\MyCustomOperator()\n);\n  \n$conditionManager = new \\LogicTree\\Service\\ConditionManager($operatorPool);\n```\nNow we are able to use these operators in our code and are available everywhere in the library.\n\n### Condition and Combine\n\nThese class always implement the `NodeInterface`. It represent the base model of the library.  \nBecause of the composition pattern, you may want to know if the current object is the root or a node, you\nshould use the following methods:\n\n- `hasParent()` : check if the current object has parent or not.\n- `getParent()` : retrieve the current object's parent.\n- `ConditionInterface` : is an instance of, represent the last/final node.\n- `CombineInterface` : is an instance of, represent the container node.\n\n#### Condition\n\n`ConditionInterface`: it represents an expression with a comparator operator.  \nThe interface is: `\\LogicTree\\Model\\ConditionInterface`.  \nThe concrete default class is: `\\LogicTree\\Model\\Condition`.  \nIt requires a mixed value to compared by an operator to a value from the data source.\n\nThe operator is a `string` which is the code of the wanted operator.\nThe first value is a `string` which is the identifier of the value from the `DataSource` object.  \nThe value to compare must be a `mixed` value.\n\n#### Combine\n\n`Combine`: it represents an expression with a logical operator.  \nThe interface is: `\\LogicTree\\Model\\CombineInterface`.\nThe concrete default class is: `\\LogicTree\\Model\\Combine`.  \nIt requires at least two conditions and/or combines to commute with a logical operator.\n\nThe operator is a `string` which is the code of the wanted operator.  \nThe conditions/combine must be an instance of `ConditionInterface`.    \n\nYou can add a `NodeInterface` to the `CombineInterface` with the following methods:\n\n- `addChild(NodeInterface $node)` : add a new node child to the parent node object. \n- `setChildren(array $nodes)` : replace the children by the new ones.\n\nIf you want to invert the result of the combination of conditions, you can specify to the `CombineInterface`\nobject that the result should actually be inverted:\n\n- `setIsInvert(bool $isInvert)`.\n\n### Execute the Combine Conditions\n\nIn order to execute the combined conditions, you must use the `\\LogicTree\\Service\\ConditionManager` class.  \nThis service allows to perform many actions on your `NodeInterface` object.  \n*Actually only the `execute` method is available from the public API.*  \nAnyway, the `execute` method allows you to determine the final result of your combination of conditions.\n\n### Export the Combine Conditions to Format\n\n*No documentation available yet!* \n\n## Examples\n\nExamples of code usage cases are available in this [file](./examples/index.php).\n\n## Running the tests\n\nThe tests are placed in the `tests` directory, at the root of the library.  \n\n*No tests available yet!*\n\n## Contributing\n\nAny help is welcome, feel free to raise issues or to open pull requests. Further details in the [CONTRIBUTING.md](.github/CONTRIBUTING.md).\n\n### TODO List\n\nImplement the following comparator operators:\n\n      - array(\"from\" =\u003e $fromValue, \"to\" =\u003e $toValue)\n      - array(\"like\" =\u003e $likeValue)\n      - array(\"moreq\" =\u003e $moreOrEqualValue)\n      - array(\"finset\" =\u003e $valueInSet)\n      - array(\"seq\" =\u003e $stringValue)\n      - array(\"sneq\" =\u003e $stringValue)\n\nToString methods, in order to render the combine conditions in full text / different formats.  \nHave to: AdapterPool (toFormat) - ConverterPool (getSymbol, getExpression, getFullExpression).  \nPossibility to add custom type node and process them in the services.  \nStudy Namespace and Lib Name: [trends](https://trends.google.fr/trends/explore?q=filter%20criteria,group%20conditions,complex%20conditions)\n\nTodo Builders (setter... getter... then reset)  \n-\u003e possibility of immutable implementation (do not break it!)\n\nAdd tests and examples: from scratch, with and without new operators, and execution (with expected results).   \n\n## Authors\n\n* **Thomas Klein** - *Initial work* - [It's me!](https://github.com/thomas-kl1)\n\nSee also the list of [contributors](https://github.com/thomas-kl1/php-combine-conditions/contributors) who participated in this project.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomas-kl1%2Fphp-combine-conditions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomas-kl1%2Fphp-combine-conditions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomas-kl1%2Fphp-combine-conditions/lists"}