Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Lexicon\Schema;
use Wotnak\Atproto\Lexicon\DataModelValidator;use Wotnak\Atproto\Lexicon\ValidationContext;
/** * Validates integer values and numeric constraints. * * @api */final class IntegerSchema extends AbstractSchema implements DefaultValueSchemaInterface {
/** * @param list<int>|null $enum */ public function __construct( private readonly ?int $minimum = NULL, private readonly ?int $maximum = NULL, private readonly ?array $enum = NULL, private readonly ?int $const = NULL, private readonly ?int $default = NULL, ) {}
/** * Validates the value. */ public function validateValue(mixed $value, ValidationContext $context): mixed { if ($value === NULL && $context->options->mode === 'parse' && $this->default !== NULL) { $value = $this->default; } $value = DataModelValidator::integerValue($value); if ($value === NULL) { $context->addViolation('Expected integer.', 'invalid_type'); return $value; } if ($this->const !== NULL && $value !== $this->const) { $context->addViolation(\sprintf('Expected %d.', $this->const), 'invalid_const'); } if ($this->enum !== NULL && !\in_array($value, $this->enum, TRUE)) { $context->addViolation('Value is not one of the allowed enum values.', 'invalid_enum'); } if ($this->minimum !== NULL && $value < $this->minimum) { $context->addViolation(\sprintf('Expected value greater than or equal to %d.', $this->minimum), 'minimum'); } if ($this->maximum !== NULL && $value > $this->maximum) { $context->addViolation(\sprintf('Expected value less than or equal to %d.', $this->maximum), 'maximum'); } return $value; }
/** * Returns the default value declared by the schema. */ public function defaultValue(): ?int { return $this->default; }
}