-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Václav Pelíšek
committed
Nov 6, 2023
1 parent
c5c0e4d
commit a93005e
Showing
4 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Graphpinator\Typesystem\Exception; | ||
|
||
final class OneOfDirectiveNotSatisfied extends \Graphpinator\Typesystem\Exception\TypeError | ||
{ | ||
public const MESSAGE = 'Exactly one field must be specified and be not null.'; | ||
|
||
public function isOutputable() : bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Graphpinator\Typesystem\Exception; | ||
|
||
final class OneOfInputInvalidFields extends \Graphpinator\Typesystem\Exception\TypeError | ||
{ | ||
public const MESSAGE = 'OneOf input type must have only nullable fields without default values.'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Graphpinator\Typesystem\Spec; | ||
|
||
use \Graphpinator\Value\ArgumentValueSet; | ||
use \Graphpinator\Value\InputValue; | ||
|
||
#[\Graphpinator\Typesystem\Attribute\Description('Built-in oneOf directive')] | ||
final class OneOfDirective extends \Graphpinator\Typesystem\Directive implements \Graphpinator\Typesystem\Location\InputObjectLocation | ||
{ | ||
protected const NAME = 'oneOf'; | ||
|
||
public function validateInputUsage(\Graphpinator\Typesystem\InputType $inputType, ArgumentValueSet $arguments) : bool | ||
{ | ||
foreach ($inputType->getArguments() as $argument) { | ||
if ($argument->getType() instanceof \Graphpinator\Typesystem\NotNullType || | ||
$argument->getDefaultValue() instanceof \Graphpinator\Value\ArgumentValue) { | ||
throw new \Graphpinator\Typesystem\Exception\OneOfInputInvalidFields(); | ||
} | ||
} | ||
} | ||
|
||
public function resolveInputObject(ArgumentValueSet $arguments, InputValue $inputValue) : void | ||
{ | ||
$currentCount = 0; | ||
|
||
foreach ($inputValue as $innerValue) { | ||
\assert($innerValue instanceof \Graphpinator\Value\ArgumentValue); | ||
|
||
if ($currentCount >= 1 || $innerValue->getValue() instanceof \Graphpinator\Value\NullValue) { | ||
throw new \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied(); | ||
} | ||
|
||
++$currentCount; | ||
} | ||
} | ||
|
||
protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet | ||
{ | ||
return new \Graphpinator\Typesystem\Argument\ArgumentSet([]); | ||
} | ||
} |