Skip to content

Commit

Permalink
Merge pull request #4 from lsst-epo/enhanced_gql_search
Browse files Browse the repository at this point in the history
Preliminary proof-of-concept for `whereContainsIn`
  • Loading branch information
ericdrosas87 authored Mar 11, 2024
2 parents 7a9148b + 84f963e commit aa65de0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/fields/CantoDamAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ protected function getGqlArguments(): array
],
]),
],
'whereContainsIn' => [
'name' => 'whereContainsIn',
'description' => 'Look across the given key-values and return fuzzy match on a single search term',
'type' => new InputObjectType([
'name' => 'WhereContainsInFilterInput',
'fields' => [
'keys' => [
'type' => Type::listOf(Type::string()),
'description' => 'The keys to search on, you can use the `field.subField` syntax for nested fields'
],
'value' => [
'type' => Type::string(),
'description' => 'The value that should be fuzzy matched in the key-values'
],
]
]),
],
'where' => [
'name' => 'where',
'description' => 'Get all items by the given key value pair, using the optional operator for comparison. (See https://laravel.com/docs/10.x/collections#method-where).',
Expand Down
12 changes: 12 additions & 0 deletions src/gql/resolvers/CantoDamAssetResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class CantoDamAssetResolver extends Resolver
{
// List of arguments in the order they should be processed, along with the argument transform method
protected static array $argsList = [
[
'args' => ['whereContainsIn'],
'method' => 'whereContainsInArgs',
],
[
'args' => ['where'],
'method' => 'whereArgs',
Expand Down Expand Up @@ -97,6 +101,14 @@ protected static function sortArgs(Collection $collection, array $arguments, str
return $collection->$arg($resolvedArg);
}

protected static function whereContainsInArgs(Collection $collection, array $arguments, string $arg): Collection
{
return $collection->$arg(
$arguments[$arg]['keys'] ?? null,
$arguments[$arg]['value'] ?? null
);
}

protected static function whereArrayArgs(Collection $collection, array $arguments, string $arg): Collection
{
return $collection->$arg(
Expand Down
52 changes: 52 additions & 0 deletions src/lib/laravel/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,64 @@

namespace lsst\cantodamassets\lib\laravel;

use Craft;
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection as LaravelCollection;

class Collection extends LaravelCollection
{

/**
* Fuzzy search across multiple keys
*
* @param $keys
* @param $value
* @return Collection
*/
public function whereContainsIn($keys, $value): Collection
{
$keys = $this->getArrayableItems($keys);
$value2 = preg_split('/\s+/', $value);
$values = array_change_key_case($value2, CASE_LOWER);
$matched_records = [];

foreach($keys as $key) {
$recs = $this->filter(function ($item) use ($key, $values) {
$data = data_get($item, $key);
if (is_array($data)) {
$data = implode(', ', $data);
}

$found = false;
foreach($values as $value) {
if (str_contains(strtolower($data), strtolower($value))) {
$found = true;
break;
}
}

return $found;
});

if($recs->count() > 0) {
if ($matched_records == []) {
$matched_records = $recs;
} else {
$matched_records = $matched_records->concat($recs);
}
}

}

if($matched_records == []) {
// To-do: Come up with a more elegant solution, rather than calling only()
return $this->only([""]);
}

return $matched_records->unique("id");
}

/**
* Filter items by the given key value pair.
*
Expand Down

0 comments on commit aa65de0

Please sign in to comment.