|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Graphpinator\ExtraTypes; |
| 6 | + |
| 7 | +final class RationalNumberType extends \Graphpinator\Typesystem\Type |
| 8 | +{ |
| 9 | + protected const NAME = 'RationalNumberType'; |
| 10 | + protected const DESCRIPTION = 'RationalNumber type - structure for numerator and denominator.'; |
| 11 | + |
| 12 | + public function __construct( |
| 13 | + private \Graphpinator\ConstraintDirectives\ConstraintDirectiveAccessor $constraintDirectiveAccessor, |
| 14 | + ) |
| 15 | + { |
| 16 | + parent::__construct(); |
| 17 | + } |
| 18 | + |
| 19 | + public function validateNonNullValue(mixed $rawValue) : bool |
| 20 | + { |
| 21 | + return $rawValue instanceof \stdClass |
| 22 | + && \property_exists($rawValue, 'numerator') |
| 23 | + && \property_exists($rawValue, 'denominator') |
| 24 | + && \is_int($rawValue->numerator) |
| 25 | + && \is_int($rawValue->denominator); |
| 26 | + } |
| 27 | + |
| 28 | + protected function getFieldDefinition() : \Graphpinator\Typesystem\Field\ResolvableFieldSet |
| 29 | + { |
| 30 | + return new \Graphpinator\Typesystem\Field\ResolvableFieldSet([ |
| 31 | + \Graphpinator\Typesystem\Field\ResolvableField::create( |
| 32 | + 'numerator', |
| 33 | + \Graphpinator\Typesystem\Container::Int()->notNull(), |
| 34 | + static function(\stdClass $number) : int { |
| 35 | + return $number->numerator; |
| 36 | + }, |
| 37 | + ), |
| 38 | + \Graphpinator\Typesystem\Field\ResolvableField::create( |
| 39 | + 'denominator', |
| 40 | + \Graphpinator\Typesystem\Container::Int()->notNull(), |
| 41 | + static function(\stdClass $number) : int { |
| 42 | + return $number->denominator; |
| 43 | + }, |
| 44 | + )->addDirective( |
| 45 | + $this->constraintDirectiveAccessor->getInt(), |
| 46 | + ['min' => 0], |
| 47 | + ), |
| 48 | + ]); |
| 49 | + } |
| 50 | +} |
0 commit comments