Refine will be launching soon 🎉 Sign up for the early access list to stay up to date.
Refine for Laravel
A package by Hammerstone

Extending Standard Conditions

Hammerstone Refine has not launched yet. Sign up for the early access list to stay up to date!

No matter how configurable the standard conditions may be, there are still going to be times when you need to do something custom for your application.

We've put a great amount of effort into giving you as many places to modify the behavior of the standard conditions.

Psst... You can also create your own custom condition entirely from scratch.

The Boot Method

Every condition has a boot method that can be used to do some kind of post-constructor initializing. We often use that internally to add validation rules and ensure that the condition is configured correctly. Below is the boot method from the Date Condition:

1protected function boot()
2{
3 // Add validation rules.
4 $this->addRules([
5 'date1' => [
6 'nullable',
7 'date'
8 ],
9 'date2' => [
10 'nullable',
11 'date'
12 ],
13 ]);
14 
15 // Ensure the developer has set timezones up correctly.
16 $this->addEnsurance([$this, 'ensureTimezones']);
17}

You may use it to do any number of things, like hide clauses:

1protected function boot()
2{
3 parent::boot();
4 
5 $this->withoutClauses([
6 self::CLAUSE_LESS_THAN,
7 self::CLAUSE_GREATER_THAN
8 ]);
9}
Code highlighting powered by torchlight.dev, a Hammerstone product.

or add meta:

1protected function boot()
2{
3 parent::boot();
4 
5 $this->withMeta([
6 'me' => Auth::id()
7 ]);
8}

Booting Traits

Every trait that is added to a condition is given the opportunity to boot itself. If you have a set of common functionality that you've factored out to a trait called RequiresConfirmation, then you can have a method called bootRequiresConfirmation and do any setup work there:

1trait RequiresConfirmation
2{
3 // This will be automatically called while the
4 // condition is booting.
5 protected function bootRequiresConfirmation()
6 {
7 $this->addRules([
8 'confirmed' => [
9 'required',
10 'accepted'
11 ]
12 ]);
13 }
14}

You can take a look at the bootHasClauses method in the HasClauses trait to see some of this in action:

1protected function bootHasClauses()
2{
3 // ...
4 
5 $this->beforeValidate([$this, 'beforeValidationOfClause']);
6}

In this case, the HasClauses trait is hooking into the beforeValidate method to potentially add extra rules based on which clause the user has chosen. If the user chooses a clause that requires extra fields, then we can make those fields required right before we run validation.

Modifying the Query

If you'd like to modify how the user's input is being applied to the query, you may extend the applyCondition method and do anything that you need. Often times, however, you only want to modify the application of a particular clause. Each condition handles application differently, but many of them break out the application into several small methods.

For example, if you wanted to use MySQL's fulltext search instead of the simple LIKE matching, you could do so by extending the Text Condition and overriding the applyClauseContains method:

1class TextCondition extends BaseTextCondition
2{
3 protected function applyClauseContains($query, $value)
4 {
5 // Use fulltext search for the `contains` clause only.
6 $query->whereRaw("MATCH ($this->attribute) AGAINST (?)" , [$value]);
7 }
8}

You'll have to take a look at each of the individual conditions to see where the best place to hook in might be.

Custom Clauses

If you are adding custom clauses to a condition, you'll need to specify how those clauses get applied to the query. The base condition has a method called applyCustomClause that you can override to handle your custom clause application. By default this method just throws an exception alerting you that a custom clause is unhandled.

Let's take the fulltext example from above and rework it as a custom clause.

1class TextCondition extends BaseTextCondition
2{
3 const CLAUSE_SIMILAR = 'similar';
4 
5 protected function customClauses()
6 {
7 return [
8 Clause::make(self::CLAUSE_SIMILAR, 'Is Similar To')
9 ->rules([
10 'value' => ['required']
11 ])
12 ];
13 }
14}

We've added our custom clause and a validation rule, but now we need to handle the application itself.

1class TextCondition extends BaseTextCondition
2{
3 const CLAUSE_SIMILAR = 'similar';
4 
5 protected function customClauses()
6 {
7 return [
8 Clause::make(self::CLAUSE_SIMILAR, 'Is Similar To')
9 ->rules([
10 'value' => ['required']
11 ])
12 ];
13 }
14 
15 public function applyCustomClause($query, $clause, $input)
16 {
17 $query->whereRaw("MATCH ($this->attribute) AGAINST (?)" , [$value]);
18 }
19}
Code highlighting powered by torchlight.dev, a Hammerstone product.

Now your users are able to use fulltext search when they choose the "Is Similar To" clause.

The first parameter is the currently built query, the second is the clause that the user has chosen, and the third is an array of input.

In the $input array will always include a raw key that contains the user's input exactly as we received it, in case you need to do anything with it. In many cases, the condition that you're extending has done some kind of work cleaning and standardizing the input, so we'll pass that cleaned input to you as well under various keys.

Helpful Extension Methods

There are a number of methods designed to help you extend the standard conditions.

addEnsurance

Add a callback to ensure that the condition has been configured correctly. Check for options or properties being set, etc. From HasClauses:

1protected function bootHasClauses()
2{
3 // Add a callback to ensure that the clauses are
4 // all configured correctly.
5 $this->addEnsurance([$this, 'ensureClauses']);
6}

addRules

Add validation rules. Your rules can be callables if they depend configuration settings. For example, here is the rule that the HasClauses trait adds:

1protected function bootHasClauses()
2{
3 $this->addRules([
4 'clause' => [
5 'required',
6 // Lazily evaluate the clauses to give the developer time to
7 // exclude certain clauses. By passing a closure, this rule
8 // will be resolved only when it's time to validate.
9 function () {
10 return Rule::in(Arr::pluck($this->getClauses(), 'id'));
11 }
12 ]
13 ]);
14}

withMeta

Add arbitrary meta to a condition. Your meta value can be a callable for lazy evaluation.

1protected function bootHasClauses()
2{
3 // Lazily add the clauses to the meta object
4 // for the same reason as mentioned above.
5 $this->withMeta('clauses', [$this, 'getClauses']);
6}

beforeValidate

Add a callback to be run before any validation occurs. This callback will receive the user's input as its only argument.

modifyUserInput

This is a method in the base condition that you can extend to modify the user's input in any way you want. By default it just returns exactly what it was given.