Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary proof-of-concept for whereContainsIn #4

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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