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

Added whereAll filter condition argument #23

Open
wants to merge 1 commit into
base: develop-v4
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## 4.6.0 - 2024.10.29
### Added
* Added `whereAll` argument which accepts stringified JSON `whereIn` conditions as an array of strings and filters by all conditions

## 4.5.1 - 2024.9.24
### Fixed
* The `whereIn` and `whereNotIn` argument methods now are case-insensitive
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lsst-epo/canto-dam-assets",
"description": "This Craft CMS plugin adds a Field Type with GraphQL support for the Canto Digital Asset Management (DAM) web system",
"type": "craft-plugin",
"version": "4.5.1",
"version": "4.6.0",
"license": "MIT",
"require": {
"php": ">=8.0.2",
Expand Down
13 changes: 13 additions & 0 deletions src/fields/CantoDamAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,19 @@ protected function getGqlArguments(): array
'description' => 'Return items from the collection where the given key is not null. You can use the `field.subField` syntax for nested fields.',
'type' => Type::string(),
],
'whereAll' => [
'name' => 'whereAll',
'description' => 'A container field for multiple where args',
'type' => new InputObjectType([
'name' => 'WhereAllFilterInput',
'fields' => [
'filters' => [
'type' => Type::listOf(Type::string()),
'description' => 'A list of JSON strings representing whereIn conditions to filter by.'
]
]
])
],
'whereIn' => [
'name' => 'whereIn',
'description' => 'Filter items such that the value of the given key is in the array of values provided. (See https://laravel.com/docs/10.x/collections#method-wherein).',
Expand Down
27 changes: 24 additions & 3 deletions src/gql/resolvers/CantoDamAssetResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lsst\cantodamassets\gql\resolvers;

use Craft;
use craft\base\ElementInterface;
use craft\gql\base\Resolver;
use GraphQL\Type\Definition\ResolveInfo;
Expand All @@ -16,6 +17,10 @@
'args' => ['whereContainsIn'],
'method' => 'whereContainsInArgs',
],
[
'args' => ['whereAll'],
'method' => 'whereAllArgs',
],
[
'args' => ['where'],
'method' => 'whereArgs',
Expand Down Expand Up @@ -50,7 +55,8 @@
],
];

public static function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed
// public static function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed
public static function resolve(mixed $source, mixed $arguments, mixed $context, ResolveInfo $resolveInfo): mixed
{
/** @var ElementInterface $source */
$fieldName = $resolveInfo->fieldName;
Expand All @@ -64,7 +70,8 @@
return static::applyArguments($cantoFieldData->cantoAssetData, $arguments);
}

protected static function applyArguments(Collection $collection, array $arguments): Collection
//protected static function applyArguments(Collection $collection, array $arguments): Collection
protected static function applyArguments(Collection $collection, mixed $arguments): Collection
{
foreach (static::$argsList as $argList) {
foreach ($argList['args'] as $arg) {
Expand All @@ -86,12 +93,19 @@
);
}

protected static function whereAllArgs(Collection $collection, array $arguments, string $arg): Collection
{
return $collection->$arg(
$arguments[$arg]
);
}

protected static function whereArgs(Collection $collection, array $arguments, string $arg): Collection
{
return $collection->$arg(
$arguments[$arg]['key'] ?? null,
$arguments[$arg]['operator'] ?? null,
$arguments[$arg]['value'] ?? null
strtolower($arguments[$arg]['value']) ?? null

Check failure on line 108 in src/gql/resolvers/CantoDamAssetResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan

Expression on left side of ?? is not nullable.
);
}

Expand Down Expand Up @@ -126,4 +140,11 @@
{
return new Collection([$collection->$arg(null)]);
}

protected static function nestedLowercase($value) {
if (is_array($value)) {
return array_map('nestedLowercase', $value);

Check failure on line 146 in src/gql/resolvers/CantoDamAssetResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $callback of function array_map expects (callable(mixed): mixed)|null, 'nestedLowercase' given.
}
return strtolower($value);
}
}
1 change: 1 addition & 0 deletions src/gql/types/CantoDamAssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lsst\cantodamassets\gql\types;

use Craft;
use craft\gql\base\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use lsst\cantodamassets\gql\interfaces\CantoDamAssetInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/gql/types/generators/CantoDamAssetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lsst\cantodamassets\gql\types\generators;

use Craft;
use craft\gql\base\GeneratorInterface;
use craft\gql\GqlEntityRegistry;
use craft\gql\TypeLoader;
Expand All @@ -28,7 +29,6 @@ public static function generateTypes(mixed $context = null): array
TypeLoader::registerType($typeName, function() use ($cantoDamAssetType) {
return $cantoDamAssetType;
});

return $gqlTypes;
}

Expand Down
13 changes: 12 additions & 1 deletion src/lib/laravel/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public function whereContainsIn($keys, $value): Collection
public function whereIn($key, $values, $strict = false)
{
$values = $this->getArrayableItems($values);

return $this->filter(function($item) use ($key, $values, $strict) {
$item = data_get($item, $key);
// Handle the case where the data is an array of items
Expand All @@ -82,6 +81,18 @@ public function whereIn($key, $values, $strict = false)
});
}

public function whereAll(array $keyValues) {
$whereAllArr = $this;
foreach($keyValues as $filters) {
foreach($filters as $filter) {
$filter = str_replace("'", '"', $filter );
$filter_decoded = json_decode($filter, true);
$whereAllArr = $whereAllArr->whereIn($filter_decoded["key"], $filter_decoded["values"]);
}
}
return $whereAllArr;
}

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