Download the PHP package nmarniesse/pomm-filter without Composer
On this page you can find all versions of the php package nmarniesse/pomm-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nmarniesse/pomm-filter
More information about nmarniesse/pomm-filter
Files in nmarniesse/pomm-filter
Package pomm-filter
Short Description Simple filter implementation for pomm project
License MIT
Informations about the package pomm-filter
Simple filter implementation for Pomm Project
Asking a collection of resources is often given with filter needs. But it's not always obvious to handle filters and build the query accordingly: multiple filters on different tables, multiple values for a filter, null values, dates, ...
This library provides a simple implementation to build query's condition from an array of filters.
Requirements and installation
- php >=5.4
Usage
The library helps to create an instance of PommProject\Foundation\Where
that you could use in every pomm query
(see here
for further explanation).
To explain what we can do with this library, we can take a practical case: we want to filter on active products with color 'blue' or 'yellow', in category 'accessory', with price between 50 and 100, and which have one tag.
The filter array representation is:
With an HTTP query similar to ?filter[is_active]=1&filter[color][]=blue&filter[color][]=yellow&filter[category]=accessory&filter[price_from]=50&filter[price_from]=50&filter[price_to]=100&filter[tag]=_not_null_
you can have he same array in php with $array_filters = $_GET['filter'];
.
You have your array filters, now let build the query:
Important note
Even if the generated Where condition protects the query against SQL injection, please note you must clean and validate the data coming from users, according to your business rules.
Documentation
FilterCollection
By default the FilterCollection does not contain any filter.
The method getWhere($filters)
convert any associative array into Where
instance
When you do a getWhere(['key1' => 'val1'])
, it assumes that the key1 field exists in your query and
build a simple condition query key1 = $*
with parameter 'val1'
.
If you want to specify a table alias in the condition query, or not use the =
operator, you have to
add the filter manually using the addFilter
method.
Examples:
Filter types
This library provides several filter types to help you to create your own filter collection.
BasicFilter
As its name indicates, this class is useful to create simple filter.
However you can specify the operator you want to use (default is =
) in order to
customize the behavior of your filter.
DateTimeFilter
This filter allow you to use date values.
BooleanFilter
This filter is used to handle boolean fields.
HstoreFilter
This filter is used to handle hstore fields.
Given we have a hstore field full_address which contains keys like street, city, postal code, country, etc...
LtreeFilter
This filter is used to handle ltree fields. As ltree is commonly used to handle tree views, you can filter on a value and all its descendants using this filter. If you don't want to filter on the descendants the BasicFilter is enough.
RangeFilter
This filter is used to handle range fields. The value could be a single value or a range of values identified by a NMarniesse\PommFilter\ValueType\RangeValue object. In both cases, the filter tests the value is included into the range field.