PHPackages                             krzysztof-gzocha/searcher - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. krzysztof-gzocha/searcher

ActiveLibrary[Database &amp; ORM](/categories/database)

krzysztof-gzocha/searcher
=========================

Searcher is a framework-agnostic search query builder. Search queries are written using Criterias and can be run against MySQL, MongoDB, ElasticSearch or even files.

5.0.0(9y ago)11861.6k81MITPHPPHP ^7CI failing

Since Feb 4Pushed 7y ago10 watchersCompare

[ Source](https://github.com/krzysztof-gzocha/searcher)[ Packagist](https://packagist.org/packages/krzysztof-gzocha/searcher)[ Docs](http://searcher.rtfd.io)[ RSS](/packages/krzysztof-gzocha-searcher/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (34)Used By (1)

[![](https://camo.githubusercontent.com/03659f3fcddeaec49aa2f494c1d4aff0ec9cbd36/687474703a2f2f7777772e636c6b65722e636f6d2f636c6970617274732f612f632f612f382f31313934393936353638313938333637303238396b63616368656772696e642e7376672e7468756d622e706e67)](https://camo.githubusercontent.com/03659f3fcddeaec49aa2f494c1d4aff0ec9cbd36/687474703a2f2f7777772e636c6b65722e636f6d2f636c6970617274732f612f632f612f382f31313934393936353638313938333637303238396b63616368656772696e642e7376672e7468756d622e706e67)

Searcher [![Build Status](https://camo.githubusercontent.com/d5b42fe1306e86bdd445ac6a14d956eddf6cef5bff2c68ee676be42e0fe4bf03/68747470733a2f2f7472617669732d63692e6f72672f6b727a79737a746f662d677a6f6368612f73656172636865722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/krzysztof-gzocha/searcher) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/97ba482340481576497737e308a7104ac54c4da9cd69641e2c3cbc11bd7610fd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b727a79737a746f662d677a6f6368612f73656172636865722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/krzysztof-gzocha/searcher/?branch=master) [![Code Coverage](https://camo.githubusercontent.com/607535e94006527673805df9e771cf6945fc375fec9efe2cd101aaa74d79df72/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b727a79737a746f662d677a6f6368612f73656172636865722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/krzysztof-gzocha/searcher/?branch=master) [![Packagist](https://camo.githubusercontent.com/b59c4aa363cc8f8db04c9eed98031fdc783e4034badcbf17e780499172e8aeec/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b727a79737a746f662d677a6f6368612f73656172636865722e7376673f7374796c653d706c6173746963)](https://packagist.org/packages/krzysztof-gzocha/searcher) [![SensioLabsInsight](https://camo.githubusercontent.com/4c0dffc9375e24daee54bfa1df8c2c0f2161c88f6eb51adb5c121324d38c5f57/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f32623764663039382d343663362d343365312d616339342d6230656162336434363430312f6d696e692e706e67)](https://insight.sensiolabs.com/projects/2b7df098-46c6-43e1-ac94-b0eab3d46401)
====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#searcher-----)

### What is that?

[](#what-is-that)

*Searcher* is a framework-agnostic search query builder. Search queries are written using criterias and can be run against MySQL, MongoDB, ElasticSearch, files or whatever else you like. **Latest version is supporting only PHP 7**. **Now tested also with [Humbug](https://github.com/padraic/humbug)**

### See [this presentation](https://krzysztof-gzocha.github.io/searcher/) to understand better

[](#see-this-presentation-to-understand-better)

### Why?

[](#why)

Have you ever seen code responsible for searching for something based on many different criteria? It can become quite a mess! Imagine you have a form with 20 fields and all of them have some impact on searching conditions. It's not a great idea to pass a whole form to some service at let it parse everything in one place. Thanks to this library you can split the responsibility of building query criteria to several smaller classes. One class per filter. One `CriteriaBuilder` per `Criteria`. This way, inside `CriteriaBuilder` you care only about one `Criteria`, which makes it a lot more readable and maintanable. You can later use exactly the same `Criteria` for different searches, with different `CriteriaBuilder` and even different `SearchingContext` which can use even different databases. You can even use searcher to find **files** on your system thanks to `FinderSearchingContext`.

### Full documentation

[](#full-documentation)

Full documentation can be found at [http://searcher.rtfd.io/](http://searcher.readthedocs.io/en/stable/introduction.html)

### Installation

[](#installation)

You can install the library via composer by typing in terminal:

```
$ composer require krzysztof-gzocha/searcher
```

### Integration

[](#integration)

Integration with Symfony is done in **[SearcherBundle](https://github.com/krzysztof-gzocha/searcher-bundle)**

### Idea

[](#idea)

- `CriteriaBuilder` - will build new *conditions* for single `Criteria`,
- `Criteria` - model that will be passed to `CriteriaBuilder`. You just need to hydrate it somehow, so it will be useful. Criteria can hold multiple fields inside and all (or some) of them might be used inside `CriteriaBuilder`,
- `SearchingContext` - context of single search. This service should know how to fetch results from constructed query and it holds something called `QueryBuilder`, but it can be anything that works for you - any service. This is an abstraction layer between search and database. There are different contexts for Doctrine's ORM, ODM, Elastica, *Files* and so on. If there is no context for you you can implement one - it's shouldn't be hard,
- `Searcher` - holds collection of `CriteriaBuilder` and will pass `Criteria` to appropriate `CriteriaBuilder`.

### Example

[](#example)

Let's say we want to search for **people** whose **age** is in some filtered range. In this example we will use Doctrine's QueryBuilder, so we will use `QueryBuilderSearchingContext` and will specify in our `CriteriaBuidler` that it should interact only with `Doctrine\ORM\QueryBuilder`, but remember that we do **not** have to use only Doctrine.

#### 1. Criteria

[](#1-criteria)

First of all we would need to create `AgeRangeCriteria` - the class that will holds values of minimal and maximal age. There are already implemented default `Criteria` in [here](https://github.com/krzysztof-gzocha/searcher/tree/master/src/KGzocha/Searcher/Criteria).

```
class AgeRangeCriteria implements CriteriaInterface
{
    private $minimalAge;
    private $maximalAge;

    /**
    * Only required method.
    * If will return true, then it will be passed to some of the CriteriaBuilder(s)
    */
    public function shouldBeApplied(): bool
    {
        return null !== $this->minimalAge && null !== $this->maximalAge;
    }

    // getters, setters, whatever
}
```

#### 2. CriteriaBuilder

[](#2-criteriabuilder)

In second step we would like to specify conditions that should be imposed for this model. That's why we would need to create `AgeRangeCriteriaBuilder`

```
class AgeRangeCriteriaBuilder implements CriteriaBuilderInterface
{
    public function buildCriteria(
        CriteriaInterface $criteria,
        SearchingContextInterface $searchingContext
    ) {
        $searchingContext
            ->getQueryBuilder()
            ->andWhere('e.age >= :minimalAge')
            ->andWhere('e.age setItemsPerPage(250);    getItemsPerPage() = 50
