nadylib / type
A library for ensuring and checking variable types
Installs: 400
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/nadylib/type
Requires
- php: >=8.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.49
- nadybot/nadystyle: ^1.0
- phpstan/phpstan: ^1.10 || ^2
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.8
README
Introduction
The type component of Nadylib is a ripped version of Psl\Type that was stripped down to be self-dependant and only has minimum requirements as well as working with PHP down to version 8.1. If you want a full framework, check out azjezz's awesome PHP Standard Library.
Nadylib\Type provides a set of functions to ensure that a given value is of a specific type at Runtime.
It aims to provide a solution for the Parse, Don't Validate problem.
Usage
use Nadylib\Type; $untrustedInput = $request->get('input'); // Turns a string-like value into a non-empty-string $trustedInput = Type\nonEmptyString()->coerce($untrustedInput); // Or assert that it's already a non-empty-string $trustedInput = Type\nonEmptyString()->assert($untrustedInput); // Or check if it's a non-empty-string $isTrustworthy = Type\nonEmptyString()->matches($untrustedInput);
Every type provided by this component is an instance of Type\TypeInterface<Tv>.
This interface provides the following methods:
matches(mixed $value): $value is Tv– Checks if the provided value is of the type.assert(mixed $value): Tv– Asserts that the provided value is of the type or throws anAssertExceptionon failure.coerce(mixed $value): Tv– Coerces the provided value into the type or throws aCoercionExceptionon failure.
Static Analysis
Your static analyzer should fully understand the types provided by this component if they support @psalm-assert. The only exception is shape() which would require a special component on the analyzers which is not available for Nadylib\Type. If you need this, use the PHP Standard Library instead.