Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Lexicon\Schema;
use Wotnak\Atproto\Lexicon\ValidationContext;
/** * Validates boolean values and their defaults. * * @api */final class BooleanSchema extends AbstractSchema implements DefaultValueSchemaInterface {
public function __construct( private readonly ?bool $const = NULL, private readonly ?bool $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; } if (!\is_bool($value)) { $context->addViolation('Expected boolean.', 'invalid_type'); return $value; } if ($this->const !== NULL && $value !== $this->const) { $context->addViolation(\sprintf('Expected %s.', $this->const ? 'true' : 'false'), 'invalid_const'); } return $value; }
/** * Returns the default value declared by the schema. */ public function defaultValue(): ?bool { return $this->default; }
}