Skip to content

Commit 5fe2038

Browse files
committed
Refactor and standardize type naming across strings, floats, and integers
- Renamed classes to align with naming conventions (`IntegerStandart` to `IntegerStandard`, `StringBasic` to `StringStandard`, `NonNegativeFloat` to `FloatNonNegative`, etc.). - Introduced alias classes (`NonEmptyStr`, `NonNegativeFloat`) for backward compatibility and improved extensibility. - Added `StringNonEmpty` and `StringVarChar255` types with strict validation for non-empty and varchar constraints. - Updated tests, documentation, and examples to reflect new names and added functionality. - Enhanced exception handling with specific error messages for better validation feedback.
1 parent d2c74fb commit 5fe2038

17 files changed

+198
-87
lines changed

docs/USAGE.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ Static usage examples
4141
---------------------
4242

4343
```php
44-
use PhpTypedValues\DateTime\DateTimeAtom;use PhpTypedValues\DateTime\Timestamp\TimestampSeconds;use PhpTypedValues\Float\FloatBasic;use PhpTypedValues\Float\NonNegativeFloat;use PhpTypedValues\Integer\IntegerStandart;use PhpTypedValues\Integer\IntegerNonNegative;use PhpTypedValues\Integer\IntegerPositive;use PhpTypedValues\Integer\IntegerWeekDay;use PhpTypedValues\String\NonEmptyStr;use PhpTypedValues\String\StringBasic;
44+
use PhpTypedValues\DateTime\DateTimeAtom;use PhpTypedValues\DateTime\Timestamp\TimestampSeconds;use PhpTypedValues\Float\FloatStandard;use PhpTypedValues\Float\FloatNonNegative;use PhpTypedValues\Integer\IntegerStandard;use PhpTypedValues\Integer\IntegerNonNegative;use PhpTypedValues\Integer\IntegerPositive;use PhpTypedValues\Integer\IntegerWeekDay;use PhpTypedValues\String\StringNonEmpty;use PhpTypedValues\String\StringStandard;
4545

4646
// Integers
47-
$any = IntegerStandart::fromInt(-10);
47+
$any = IntegerStandard::fromInt(-10);
4848
$pos = IntegerPositive::fromInt(1);
4949
$nn = IntegerNonNegative::fromInt(0);
5050
$wd = IntegerWeekDay::fromInt(7); // 1..7
@@ -54,12 +54,12 @@ $posFromString = IntegerPositive::fromString('123');
5454
$wdFromString = IntegerWeekDay::fromString('5');
5555

5656
// Strings
57-
$greeting = StringBasic::fromString('hello');
58-
$name = NonEmptyStr::fromString('Alice');
57+
$greeting = StringStandard::fromString('hello');
58+
$name = StringNonEmpty::fromString('Alice');
5959

6060
// Floats
61-
$price = FloatBasic::fromString('19.99');
62-
$ratio = NonNegativeFloat::fromFloat(0.5); // >= 0
61+
$price = FloatStandard::fromString('19.99');
62+
$ratio = FloatNonNegative::fromFloat(0.5); // >= 0
6363

6464
// DateTime (RFC 3339 / ATOM)
6565
$dt = DateTimeAtom::fromString('2025-01-02T03:04:05+00:00');
@@ -83,18 +83,18 @@ Invalid input throws an exception with a helpful message.
8383
```php
8484
use PhpTypedValues\Integer\IntegerPositive;
8585
use PhpTypedValues\Integer\IntegerWeekDay;
86-
use PhpTypedValues\String\NonEmptyStr;
87-
use PhpTypedValues\Float\NonNegativeFloat;
86+
use PhpTypedValues\String\StringNonEmpty;
87+
use PhpTypedValues\Float\FloatNonNegative;
8888
use PhpTypedValues\DateTime\DateTimeAtom;
8989

9090
IntegerPositive::fromInt(0); // throws: must be > 0
9191
IntegerPositive::fromString('12.3'); // throws: String has no valid integer
9292

9393
IntegerWeekDay::fromInt(0); // throws: Value must be between 1 and 7
9494

95-
NonEmptyStr::fromString(''); // throws: Value must be a non-empty string
95+
StringNonEmpty::fromString(''); // throws: Value must be a non-empty string
9696

97-
NonNegativeFloat::fromString('abc'); // throws: String has no valid float
97+
FloatNonNegative::fromString('abc'); // throws: String has no valid float
9898

9999
DateTimeAtom::fromString('not-a-date'); // throws: String has no valid datetime
100100
```
@@ -191,19 +191,19 @@ declare(strict_types=1);
191191
namespace App\Domain;
192192

193193
use PhpTypedValues\Integer\IntegerPositive;
194-
use PhpTypedValues\String\NonEmptyStr;
195-
use PhpTypedValues\Float\NonNegativeFloat;
194+
use PhpTypedValues\String\StringNonEmpty;
195+
use PhpTypedValues\Float\FloatNonNegative;
196196
use PhpTypedValues\DateTime\DateTimeAtom;
197197

198198
final class Profile
199199
{
200200
public function __construct(
201201
public readonly IntegerPositive $id,
202-
public readonly NonEmptyStr $firstName,
203-
public readonly NonEmptyStr $lastName,
204-
public readonly ?NonEmptyStr $middleName, // nullable field
202+
public readonly StringNonEmpty $firstName,
203+
public readonly StringNonEmpty $lastName,
204+
public readonly ?StringNonEmpty $middleName, // nullable field
205205
public readonly ?DateTimeAtom $birthDate, // nullable field
206-
public readonly ?NonNegativeFloat $heightM // nullable field
206+
public readonly ?FloatNonNegative $heightM // nullable field
207207
) {}
208208

209209
// Convenience named constructor that accepts raw scalars and builds primitives internally
@@ -217,11 +217,11 @@ final class Profile
217217
): self {
218218
return new self(
219219
IntegerPositive::fromInt($id),
220-
NonEmptyStr::fromString($firstName),
221-
NonEmptyStr::fromString($lastName),
222-
$middleName !== null ? NonEmptyStr::fromString($middleName) : null,
220+
StringNonEmpty::fromString($firstName),
221+
StringNonEmpty::fromString($lastName),
222+
$middleName !== null ? StringNonEmpty::fromString($middleName) : null,
223223
$birthDateAtom !== null ? DateTimeAtom::fromString($birthDateAtom) : null,
224-
$heightM !== null ? NonNegativeFloat::fromString((string)$heightM) : null,
224+
$heightM !== null ? FloatNonNegative::fromString((string)$heightM) : null,
225225
);
226226
}
227227
}
@@ -238,9 +238,9 @@ $p1 = Profile::fromScalars(
238238

239239
$p2 = new Profile(
240240
id: IntegerPositive::fromInt(202),
241-
firstName: NonEmptyStr::fromString('Bob'),
242-
lastName: NonEmptyStr::fromString('Johnson'),
243-
middleName: NonEmptyStr::fromString('A.'),
241+
firstName: StringNonEmpty::fromString('Bob'),
242+
lastName: StringNonEmpty::fromString('Johnson'),
243+
middleName: StringNonEmpty::fromString('A.'),
244244
birthDate: null,
245245
heightM: null,
246246
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\Float\Alias;
6+
7+
use PhpTypedValues\Float\FloatNonNegative;
8+
9+
/**
10+
* @psalm-immutable
11+
*/
12+
readonly class NonNegativeFloat extends FloatNonNegative
13+
{
14+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* @psalm-immutable
1414
*/
15-
readonly class NonNegativeFloat extends FloatType
15+
readonly class FloatNonNegative extends FloatType
1616
{
1717
protected float $value;
1818

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @psalm-immutable
1414
*/
15-
readonly class FloatBasic extends FloatType
15+
readonly class FloatStandard extends FloatType
1616
{
1717
protected float $value;
1818

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @psalm-immutable
1212
*/
13-
readonly class IntegerStandart extends IntType
13+
readonly class IntegerStandard extends IntType
1414
{
1515
protected int $value;
1616

src/String/Alias/NonEmptyStr.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\String\Alias;
6+
7+
use PhpTypedValues\String\StringNonEmpty;
8+
9+
/**
10+
* @psalm-immutable
11+
*/
12+
readonly class NonEmptyStr extends StringNonEmpty
13+
{
14+
}

src/String/DB/StringVarChar255.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\String\DB;
6+
7+
use PhpTypedValues\Code\Exception\StringTypeException;
8+
use PhpTypedValues\Code\String\StrType;
9+
10+
/**
11+
* @psalm-immutable
12+
*/
13+
readonly class StringVarChar255 extends StrType
14+
{
15+
protected string $value;
16+
17+
/**
18+
* @throws StringTypeException
19+
*/
20+
public function __construct(string $value)
21+
{
22+
if (mb_strlen($value) > 255) {
23+
throw new StringTypeException('String is too long, max 255 chars allowed');
24+
}
25+
26+
$this->value = $value;
27+
}
28+
29+
/**
30+
* @throws StringTypeException
31+
*/
32+
public static function fromString(string $value): static
33+
{
34+
return new static($value);
35+
}
36+
37+
public function value(): string
38+
{
39+
return $this->value;
40+
}
41+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* @psalm-immutable
1414
*/
15-
readonly class NonEmptyStr extends StrType
15+
readonly class StringNonEmpty extends StrType
1616
{
1717
/** @var non-empty-string */
1818
protected string $value;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @psalm-immutable
1313
*/
14-
readonly class StringBasic extends StrType
14+
readonly class StringStandard extends StrType
1515
{
1616
protected string $value;
1717

src/psalmTest.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,53 @@
1212
use PhpTypedValues\DateTime\DateTimeRFC3339;
1313
use PhpTypedValues\DateTime\Timestamp\TimestampMilliseconds;
1414
use PhpTypedValues\DateTime\Timestamp\TimestampSeconds;
15-
use PhpTypedValues\Float\FloatBasic;
16-
use PhpTypedValues\Float\NonNegativeFloat;
15+
use PhpTypedValues\Float\Alias\NonNegativeFloat;
16+
use PhpTypedValues\Float\FloatNonNegative;
17+
use PhpTypedValues\Float\FloatStandard;
1718
use PhpTypedValues\Integer\Alias\Id;
1819
use PhpTypedValues\Integer\Alias\NonNegativeInt;
1920
use PhpTypedValues\Integer\Alias\PositiveInt;
2021
use PhpTypedValues\Integer\IntegerNonNegative;
2122
use PhpTypedValues\Integer\IntegerPositive;
22-
use PhpTypedValues\Integer\IntegerStandart;
23+
use PhpTypedValues\Integer\IntegerStandard;
2324
use PhpTypedValues\Integer\IntegerWeekDay;
24-
use PhpTypedValues\String\NonEmptyStr;
25-
use PhpTypedValues\String\StringBasic;
25+
use PhpTypedValues\String\Alias\NonEmptyStr;
26+
use PhpTypedValues\String\StringNonEmpty;
27+
use PhpTypedValues\String\StringStandard;
2628

2729
/**
2830
* Integer.
2931
*/
30-
testInteger(IntegerStandart::fromInt(10)->value());
32+
testInteger(IntegerStandard::fromInt(10)->value());
3133
testPositiveInt(IntegerPositive::fromInt(10)->value());
3234
testNonNegativeInt(IntegerNonNegative::fromInt(10)->value());
3335
testWeekDayInt(IntegerWeekDay::fromInt(7)->value());
3436

3537
echo NonNegativeInt::fromString('10')->toString() . \PHP_EOL;
3638
echo PositiveInt::fromString('10')->toString() . \PHP_EOL;
37-
echo IntegerStandart::fromString('10')->toString() . \PHP_EOL;
39+
echo IntegerStandard::fromString('10')->toString() . \PHP_EOL;
3840
echo Id::fromString('10')->toString() . \PHP_EOL;
3941

4042
/**
4143
* String.
4244
*/
43-
testString(StringBasic::fromString('hi')->value());
44-
testNonEmptyString(NonEmptyStr::fromString('hi')->value());
45+
testString(StringStandard::fromString('hi')->value());
46+
testNonEmptyString(StringNonEmpty::fromString('hi')->value());
4547

46-
echo StringBasic::fromString('hi')->toString() . \PHP_EOL;
48+
echo StringStandard::fromString('hi')->toString() . \PHP_EOL;
49+
echo NonEmptyStr::fromString('hi')->toString() . \PHP_EOL;
4750

4851
/**
4952
* Float.
5053
*/
51-
testFloat(FloatBasic::fromFloat(3.14)->value());
54+
testFloat(FloatStandard::fromFloat(3.14)->value());
5255

53-
echo FloatBasic::fromString('2.71828')->toString() . \PHP_EOL;
56+
echo FloatStandard::fromString('2.71828')->toString() . \PHP_EOL;
57+
echo NonNegativeFloat::fromString('2.71828')->toString() . \PHP_EOL;
5458

5559
// PositiveFloat usage
56-
testPositiveFloat(NonNegativeFloat::fromFloat(0.5)->value());
57-
echo NonNegativeFloat::fromString('3.14159')->toString() . \PHP_EOL;
60+
testPositiveFloat(FloatNonNegative::fromFloat(0.5)->value());
61+
echo FloatNonNegative::fromString('3.14159')->toString() . \PHP_EOL;
5862

5963
/**
6064
* DateTime.

0 commit comments

Comments
 (0)