elegant-php / types
PHP types as objects
1.0.0
2024-01-11 06:48 UTC
Requires
- php: >=8.1
Requires (Dev)
- editorconfig-checker/editorconfig-checker: ^10.4
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2024-11-11 08:36:41 UTC
README
This package provides the ability to work with types as objects.
Available types:
- Integer
- String
- Float
- Boolean
- Array
Installation
composer require elegant-php/types
Usage
Use predefined classes and contracts
final class AuthorizedUser implements User { public function __construct( private readonly StringType $name ) { } } $user = new AuthorizedUser(new DefaultString('my user'));
Implement type contracts
final class UserName extends StringType { public function __construct( private readonly string $name ) { } public function value(): string { return strtolower($this->name); } }
Compare types
$first = new DefaultString('first'); $second = new DefaultString('first'); var_dump($first->equals($second)); // bool(false)
Type casting
$id = new StringAsInteger('123'); var_dump($id->value()); // int(123)