jeudi 2 février 2017

A search class. Pattern advice request

friends. Currently, I'm developing s complex search. I have next classes:

App\Search\Search
App\Search\OrderBy\Relevance
App\Search\OrderBy\Priority
App\Search\OrderBy\WordMatch

App\Search\Search is the main search class. All classes inside \OrderBy are taking query builder object by reference and applying some logics to it.

Here are some code samples:

//App\Search\Search
Class Search {

protected $query;

    public function __construct($phrase)
    {
        $this->phrase = $phrase;
        $this->query = App\Airport::newQuery();
    }

    public function orderByRelevance()
    {
        OrderBy\Relevance::apply($this->query);
        return $this;
    }

    public function orderByPriority()
    {
        OrderBy\Priority::apply($this->query);
        return $this;
    }

    public function orderByWordMatch()
    {
        OrderBy\WordMatch::apply($this->query);
        return $this;
    }
}

The main idea is to conditionally apply multiple ordering to a query, like this:

$search = new Search('Berlin');
$search->orderByRelevance()->orderByPriority()->get();

So code works just fine, but I feel some code smell in it, especially in declaring every order method manually. Is there is any pattern or trick that may improve my Search class?

Aucun commentaire:

Enregistrer un commentaire