From cdbdeba549f9ba4a32e3e228629b7d5b7996468b Mon Sep 17 00:00:00 2001 From: Eric Rosas Date: Mon, 19 Feb 2024 12:41:06 -0600 Subject: [PATCH 1/6] Preliminary proof-of-concept for `whereContainsIn` EPO-8705 Preliminary proof-of-concept for `whereContainsIn` --- src/fields/CantoDamAsset.php | 17 +++++++++++++ src/gql/resolvers/CantoDamAssetResolver.php | 12 +++++++++ src/lib/laravel/Collection.php | 28 +++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/fields/CantoDamAsset.php b/src/fields/CantoDamAsset.php index 36a2f1f..43ff9bc 100644 --- a/src/fields/CantoDamAsset.php +++ b/src/fields/CantoDamAsset.php @@ -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).', 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 c3de805..e698dfa 100644 --- a/src/lib/laravel/Collection.php +++ b/src/lib/laravel/Collection.php @@ -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) { + // 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!"); + } else { + Craft::info("Logging: $item", "lofi!"); + } + + if (str_contains($item, $value)) { + return true; + } + } + + return false; + }); + } /** * Filter items by the given key value pair. * From 85b161590a2403bfe3683f3017aff6e5d28b1a36 Mon Sep 17 00:00:00 2001 From: Eric Rosas Date: Thu, 22 Feb 2024 14:00:27 -0600 Subject: [PATCH 2/6] Removed debug log line causing error --- src/lib/laravel/Collection.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/lib/laravel/Collection.php b/src/lib/laravel/Collection.php index e698dfa..23ab85b 100644 --- a/src/lib/laravel/Collection.php +++ b/src/lib/laravel/Collection.php @@ -11,7 +11,6 @@ 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) { @@ -21,9 +20,6 @@ public function whereContainsIn($keys, $value, $strict = false) if (is_array($item)) { $item = implode(', ', $item); - Craft::info("Logging: $list", "lofi!"); - } else { - Craft::info("Logging: $item", "lofi!"); } if (str_contains($item, $value)) { From 3366d56fba424e4b3285d504c4f6ca571ea52c2c Mon Sep 17 00:00:00 2001 From: Eric Rosas Date: Thu, 22 Feb 2024 14:48:44 -0600 Subject: [PATCH 3/6] Fixed code smell related to unusued var --- src/lib/laravel/Collection.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib/laravel/Collection.php b/src/lib/laravel/Collection.php index 23ab85b..a745a2e 100644 --- a/src/lib/laravel/Collection.php +++ b/src/lib/laravel/Collection.php @@ -8,12 +8,11 @@ class Collection extends LaravelCollection { - - public function whereContainsIn($keys, $value, $strict = false) + public function whereContainsIn($keys, $value) { $keys = $this->getArrayableItems($keys); - return $this->filter(function ($item) use ($keys, $value, $strict) { + return $this->filter(function ($item) use ($keys, $value) { // Handle the case where the data is an array of items for ($i = 0; $i < count($keys); $i++) { $item = data_get($item, $keys[$i]); From 726bec8ccd40da2650dcb44d11255ce498d282a4 Mon Sep 17 00:00:00 2001 From: Eric Rosas Date: Mon, 26 Feb 2024 14:26:13 -0600 Subject: [PATCH 4/6] Got most use cases functional --- src/lib/laravel/Collection.php | 43 +++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/lib/laravel/Collection.php b/src/lib/laravel/Collection.php index a745a2e..ae04ec6 100644 --- a/src/lib/laravel/Collection.php +++ b/src/lib/laravel/Collection.php @@ -11,24 +11,45 @@ class Collection extends LaravelCollection public function whereContainsIn($keys, $value) { $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); + } - return $this->filter(function ($item) use ($keys, $value) { - // 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); + $found = false; + foreach($values as $value) { + if (str_contains($data, $value)) { + $found = true; + break; + } } - if (str_contains($item, $value)) { - return true; + return $found; + }); + + if($recs->count() > 0) { + if($matched_records == []) { + $count = $recs->count(); + $type_rec = gettype($recs); + $matched_records = $recs; + } else { + $count = $recs->count(); + $matched_records->merge($recs); + $count2 = $matched_records->count(); } } - return false; - }); + } + + return $matched_records->unique(); } + /** * Filter items by the given key value pair. * From 3d9c6d4e2b5eac104381154183509721d801b422 Mon Sep 17 00:00:00 2001 From: Eric Rosas Date: Mon, 26 Feb 2024 16:26:28 -0600 Subject: [PATCH 5/6] Enabled fuzzy search on partial string match --- src/lib/laravel/Collection.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/laravel/Collection.php b/src/lib/laravel/Collection.php index ae04ec6..c142b24 100644 --- a/src/lib/laravel/Collection.php +++ b/src/lib/laravel/Collection.php @@ -24,7 +24,7 @@ public function whereContainsIn($keys, $value) $found = false; foreach($values as $value) { - if (str_contains($data, $value)) { + if (str_contains(strtolower($data), strtolower($value))) { $found = true; break; } @@ -46,8 +46,14 @@ public function whereContainsIn($keys, $value) } } - - return $matched_records->unique(); + + if($matched_records == []) { + // To-do: Come up with a more elegant solution, rather than calling only() + return $this->only([""]); + } else { + return $matched_records->unique(); + } + } /** From 84f963ed0c74fa9c628ff911e61b10173b96d7f9 Mon Sep 17 00:00:00 2001 From: Eric Rosas Date: Wed, 28 Feb 2024 16:19:54 -0600 Subject: [PATCH 6/6] Fixed issue w/merge() not modifying arr in place Fixed issue w/merge() not modifying arr in place & instead returning --- src/lib/laravel/Collection.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/lib/laravel/Collection.php b/src/lib/laravel/Collection.php index 0e7f094..9bffb77 100644 --- a/src/lib/laravel/Collection.php +++ b/src/lib/laravel/Collection.php @@ -43,14 +43,10 @@ public function whereContainsIn($keys, $value): Collection }); if($recs->count() > 0) { - if($matched_records == []) { - $count = $recs->count(); - $type_rec = gettype($recs); + if ($matched_records == []) { $matched_records = $recs; } else { - $count = $recs->count(); - $matched_records->merge($recs); - $count2 = $matched_records->count(); + $matched_records = $matched_records->concat($recs); } } @@ -59,10 +55,9 @@ public function whereContainsIn($keys, $value): Collection if($matched_records == []) { // To-do: Come up with a more elegant solution, rather than calling only() return $this->only([""]); - } else { - return $matched_records->unique(); } - + + return $matched_records->unique("id"); } /**