-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Graphpinator\ExtraTypes; | ||
|
||
final class OptionalDirective extends \Graphpinator\Typesystem\Directive implements | ||
\Graphpinator\Typesystem\Location\ArgumentDefinitionLocation | ||
{ | ||
protected const NAME = 'optional'; | ||
protected const DESCRIPTION = 'Input value for this argument can be either omitted or have non-null value.'; | ||
|
||
public static function isPure() : bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function validateArgumentUsage( | ||
\Graphpinator\Typesystem\Argument\Argument $argument, | ||
\Graphpinator\Value\ArgumentValueSet $arguments, | ||
) : bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function validateVariance( | ||
?\Graphpinator\Value\ArgumentValueSet $biggerSet, | ||
?\Graphpinator\Value\ArgumentValueSet $smallerSet, | ||
) : void | ||
{ | ||
if ($biggerSet instanceof \Graphpinator\Value\ArgumentValueSet && | ||
$smallerSet instanceof \Graphpinator\Value\ArgumentValueSet && | ||
$biggerSet->isSame($smallerSet)) { | ||
return; | ||
} | ||
|
||
throw new \Exception(); | ||
} | ||
|
||
public function resolveArgumentDefinition( | ||
\Graphpinator\Value\ArgumentValueSet $arguments, | ||
\Graphpinator\Value\ArgumentValue $argumentValue, | ||
) : void | ||
{ | ||
if ($argumentValue->getValue() instanceof \Graphpinator\Value\NullInputedValue) { | ||
throw new class extends \Graphpinator\Exception\GraphpinatorBase { | ||
public const MESSAGE = 'Input field is @optional and therefore cannot contain null value.'; | ||
|
||
public function isOutputable(): bool | ||
{ | ||
return true; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet | ||
{ | ||
return new \Graphpinator\Typesystem\Argument\ArgumentSet(); | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Graphpinator\ExtraTypes\Tests\Unit; | ||
|
||
final class OptionalDirectiveTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function testValue() : void | ||
{ | ||
$value = self::getInput()->accept( | ||
new \Graphpinator\Value\ConvertRawValueVisitor((object) ['arg1' => 'Value'], new \Graphpinator\Common\Path()), | ||
); | ||
$value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([])); | ||
|
||
self::assertInstanceOf(\Graphpinator\Value\InputValue::class, $value); | ||
} | ||
|
||
public function testOmitted() : void | ||
{ | ||
$value = self::getInput()->accept( | ||
new \Graphpinator\Value\ConvertRawValueVisitor((object) [], new \Graphpinator\Common\Path()), | ||
); | ||
$value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([])); | ||
|
||
self::assertInstanceOf(\Graphpinator\Value\InputValue::class, $value); | ||
} | ||
|
||
public function testInvalid() : void | ||
{ | ||
$this->expectException(\Graphpinator\Exception\GraphpinatorBase::class); | ||
$this->expectExceptionMessage('Input field is @optional and therefore cannot contain null value.'); | ||
|
||
$value = self::getInput()->accept( | ||
new \Graphpinator\Value\ConvertRawValueVisitor((object) ['arg1' => null], new \Graphpinator\Common\Path()), | ||
); | ||
$value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([])); | ||
} | ||
|
||
private static function getInput() : \Graphpinator\Typesystem\InputType | ||
{ | ||
return new class extends \Graphpinator\Typesystem\InputType { | ||
protected const NAME = 'ConstraintInput'; | ||
|
||
protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet | ||
{ | ||
return new \Graphpinator\Typesystem\Argument\ArgumentSet([ | ||
\Graphpinator\Typesystem\Argument\Argument::create( | ||
'arg1', | ||
\Graphpinator\Typesystem\Container::String(), | ||
)->addDirective(new \Graphpinator\ExtraTypes\OptionalDirective()), | ||
]); | ||
} | ||
}; | ||
} | ||
} |