diff --git a/src/Integer/Alias/TinyInt.php b/src/Integer/Alias/TinyInt.php new file mode 100755 index 0000000..f8fb191 --- /dev/null +++ b/src/Integer/Alias/TinyInt.php @@ -0,0 +1,18 @@ + */ + protected int $value; + + /** + * @throws IntegerTypeException + */ + public function __construct(int $value) + { + if ($value < -128 || $value > 127) { + throw new IntegerTypeException(sprintf('Expected tiny integer in range -128..127, got "%d"', $value)); + } + + $this->value = $value; + } + + /** + * @throws IntegerTypeException + */ + public static function fromInt(int $value): static + { + return new static($value); + } + + /** + * @throws IntegerTypeException + */ + public static function fromString(string $value): static + { + parent::assertIntegerString($value); + + return new static((int) $value); + } + + /** + * @return int<-128, 127> + */ + public function value(): int + { + return $this->value; + } +} diff --git a/src/psalmTest.php b/src/psalmTest.php index 5e72430..ebddf1e 100755 --- a/src/psalmTest.php +++ b/src/psalmTest.php @@ -20,6 +20,8 @@ use PhpTypedValues\Integer\Alias\IntType; use PhpTypedValues\Integer\Alias\NonNegativeInt; use PhpTypedValues\Integer\Alias\PositiveInt; +use PhpTypedValues\Integer\Alias\TinyInt; +use PhpTypedValues\Integer\DB\IntTiny; use PhpTypedValues\Integer\IntegerNonNegative; use PhpTypedValues\Integer\IntegerPositive; use PhpTypedValues\Integer\IntegerStandard; @@ -37,6 +39,11 @@ testNonNegativeInt(IntegerNonNegative::fromInt(10)->value()); testWeekDayInt(IntegerWeekDay::fromInt(7)->value()); +// DB tinyint usage +echo TinyInt::fromInt(-5)->toString() . \PHP_EOL; +echo IntTiny::fromInt(-5)->toString() . \PHP_EOL; +echo IntTiny::fromString('127')->toString() . \PHP_EOL; + echo NonNegativeInt::fromString('10')->toString() . \PHP_EOL; echo PositiveInt::fromString('10')->toString() . \PHP_EOL; echo IntegerStandard::fromString('10')->toString() . \PHP_EOL; diff --git a/tests/Unit/Integer/DB/IntTinyTest.php b/tests/Unit/Integer/DB/IntTinyTest.php new file mode 100755 index 0000000..2b6e60e --- /dev/null +++ b/tests/Unit/Integer/DB/IntTinyTest.php @@ -0,0 +1,40 @@ +value())->toBe(-128) + ->and($a->toString())->toBe('-128') + ->and($b->value())->toBe(0) + ->and($b->toString())->toBe('0') + ->and($c->value())->toBe(127) + ->and($c->toString())->toBe('127'); +}); + +it('fromString parses integers and enforces tinyint bounds', function (): void { + $v = IntTiny::fromString('-5'); + expect($v->value())->toBe(-5) + ->and($v->toString())->toBe('-5'); +}); + +it('throws when value is below -128', function (): void { + expect(fn() => new IntTiny(-129)) + ->toThrow(IntegerTypeException::class, 'Expected tiny integer in range -128..127, got "-129"'); +}); + +it('throws when value is above 127', function (): void { + expect(fn() => IntTiny::fromInt(128)) + ->toThrow(IntegerTypeException::class, 'Expected tiny integer in range -128..127, got "128"'); +}); + +it('fromString throws on non-integer strings (strict check)', function (): void { + expect(fn() => IntTiny::fromString('12.3')) + ->toThrow(IntegerTypeException::class, 'String "12.3" has no valid integer value'); +});