Skip to content

Commit

Permalink
Preliminary proof-of-concept for whereContainsIn
Browse files Browse the repository at this point in the history
EPO-8705 Preliminary proof-of-concept for `whereContainsIn`
  • Loading branch information
ericdrosas87 committed Feb 19, 2024
1 parent 6b95321 commit cdbdeba
Show file tree
Hide file tree
Showing 3 changed files with 57 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 @@ -329,6 +329,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
28 changes: 28 additions & 0 deletions src/lib/laravel/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,38 @@

namespace lsst\cantodamassets\lib\laravel;

use Craft;
use Illuminate\Support\Collection as LaravelCollection;

class Collection extends LaravelCollection
{


public function whereContainsIn($keys, $value, $strict = false)
{
Craft::info("Got inside of whereContainsIn()!", "lofi!");
$keys = $this->getArrayableItems($keys);

return $this->filter(function ($item) use ($keys, $value, $strict) {

Check failure on line 17 in src/lib/laravel/Collection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Anonymous function has an unused use $strict.
// Handle the case where the data is an array of items
for ($i = 0; $i < count($keys); $i++) {
$item = data_get($item, $keys[$i]);

if (is_array($item)) {
$item = implode(', ', $item);
Craft::info("Logging: $list", "lofi!");

Check failure on line 24 in src/lib/laravel/Collection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Undefined variable: $list
} else {
Craft::info("Logging: $item", "lofi!");
}

if (str_contains($item, $value)) {
return true;
}
}

return false;
});
}
/**
* Filter items by the given key value pair.
*
Expand Down

0 comments on commit cdbdeba

Please sign in to comment.