Skip to content

Commit

Permalink
Update Type builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksym Churkin committed Sep 27, 2019
1 parent 159a469 commit fd8e161
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 25 deletions.
13 changes: 7 additions & 6 deletions Field/FieldHelperAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,19 @@ private function makeMultipleCallback($type)
if ($type->fields ?? false) {
foreach ($type->fields as $key => $field) {
$fieldType = $field['type'];

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

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

if (!$type instanceof ListType) {
if ($type instanceof ListType) {
$this->makeMultipleCallback($type->getItemType());
} else {
$type = new ListType($type);
}

Expand Down
18 changes: 18 additions & 0 deletions Type/Interfaces/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,22 @@ public function getFields();
* @return array
*/
public function getArguments();

/**
* Remove field from the Type
*
* @param string|array $fieldName
* @return BuilderInterface
*/
public function removeField($fieldName, bool $onlyArgument = false);

/**
* Change field options
*
* @param string|array $fieldName
* @param array $options
* @param bool $onlyArgument
* @return mixed
*/
public function changeOptions($fieldName, array $options, bool $onlyArgument = false);
}
65 changes: 55 additions & 10 deletions Type/TypeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public function __construct()
*/
abstract public function build(BuilderInterface $builder);

/**
* Returns type builder
*
* @return TypeBuilder
*/
public function getBuilder()
{
return $this->builder;
}

/**
* Create an object depends on is argument required
*
Expand All @@ -39,36 +49,71 @@ abstract public function build(BuilderInterface $builder);
public function init($argument = false, $multiple = false)
{
if (!empty($argument)) {
return new ArgumentTypeAbstract(
($multiple === true) ? $this->makeMultiple($this->getArguments()) : $this->getArguments(),
$this->getName(),
$this->getDescription(),
$multiple
);
return $this->getArgumentsType(null, $multiple);
}

return $this->getFieldsType($multiple);
}


/**
* Returns Fields Type
*
* @param bool $multiple
* @param array $roles
* @return FieldAbstract
* @throws \Youshido\GraphQL\Exception\ConfigurationException
*/
public function getFieldsType($multiple = false)
{
$fields = $this->getFields();
return new FieldAbstract(
($multiple === true) ? new ListType($this->getFields()) : $this->getFields(),
($multiple === true) ? new ListType($fields) : $fields,
$this->getName(),
$this->getDescription()
);
}

/**
* Returns Argument Type
*
* @param string $actionName
* @param bool $multiple
* @param array $roles
* @return ArgumentTypeAbstract
* @throws \Youshido\GraphQL\Exception\ConfigurationException
*/
public function getArgumentsType(string $actionName = null, $multiple = false)
{
$arguments = $this->getArguments($actionName, $multiple);
return new ArgumentTypeAbstract(
($multiple === true) ? $this->makeMultiple($arguments) : $arguments,
$this->getName(),
$this->getDescription(),
$multiple
);
}

/**
* Return array of arguments
*
* @param null $groupName
* @return array
* @throws \Youshido\GraphQL\Exception\ConfigurationException
*/
public function getArguments($groupName = null, $multiple = false)
public function getArguments($actionName = null, $multiple = false)
{
return $this->setRequired($this->updateRelations($this->builder->getArguments(), true, $multiple), $groupName);
return $this->setRequired(
$this->updateRelations($this->builder->getArguments(), true, $multiple),
$actionName
);
}

/**
* Return list of arguments
* @return array
*
* @param array $roles
* @return mixed
* @throws \Youshido\GraphQL\Exception\ConfigurationException
*/
public function getFields()
Expand Down
67 changes: 58 additions & 9 deletions Type/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,30 @@ class TypeBuilder implements BuilderInterface
{
/** @var array */
private $fields = [];

/** @var array */
private $arguments = [];

/**
* Return type fields
*
* @return array
*/
public function getFields()
{
return $this->fields;
}

/**
* Get type object
*
* @return array
*/
public function getArguments()
{
return $this->arguments;
}

/**
* Add single fields with type and list of options
*
Expand All @@ -30,23 +52,50 @@ public function addField(string $name, $type, array $options = [])
}

/**
* Return type fields
* Remove field from a Type
*
* @return array
* @param array|string $fieldName
* @param bool $onlyArgument
*
* @return $this|BuilderInterface
*/
public function getFields()
public function removeField($fieldName, bool $onlyArgument = false)
{
return $this->fields;
if(!is_array($fieldName)) {
$names = [$fieldName];
}

foreach ($names as $name) {
unset($this->$arguments[$name]);
if (false === $onlyArgument) {
unset($this->fields[$name]);
}
}

return $this;
}

/**
* Get type object
* Change field options
*
* @return array
* @param string|array $fieldName
* @param array $options
* @param bool $onlyArgument
* @return $this|mixed
*/
public function getArguments()
public function changeOptions($fieldName, array $options, bool $onlyArgument = false)
{
return $this->arguments;
}
if(!is_array($fieldName)) {
$names = [$fieldName];
}

foreach ($names as $name) {
$this->$arguments[$name] = array_merge_recursive($this->$arguments[$name], $options);
if (false === $onlyArgument) {
$this->fields[$name] = array_merge_recursive($this->fields[$name], $options);
}
}

return $this;
}
}

0 comments on commit fd8e161

Please sign in to comment.