plook / type-guard
Library to ensure correctness of types with a readable interface.
1.1.0
2026-03-13 16:08 UTC
Requires
- php: ^8.3
Requires (Dev)
- brainbits/phpcs-standard: ^8.0.0
- brainbits/phpstan-rules: ^4.0.0
- ergebnis/composer-normalize: ^2.50
- ergebnis/phpstan-rules: ^2.13.1
- phpstan/phpstan: ^2.1.40
- phpstan/phpstan-phpunit: ^2.0.16
- phpstan/phpstan-strict-rules: ^2.0.10
- phpunit/phpunit: ^12.5.14
- rector/rector: ^2.3.8
- squizlabs/php_codesniffer: ^3.13.5
- thecodingmachine/phpstan-safe-rule: ^1.4.3
- dev-main
- 1.1.0
- 1.0.1
- 1.0.0
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.0
- dev-chore/check-php-8.5
- dev-dependabot/github_actions/cycjimmy/semantic-release-action-6
- dev-dependabot/github_actions/actions/checkout-6
- dev-dependabot/github_actions/actions/cache-5
- dev-add-timezone
- dev-dependabot/composer/squizlabs/php_codesniffer-tw-3.13.2or-tw-4.0.0
This package is auto-updated.
Last update: 2026-03-13 21:20:55 UTC
README
A PHP library to ensure correctness of types providing a readable interface.
Installation
$ composer require plook/type-guard
Example
use function Plook\TypeGuard\asBool; use function Plook\TypeGuard\asDateTimeImmutable; use function Plook\TypeGuard\asFloat; use function Plook\TypeGuard\asInt; use function Plook\TypeGuard\asString; use function Plook\TypeGuard\notNull; $row = $this->fetchProjectRow(123); $project = new Project( notNull(asInt($row['id'])), notNull(asString($row['name'])), notNull(asDateTimeImmutable($row['createdAt'])), notNull(asBool($row['is_assigned'])), asDateTimeImmutable($row['closedAt']), asFloat($row['rating']), );
Provided helper functions
Ensure Types
asBool($value)Converts input value to a boolean, but passesnull.asFloat($value)Converts input value to a float, but passesnull.asInt($value)Converts input value to a int, but passesnull.asDateTimeImmutable($value)Converts input value to aDateTimeImmutableobject, but passesnull.asDateTimeZone($value)Converts input value to aDateTimeZoneobject, but passesnull.asDateTimeString($value)Converts input value to a date string including the timezone, but passesnull.asString($value)Converts input value to a string, but passesnull.
Converters
blankAsNull($value)Converts input value tonull, if it is a blank string''.falseAsNull($value)Converts input value tonull, if it is a booleanfalse.falsyAsNull($value)Converts input value tonull, if it is a falsy valuefalse,'',0, ...zeroAsNull($value)Converts input value tonull, if it is a zero0or0.0.
Assertions
notNull($value)Throws an exception if the value isnullotherwise it passes the original value.
Configuration
Setting the default target time zone of DateTimeImmutable objects
use Plook\TypeGuard\TypeGuard; TypeGuard::instance()->timeZone('Australia/Adelaide'); TypeGuard::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
Setting the default format of date time strings
use Plook\TypeGuard\TypeGuard; TypeGuard::instance()->dateTimeFormat(DateTimeInterface::ATOM);