This package provides the ability to work with types as objects.
Available types:
- Integer
- String
- Float
- Boolean
- Array
composer require elegant-php/types
final class AuthorizedUser implements User
{
public function __construct(
private readonly StringType $name
) { }
}
$user = new AuthorizedUser(new DefaultString('my user'));
final class UserName extends StringType
{
public function __construct(
private readonly string $name
) { }
public function value(): string
{
return strtolower($this->name);
}
}
$first = new DefaultString('first');
$second = new DefaultString('first');
var_dump($first->equals($second));
// bool(false)
$id = new StringAsInteger('123');
var_dump($id->value());
// int(123)