Skip to content

Conversation

lorado
Copy link
Contributor

@lorado lorado commented Feb 2, 2019

Hi. Here is me again... 🤣

in my project I wanted to define filterable fields type, but there was no option to do it. So I improved parsing of given values:

  • There is new TypedFilterInterface, with method getType(), that should return wished input type. This type will be used, if instance of class that implements TypedFilterInterface defined in YourDefinition::getFilterables()
  • there is still ability to define input type via array. So with array devinition it is possible to define type and use a closure (see examples below)
  • all possible definitions are tested with unit tests

So here are all possible definitions of filterables:

// NOTE: ComplexFilter implements TypedFilterInterface! 
public function getFilterable() {
	return [
		// Simple equality check (or "in" if value is an array). Type is Type::json()
		'id'       => new EqualsOrContainsFilter(),
		
		// Customized filter. Type is Type::json()
		"nameLike" => function($builder, $value) {
			return $builder->whereRaw('name like ?', $value);
		},
		
		// type is Type::string()
		"anotherFilter" => [
			"type" => Type::string(),
			"resolver" => function($builder, $value) {
				return $builder->whereRaw('anotherFilter like ?', $value);				
			}
		],
		
		// type is what is returned from `ComplexFilter::getType()`.
		// This is the preffered way to define filters, as it keeps definitions code clean
		"complexFilter" => new ComplexFilter(),
		
		// type in array will be overriden by what is returned from `ComplexFilter::getType()`.
		// this kind of difinition is not clear, but is implemented for backward compatibilities. Please don't use it
		"complexFilter2" => [
			"type" => Type::int(),
			"resolver" => new ComplexFilter()
		],
	];
}

I also updated README and listed there all possible usages (as above).

There is NO breaking changes. Old definitions are still working, with default Type::json() type.

As this is already 4th pull request, after merging some of PRs may require manually merge from me. So... let me know...

@drasill drasill merged commit eda0cd6 into studio-net:master Feb 8, 2019
@drasill
Copy link
Contributor

drasill commented Feb 8, 2019

Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants