Skip to content

Commit 52ab180

Browse files
committed
Introduce WeekDayInt type and improve integer assertions
- Added `WeekDayInt` class to validate values between 1 and 7 inclusively. - Extended `BaseIntType` with `assertGreaterThan` and `assertLessThan` methods for flexible value validation. - Refactored test cases for `PositiveInt`, `NonNegativeInt`, and other integer types to improve clarity and separation of concerns. - Integrated `symfony/polyfill-php83` to support future-proof functionality. - Updated `composer.lock` to reflect dependency changes. - Adjusted static analysis (`psalm-baseline.xml`) for new types.
1 parent d3f1006 commit 52ab180

File tree

10 files changed

+209
-23
lines changed

10 files changed

+209
-23
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"type": "library",
55
"license": "MIT",
66
"require": {
7-
"php": ">=8.2"
7+
"php": ">=8.2",
8+
"symfony/polyfill-php83": "^1.33"
89
},
910
"autoload": {
1011
"psr-4": {

composer.lock

Lines changed: 89 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

psalm-baseline.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<files psalm-version="6.13.1@1e3b7f0a8ab32b23197b91107adc0a7ed8a05b51">
3-
<file src="src/BaseType/BaseIntType.php">
4-
<UnusedMethodCall>
5-
<code><![CDATA[assert]]></code>
6-
</UnusedMethodCall>
7-
</file>
83
<file src="src/Contract/BaseTypeInterface.php">
94
<PossiblyUnusedMethod>
105
<code><![CDATA[toString]]></code>
@@ -33,4 +28,9 @@
3328
<code><![CDATA[PositiveInt]]></code>
3429
</UnusedClass>
3530
</file>
31+
<file src="src/Type/Integer/WeekDayInt.php">
32+
<UnusedClass>
33+
<code><![CDATA[WeekDayInt]]></code>
34+
</UnusedClass>
35+
</file>
3636
</files>

src/BaseType/BaseIntType.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PhpTypedValues\Contract\IntTypeInterface;
1010
use PhpTypedValues\Exception\IntegerTypeException;
1111

12+
use function sprintf;
13+
1214
/**
1315
* @psalm-immutable
1416
*/
@@ -52,4 +54,42 @@ public function toString(): string
5254
{
5355
return (string) $this->value;
5456
}
57+
58+
/**
59+
* @throws IntegerTypeException
60+
*/
61+
#[Override]
62+
public function assertLessThan(int $value, int $limit, bool $inclusive = false): void
63+
{
64+
if ($inclusive && $value > $limit) {
65+
throw new IntegerTypeException(sprintf('Value "%d" is greater than maximum "%d"', $value, $limit));
66+
}
67+
68+
if (!$inclusive && $value >= $limit) {
69+
throw new IntegerTypeException(sprintf('Value "%d" is greater than or equal to maximum "%d"', $value, $limit));
70+
}
71+
}
72+
73+
/**
74+
* @throws IntegerTypeException
75+
*/
76+
#[Override]
77+
public function assertGreaterThan(int $value, int $limit, bool $inclusive = false): void
78+
{
79+
if ($inclusive && $value < $limit) {
80+
throw new IntegerTypeException(sprintf('Value "%d" is less than minimum "%d"', $value, $limit));
81+
}
82+
83+
if (!$inclusive && $value <= $limit) {
84+
throw new IntegerTypeException(sprintf('Value "%d" is less than or equal to minimum "%d"', $value, $limit));
85+
}
86+
}
87+
88+
/**
89+
* Domain-specific assertion to validate provided integer value.
90+
*
91+
* @throws IntegerTypeException
92+
*/
93+
#[Override]
94+
abstract public function assert(int $value): void;
5595
}

src/Contract/IntTypeInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ public static function fromString(string $value): self;
1616
public static function fromInt(int $value): self;
1717

1818
public function assert(int $value): void;
19+
20+
public function assertGreaterThan(int $value, int $limit, bool $inclusive = false): void;
21+
22+
public function assertLessThan(int $value, int $limit, bool $inclusive = false): void;
1923
}

src/Type/Integer/WeekDayInt.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\Type\Integer;
6+
7+
use Override;
8+
use PhpTypedValues\BaseType\BaseIntType;
9+
use PhpTypedValues\Exception\IntegerTypeException;
10+
11+
/**
12+
* @psalm-immutable
13+
*/
14+
final readonly class WeekDayInt extends BaseIntType
15+
{
16+
/**
17+
* @throws IntegerTypeException
18+
*/
19+
#[Override]
20+
public function assert(int $value): void
21+
{
22+
$this->assertGreaterThan($value, 1, true);
23+
$this->assertLessThan($value, 7, true);
24+
}
25+
}

tests/Unit/Type/Integer/IntegerTypeTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
expect(Integer::fromString('5')->value())->toBe(5);
1414
});
1515

16-
it('creates Integer from string value and fails', function (): void {
16+
it('fails on "integer-ish" float string', function (): void {
17+
expect(fn() => Integer::fromString('5.0'))->toThrow(IntegerTypeException::class);
18+
});
19+
20+
it('fails on float string', function (): void {
1721
expect(fn() => Integer::fromString('5.5'))->toThrow(IntegerTypeException::class);
1822
});
1923

20-
it('creates Integer from invalid string and fail', function (): void {
24+
it('fails on type mismatch', function (): void {
2125
expect(function () {
2226
try {
2327
// invalid integer string (contains decimal point)
24-
Integer::fromString('34.0');
28+
Integer::fromInt('34.66');
2529
} catch (Throwable $e) {
2630
throw new IntegerTypeException('Failed to create Integer from string', previous: $e);
2731
}

tests/Unit/Type/Integer/NonNegativeIntTypeTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use PhpTypedValues\Exception\IntegerTypeException;
66
use PhpTypedValues\Type\Integer\NonNegativeInt;
77

8-
it('NonNegativeInt accepts zero and rejects negatives', function (): void {
8+
it('creates NonNegativeInt', function (): void {
99
expect((new NonNegativeInt(0))->value())->toBe(0);
10-
expect(fn() => new NonNegativeInt(-1))->toThrow(IntegerTypeException::class);
10+
});
11+
12+
it('fails on negatives', function (): void {
13+
expect(fn() => NonNegativeInt::fromInt(-1))->toThrow(IntegerTypeException::class);
1114
});

tests/Unit/Type/Integer/PositiveIntTypeTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
use PhpTypedValues\Exception\IntegerTypeException;
66
use PhpTypedValues\Type\Integer\PositiveInt;
77

8-
it('PositiveInt accepts >0 and rejects 0 or negatives', function (): void {
9-
expect((new PositiveInt(1))->value())->toBe(1);
10-
expect(fn() => new PositiveInt(0))->toThrow(IntegerTypeException::class);
11-
expect(fn() => new PositiveInt(-1))->toThrow(IntegerTypeException::class);
8+
it('creates PositiveInt', function (): void {
9+
expect(PositiveInt::fromInt(1)->value())->toBe(1);
10+
});
11+
12+
it('fails on 0', function (): void {
13+
expect(fn() => PositiveInt::fromInt(0))->toThrow(IntegerTypeException::class);
14+
});
15+
16+
it('fails on negatives', function (): void {
17+
expect(fn() => PositiveInt::fromInt(-1))->toThrow(IntegerTypeException::class);
1218
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Exception\IntegerTypeException;
6+
use PhpTypedValues\Type\Integer\WeekDayInt;
7+
8+
it('creates WeekDayInt from int 1', function (): void {
9+
expect(WeekDayInt::fromInt(1)->value())->toBe(1);
10+
});
11+
12+
it('creates WeekDayInt from int 7', function (): void {
13+
expect(WeekDayInt::fromInt(7)->value())->toBe(7);
14+
});
15+
16+
it('fails on 8', function (): void {
17+
expect(fn() => WeekDayInt::fromInt(8))->toThrow(IntegerTypeException::class);
18+
});
19+
20+
it('fails on 0', function (): void {
21+
expect(fn() => WeekDayInt::fromInt(0))->toThrow(IntegerTypeException::class);
22+
});

0 commit comments

Comments
 (0)