A PHP library to ensure correctness of types providing a readable interface.
$ composer require plook/type-guard
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']),
);
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 aDateTimeImmutable
object, 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
.
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 zero0
or0.0
.
notNull($value)
Throws an exception if the value isnull
otherwise it passes the original value.
use Plook\TypeGuard\TypeGuard;
TypeGuard::instance()->timeZone('Australia/Adelaide');
TypeGuard::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
use Plook\TypeGuard\TypeGuard;
TypeGuard::instance()->dateTimeFormat(DateTimeInterface::ATOM);