Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax committed Nov 10, 2023
2 parents 3bdcb07 + c8f8c62 commit 3c287df
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ This package contains the following types:
- `\Graphpinator\ExtraTypes\NotNullForArgDirective`
- Directive on Field definition location.
- It guarantees that nullable field wont return null if specified argument value is provided.
- `\Graphpinator\ExtraTypes\OptionalDirective`
- Argument definition location.
- It marks nullable input field as optional, but not nullable. This input field may be either omitted or contain valid value, but may not be set to null.
2 changes: 1 addition & 1 deletion src/OptionalDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function resolveArgumentDefinition(
throw new class extends \Graphpinator\Exception\GraphpinatorBase {
public const MESSAGE = 'Input field is @optional and therefore cannot contain null value.';

public function isOutputable(): bool
public function isOutputable() : bool
{
return true;
}
Expand Down
35 changes: 35 additions & 0 deletions src/RationalNumberInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\ExtraTypes;

final class RationalNumberInput extends \Graphpinator\Typesystem\InputType
{
protected const NAME = 'RationalNumberInput';
protected const DESCRIPTION = 'RationalNumber input - input for the RationalNumber structure.';

public function __construct(
private \Graphpinator\ConstraintDirectives\ConstraintDirectiveAccessor $constraintDirectiveAccessor,
)
{
parent::__construct();
}

protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet
{
return new \Graphpinator\Typesystem\Argument\ArgumentSet([
\Graphpinator\Typesystem\Argument\Argument::create(
'numerator',
\Graphpinator\Typesystem\Container::Int()->notNull(),
),
\Graphpinator\Typesystem\Argument\Argument::create(
'denominator',
\Graphpinator\Typesystem\Container::Int()->notNull(),
)->addDirective(
$this->constraintDirectiveAccessor->getInt(),
['min' => 1],
),
]);
}
}
50 changes: 50 additions & 0 deletions src/RationalNumberType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\ExtraTypes;

final class RationalNumberType extends \Graphpinator\Typesystem\Type
{
protected const NAME = 'RationalNumberType';
protected const DESCRIPTION = 'RationalNumber type - structure for numerator and denominator.';

public function __construct(
private \Graphpinator\ConstraintDirectives\ConstraintDirectiveAccessor $constraintDirectiveAccessor,
)
{
parent::__construct();
}

public function validateNonNullValue(mixed $rawValue) : bool
{
return $rawValue instanceof \stdClass
&& \property_exists($rawValue, 'numerator')
&& \property_exists($rawValue, 'denominator')
&& \is_int($rawValue->numerator)
&& \is_int($rawValue->denominator);
}

protected function getFieldDefinition() : \Graphpinator\Typesystem\Field\ResolvableFieldSet
{
return new \Graphpinator\Typesystem\Field\ResolvableFieldSet([
\Graphpinator\Typesystem\Field\ResolvableField::create(
'numerator',
\Graphpinator\Typesystem\Container::Int()->notNull(),
static function(\stdClass $number) : int {
return $number->numerator;
},
),
\Graphpinator\Typesystem\Field\ResolvableField::create(
'denominator',
\Graphpinator\Typesystem\Container::Int()->notNull(),
static function(\stdClass $number) : int {
return $number->denominator;
},
)->addDirective(
$this->constraintDirectiveAccessor->getInt(),
['min' => 1],
),
]);
}
}

0 comments on commit 3c287df

Please sign in to comment.