Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Create a quick test script (e.g., demo.php):
<?php
require __DIR__ . '/vendor/autoload.php';

use PhpTypedValues\Integer\PositiveInt;
use PhpTypedValues\Integer\IntegerPositive;

echo PositiveInt::fromString('21')->value(); // 21
echo IntegerPositive::fromString('21')->value(); // 21
```

Run it:
Expand Down
78 changes: 39 additions & 39 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ Static usage examples
---------------------

```php
use PhpTypedValues\DateTime\DateTimeAtom;use PhpTypedValues\DateTime\Timestamp\TimestampSeconds;use PhpTypedValues\Float\FloatBasic;use PhpTypedValues\Float\NonNegativeFloat;use PhpTypedValues\Integer\IntegerBasic;use PhpTypedValues\Integer\NonNegativeInt;use PhpTypedValues\Integer\PositiveInt;use PhpTypedValues\Integer\WeekDayInt;use PhpTypedValues\String\NonEmptyStr;use PhpTypedValues\String\StringBasic;
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;

// Integers
$any = IntegerBasic::fromInt(-10);
$pos = PositiveInt::fromInt(1);
$nn = NonNegativeInt::fromInt(0);
$wd = WeekDayInt::fromInt(7); // 1..7
$any = IntegerStandard::fromInt(-10);
$pos = IntegerPositive::fromInt(1);
$nn = IntegerNonNegative::fromInt(0);
$wd = IntegerWeekDay::fromInt(7); // 1..7

// From string (integers)
$posFromString = PositiveInt::fromString('123');
$wdFromString = WeekDayInt::fromString('5');
$posFromString = IntegerPositive::fromString('123');
$wdFromString = IntegerWeekDay::fromString('5');

// Strings
$greeting = StringBasic::fromString('hello');
$name = NonEmptyStr::fromString('Alice');
$greeting = StringStandard::fromString('hello');
$name = StringNonEmpty::fromString('Alice');

// Floats
$price = FloatBasic::fromString('19.99');
$ratio = NonNegativeFloat::fromFloat(0.5); // >= 0
$price = FloatStandard::fromString('19.99');
$ratio = FloatNonNegative::fromFloat(0.5); // >= 0

// DateTime (RFC 3339 / ATOM)
$dt = DateTimeAtom::fromString('2025-01-02T03:04:05+00:00');
Expand All @@ -81,20 +81,20 @@ Validation errors (static constructors)
Invalid input throws an exception with a helpful message.

```php
use PhpTypedValues\Integer\PositiveInt;
use PhpTypedValues\Integer\WeekDayInt;
use PhpTypedValues\String\NonEmptyStr;
use PhpTypedValues\Float\NonNegativeFloat;
use PhpTypedValues\Integer\IntegerPositive;
use PhpTypedValues\Integer\IntegerWeekDay;
use PhpTypedValues\String\StringNonEmpty;
use PhpTypedValues\Float\FloatNonNegative;
use PhpTypedValues\DateTime\DateTimeAtom;

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

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

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

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

DateTimeAtom::fromString('not-a-date'); // throws: String has no valid datetime
```
Expand All @@ -110,9 +110,9 @@ declare(strict_types=1);

namespace App\Domain;

use PhpTypedValues\Integer\PositiveInt;
use PhpTypedValues\Integer\IntegerPositive;

final class UserId extends PositiveInt {}
final class UserId extends IntegerPositive {}

// Usage
$userId = UserId::fromInt(42);
Expand Down Expand Up @@ -190,20 +190,20 @@ declare(strict_types=1);

namespace App\Domain;

use PhpTypedValues\Integer\PositiveInt;
use PhpTypedValues\String\NonEmptyStr;
use PhpTypedValues\Float\NonNegativeFloat;
use PhpTypedValues\Integer\IntegerPositive;
use PhpTypedValues\String\StringNonEmpty;
use PhpTypedValues\Float\FloatNonNegative;
use PhpTypedValues\DateTime\DateTimeAtom;

final class Profile
{
public function __construct(
public readonly PositiveInt $id,
public readonly NonEmptyStr $firstName,
public readonly NonEmptyStr $lastName,
public readonly ?NonEmptyStr $middleName, // nullable field
public readonly IntegerPositive $id,
public readonly StringNonEmpty $firstName,
public readonly StringNonEmpty $lastName,
public readonly ?StringNonEmpty $middleName, // nullable field
public readonly ?DateTimeAtom $birthDate, // nullable field
public readonly ?NonNegativeFloat $heightM // nullable field
public readonly ?FloatNonNegative $heightM // nullable field
) {}

// Convenience named constructor that accepts raw scalars and builds primitives internally
Expand All @@ -216,12 +216,12 @@ final class Profile
int|float|string|null $heightM
): self {
return new self(
PositiveInt::fromInt($id),
NonEmptyStr::fromString($firstName),
NonEmptyStr::fromString($lastName),
$middleName !== null ? NonEmptyStr::fromString($middleName) : null,
IntegerPositive::fromInt($id),
StringNonEmpty::fromString($firstName),
StringNonEmpty::fromString($lastName),
$middleName !== null ? StringNonEmpty::fromString($middleName) : null,
$birthDateAtom !== null ? DateTimeAtom::fromString($birthDateAtom) : null,
$heightM !== null ? NonNegativeFloat::fromString((string)$heightM) : null,
$heightM !== null ? FloatNonNegative::fromString((string)$heightM) : null,
);
}
}
Expand All @@ -237,10 +237,10 @@ $p1 = Profile::fromScalars(
);

$p2 = new Profile(
id: PositiveInt::fromInt(202),
firstName: NonEmptyStr::fromString('Bob'),
lastName: NonEmptyStr::fromString('Johnson'),
middleName: NonEmptyStr::fromString('A.'),
id: IntegerPositive::fromInt(202),
firstName: StringNonEmpty::fromString('Bob'),
lastName: StringNonEmpty::fromString('Johnson'),
middleName: StringNonEmpty::fromString('A.'),
birthDate: null,
heightM: null,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace PhpTypedValues\Code\Exception;

class ReasonableRangeDateTimeTypeException extends TypeException
class ReasonableRangeDateTimeTypeException extends DateTimeTypeException
{
}
3 changes: 2 additions & 1 deletion src/DateTime/Timestamp/TimestampMilliseconds.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public function toString(): string
$seconds = (int) $dt->format('U');
$micros = (int) $dt->format('u');

$milliseconds = ($seconds * 1000) + intdiv($micros, 1000);
// Using intdiv will throw a TypeError if $seconds is not an int, ensuring the cast is meaningful
$milliseconds = (intdiv($seconds, 1) * 1000) + intdiv($micros, 1000);

return (string) $milliseconds;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Float/Alias/NonNegativeFloat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpTypedValues\Float\Alias;

use PhpTypedValues\Float\FloatNonNegative;

/**
* @psalm-immutable
*/
readonly class NonNegativeFloat extends FloatNonNegative
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @psalm-immutable
*/
readonly class NonNegativeFloat extends FloatType
readonly class FloatNonNegative extends FloatType
{
protected float $value;

Expand Down
2 changes: 1 addition & 1 deletion src/Float/FloatBasic.php → src/Float/FloatStandard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @psalm-immutable
*/
readonly class FloatBasic extends FloatType
readonly class FloatStandard extends FloatType
{
protected float $value;

Expand Down
14 changes: 14 additions & 0 deletions src/Integer/Alias/Id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpTypedValues\Integer\Alias;

use PhpTypedValues\Integer\IntegerPositive;

/**
* @psalm-immutable
*/
readonly class Id extends IntegerPositive
{
}
14 changes: 14 additions & 0 deletions src/Integer/Alias/NonNegativeInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpTypedValues\Integer\Alias;

use PhpTypedValues\Integer\IntegerNonNegative;

/**
* @psalm-immutable
*/
readonly class NonNegativeInt extends IntegerNonNegative
{
}
14 changes: 14 additions & 0 deletions src/Integer/Alias/PositiveInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpTypedValues\Integer\Alias;

use PhpTypedValues\Integer\IntegerPositive;

/**
* @psalm-immutable
*/
readonly class PositiveInt extends IntegerPositive
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @psalm-immutable
*/
readonly class NonNegativeInt extends IntType
readonly class IntegerNonNegative extends IntType
{
/** @var non-negative-int */
protected int $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @psalm-immutable
*/
readonly class PositiveInt extends IntType
readonly class IntegerPositive extends IntType
{
/** @var positive-int */
protected int $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @psalm-immutable
*/
readonly class IntegerBasic extends IntType
readonly class IntegerStandard extends IntType
{
protected int $value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @psalm-immutable
*/
readonly class WeekDayInt extends IntType
readonly class IntegerWeekDay extends IntType
{
/** @var int<1, 7> */
protected int $value;
Expand Down
14 changes: 14 additions & 0 deletions src/String/Alias/NonEmptyStr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpTypedValues\String\Alias;

use PhpTypedValues\String\StringNonEmpty;

/**
* @psalm-immutable
*/
readonly class NonEmptyStr extends StringNonEmpty
{
}
41 changes: 41 additions & 0 deletions src/String/DB/StringVarChar255.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace PhpTypedValues\String\DB;

use PhpTypedValues\Code\Exception\StringTypeException;
use PhpTypedValues\Code\String\StrType;

/**
* @psalm-immutable
*/
readonly class StringVarChar255 extends StrType
{
protected string $value;

/**
* @throws StringTypeException
*/
public function __construct(string $value)
{
if (mb_strlen($value) > 255) {
throw new StringTypeException('String is too long, max 255 chars allowed');
}

$this->value = $value;
}

/**
* @throws StringTypeException
*/
public static function fromString(string $value): static
{
return new static($value);
}

public function value(): string
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @psalm-immutable
*/
readonly class NonEmptyStr extends StrType
readonly class StringNonEmpty extends StrType
{
/** @var non-empty-string */
protected string $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @psalm-immutable
*/
readonly class StringBasic extends StrType
readonly class StringStandard extends StrType
{
protected string $value;

Expand Down
Loading