1. Go to this page and download the library: Download stryxx/strict-types library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stryxx / strict-types example snippets
use Stryxx\StrictTypes\Cast;
use Stryxx\StrictTypes\Type;
$name = Type::nonEmptyString($data['name'] ?? null);
$userId = Cast::int($data['id'] ?? null);
$result = $connection->executeQuery('SELECT COUNT(*) FROM users');
$value = $result->fetchOne();
if (false === $value) {
throw new UnexpectedValueException('Count query returned no row.');
}
$count = Cast::int($value);
$result = $connection->executeQuery(
'SELECT id, name FROM users WHERE email = :email',
['email' => $email],
);
$row = $result->fetchAssociative();
if (false === $row) {
return null;
}
$user = new User(
Cast::int($row['id'] ?? null),
Type::nonEmptyString($row['name'] ?? null),
);
$result = $connection->executeQuery('SELECT id FROM users');
$ids = Cast::listOf(
$result->fetchFirstColumn(),
Cast::int(...),
);
use Stryxx\StrictTypes\Contract\Resolver;
/**
* @implements Resolver<User>
*/
final class UserResolver implements Resolver
{
public function resolve(mixed $value): User
{
$data = Type::array($value);
return new User(
Cast::int($data['id'] ?? null),
Type::nonEmptyString($data['name'] ?? null),
);
}
}
use Stryxx\StrictTypes\Contract\TargetTypeProvider;
/**
* @implements TargetTypeProvider<UserResponse>
*/
final class UserRequest implements TargetTypeProvider
{
public static function targetType(): string
{
return UserResponse::class;
}
}