diff --git a/src/fields/CantoDamAsset.php b/src/fields/CantoDamAsset.php index 7f5a9e4..0b6b112 100644 --- a/src/fields/CantoDamAsset.php +++ b/src/fields/CantoDamAsset.php @@ -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).', diff --git a/src/gql/resolvers/CantoDamAssetResolver.php b/src/gql/resolvers/CantoDamAssetResolver.php index d441c7a..354693c 100644 --- a/src/gql/resolvers/CantoDamAssetResolver.php +++ b/src/gql/resolvers/CantoDamAssetResolver.php @@ -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', @@ -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( diff --git a/src/lib/laravel/Collection.php b/src/lib/laravel/Collection.php index 14ab8b4..9bffb77 100644 --- a/src/lib/laravel/Collection.php +++ b/src/lib/laravel/Collection.php @@ -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. *