Skip to content

Commit

Permalink
Create RationalNumberType.php
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax authored Nov 15, 2022
1 parent dc7d14d commit bdd46e0
Showing 1 changed file with 50 additions and 0 deletions.
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' => 0],
),
]);
}
}

0 comments on commit bdd46e0

Please sign in to comment.