Skip to content

Commit

Permalink
Merge pull request #12 from vladshoob/feature/add_make_multiple_recur…
Browse files Browse the repository at this point in the history
…sive

Added makeMultipleRecursive in FieldHelperAbstract
  • Loading branch information
imaximius authored Sep 20, 2019
2 parents 9b27f78 + 5b0b671 commit 159a469
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Argument/ArgumentTypeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class ArgumentTypeAbstract extends AbstractInputObjectType
{
/** @var array */
private $fields;
public $fields;
private $name;
private $description;
private $multiple;
Expand Down
55 changes: 50 additions & 5 deletions Field/FieldHelperAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Garlic\GraphQL\Field;

use Garlic\GraphQL\Argument\ArgumentTypeAbstract;
use Youshido\GraphQLBundle\Field\AbstractContainerAwareField;
use Youshido\GraphQL\Type\ListType\ListType;
use Youshido\GraphQLExtension\Type\PagingParamsType;
Expand All @@ -10,7 +11,7 @@
abstract class FieldHelperAbstract extends AbstractContainerAwareField
{
/**
* Get argument and delete them from list of incoming arguments
* Get arguments and delete them from list of incoming arguments.
*
* @param $name
* @param $args
Expand All @@ -29,12 +30,12 @@ protected function cutArgument($name, &$args)
}

/**
* Make argument as list
* Modify arguments to accept list of values.
*
* @param array $arguments
* @return array
*/
protected function makeMultiple(array $arguments)
protected function makeMultiple(array $arguments): array
{
$result = [];
foreach ($arguments as $argumentName => $argument) {
Expand All @@ -44,6 +45,52 @@ protected function makeMultiple(array $arguments)
return $result;
}

/**
* Modify arguments recursively to accept list of values.
*
* @param array $arguments
* @return array
*/
protected function makeMultipleRecursive(array $arguments): array
{
foreach ($arguments as $key => $argument) {
if (isset($argument['type'])) {
$result[$key] = $this->makeMultipleCallback($argument['type']);
}
}

return $result ?? [];
}

/**
* Helper function that recursively modifies type to accept list of values.
*
* @param $type
* @return ListType
*/
private function makeMultipleCallback($type)
{
if ($type->fields ?? false) {
foreach ($type->fields as $key => $field) {
$fieldType = $field['type'];

if ($field['type'] instanceof ArgumentTypeAbstract) {
$type->fields[$key] = $this->makeMultipleCallback($fieldType);
}

if (!$field['type'] instanceof ListType) {
$type->fields[$key] = new ListType($fieldType);
}
}
}

if (!$type instanceof ListType) {
$type = new ListType($type);
}

return $type;
}

/**
* Cut and map pagination and sort arguments
*
Expand Down Expand Up @@ -85,5 +132,3 @@ protected function mergePagingSortArguments($type, array $args = [])
);
}
}


0 comments on commit 159a469

Please sign in to comment.