Download the PHP package koba/filter-builder-core without Composer
On this page you can find all versions of the php package koba/filter-builder-core. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download koba/filter-builder-core
More information about koba/filter-builder-core
Files in koba/filter-builder-core
Package filter-builder-core
Short Description A library that allows you to apply filters to objects which can be stored as arrays.
License MIT
Informations about the package filter-builder-core
Filter Builder Core
A flexible, type-safe PHP library for creating and applying filters to objects. Filter Builder Core allows you to define filterable fields, parse filter criteria from arrays (typically JSON/API requests), validate them, and execute complex filtering logic against your domain objects.
Features
- Type-Safe Filtering: Built with PHP generics
- Complex Logic Groups: Support for nested AND/OR operations
- Multiple Data Types: Strings, numbers, and dropdown values
- Relation Support: Filter based on related objects with nested configurations
- Comprehensive Validation: Type checking, operation validation, and custom validation hooks
- Internationalization: Built-in English and Dutch translations with pluggable translation system
- Extensible Architecture: Strategy pattern allows custom implementations
Installation
Install via Composer:
Quick Start
Core Concepts
Configuration
The Configuration class is the central point for defining what can be filtered. It manages rules and relations.
You can optionally provide a translation object and custom bound filter factory:
Constraint Types
Filter Builder Core supports three constraint types:
-
ConstraintType::STRING: For string values- Supported operations:
EQUALS,STARTS_WITH,ONE_OF
- Supported operations:
-
ConstraintType::NUMBER: For numeric values (int/float)- Supported operations:
EQUALS,GREATER_THAN,LESS_THAN,ONE_OF
- Supported operations:
ConstraintType::DROPDOWN: For predefined values- Supported operations:
EQUALS,ONE_OF
- Supported operations:
Operations
Available filter operations:
Operation::EQUALS: Exact matchOperation::STARTS_WITH: String starts with value (case-sensitive)Operation::LESS_THAN: Numeric less than comparisonOperation::GREATER_THAN: Numeric greater than comparisonOperation::ONE_OF: Value is in array of options
Adding Rule Entries
Define filterable fields using addRuleEntry():
With custom validation:
Adding Relation Entries
Filter based on related objects:
Filter Array Format
Filters are defined as arrays with a specific structure:
Group Filter
Rule Filter
For ONE_OF operations, provide an array:
Relation Filter
Complex Example
This translates to: (category = 'Electronics' OR category = 'Books') AND price < 100 AND inStock = true
Validation and Error Handling
The library provides comprehensive validation with detailed error messages.
Validation Exception
When filter validation fails, a ValidationException is thrown:
Common Validation Errors
- Missing or invalid field names
- Unsupported operations for a field type
- Invalid value types
- Custom validation failures
- Invalid filter structure
Internationalization
The library supports multiple languages for error messages.
Using Built-in Translations
Creating Custom Translations
Implement the Translationinterface:
Advanced Usage
Working with Collections
Filter multiple objects:
Custom Bound Filter Factory
For advanced use cases, you can create a custom bound filter factory:
Strategy Pattern
The library uses the strategy pattern for extensibility. Currently, ObjectStrategy is provided for filtering objects, but you can implement custom strategies:
Development
Static Analysis
The library uses PHPStan for static analysis:
Configuration is in phpstan.neon.
Support
For issues, questions, or suggestions, please open an issue on the GitHub repository.