1. Go to this page and download the library: Download koba/filter-builder-core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
koba / filter-builder-core example snippets
use Koba\FilterBuilder\Core\Configuration\Configuration;
use Koba\FilterBuilder\Core\Enums\ConstraintType;
use Koba\FilterBuilder\Core\Enums\Operation;
use Koba\FilterBuilder\Core\ObjectStrategy\ObjectStrategy;
// Define your class
class Product
{
public function __construct(
public string $name,
public float $price,
public string $category,
) {}
}
// Create a product instance
$product = new Product("Laptop", 999.99, "Electronics");
// Define filter criteria (typically from API request)
$filterArray = [
'type' => 'group',
'operation' => 'and',
'children' => [
['name' => 'name', 'operation' => 'starts_with', 'value' => 'Lap'],
['name' => 'price', 'operation' => 'less_than', 'value' => 1000],
]
];
// Configure filterable fields
$strategy = new ObjectStrategy(Product::class);
$configuration = (new Configuration($strategy))
->addRuleEntry(
'name',
ConstraintType::STRING,
[Operation::EQUALS, Operation::STARTS_WITH],
fn($factory) => $factory->makeRule(fn($obj) => $obj->name)
)
->addRuleEntry(
'price',
ConstraintType::NUMBER,
[Operation::LESS_THAN, Operation::GREATER_THAN, Operation::EQUALS],
fn($factory) => $factory->makeRule(fn($obj) => $obj->price)
)
->addRuleEntry(
'category',
ConstraintType::DROPDOWN,
[Operation::EQUALS, Operation::ONE_OF],
fn($factory) => $factory->makeRule(fn($obj) => $obj->category)
);
// Get the filter and check if object adheres
$filter = $configuration->getFilter($filterArray);
if ($filter->adheres($product)) {
echo "Product matches the filter!";
}
$configuration = new Configuration($strategy);
use Koba\FilterBuilder\Core\Translation\Dutch;
$configuration = new Configuration(
$strategy,
new Dutch(), // Translation
null // Custom BoundFilterFactory (optional)
);
$configuration->addRuleEntry(
'fieldName', // Field identifier
ConstraintType::STRING, // Data type
[Operation::EQUALS], // Supported operations
fn($factory) => $factory->makeRule(
fn($obj) => $obj->fieldName // Property accessor
)
);
use Koba\FilterBuilder\Core\Exceptions\ValidationException;
try {
$filter = $configuration->getFilter($invalidFilterArray);
} catch (ValidationException $e) {
// Get all error messages
$errors = $e->getMessages();
foreach ($errors as $error) {
echo $error . "\n";
}
}
use Koba\FilterBuilder\Core\Translation\English;
use Koba\FilterBuilder\Core\Translation\Dutch;
// English (default)
$configuration = new Configuration($strategy, new English());
// Dutch
$configuration = new Configuration($strategy, new Dutch());
use Koba\FilterBuilder\Core\Contracts\Translationinterface;
use Koba\FilterBuilder\Core\Enums\ErrorMessage;
use Koba\FilterBuilder\Core\Enums\ErrorFieldMessage;
class Spanish implements Translationinterface
{
public function translateError(ErrorMessage $message): string
{
return match($message) {
ErrorMessage::INVALID_CONFIGURATION => 'Configuración inválida',
// ... other translations
};
}
public function translateErrorWithField(
ErrorFieldMessage $message,
string $field
): string {
return match($message) {
ErrorFieldMessage::MISSING_CONFIGURATION_ENTRY =>
"Falta la entrada de configuración: $field",
// ... other translations
};
}
}
$configuration = new Configuration($strategy, new Spanish());
$products = [
new Product("Laptop", 999.99, "Electronics"),
new Product("Book", 19.99, "Books"),
new Product("Phone", 599.99, "Electronics"),
];
$filter = $configuration->getFilter($filterArray);
$filtered = array_filter($products, fn($product) => $filter->adheres($product));
use Koba\FilterBuilder\Core\Contracts\BoundFilterFactoryInterface;
use Koba\FilterBuilder\Core\Factories\BoundFilterFactory;
class CustomBoundFilterFactory extends BoundFilterFactory
{
// Override methods as needed
}
$configuration = new Configuration(
$strategy,
null,
new CustomBoundFilterFactory($configuration)
);
use Koba\FilterBuilder\Core\Contracts\StrategyInterface;
use Koba\FilterBuilder\Core\Contracts\BoundFilterInterface;
class DatabaseQueryStrategy implements StrategyInterface
{
// Implement strategy for building database queries
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.