https://github.com/bermudaphp/reflection-type-matcher
https://github.com/bermudaphp/reflection-type-matcher
reflection reflection-api
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bermudaphp/reflection-type-matcher
- Owner: bermudaphp
- License: mit
- Created: 2023-03-10T19:59:03.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-05T13:50:41.000Z (almost 2 years ago)
- Last Synced: 2024-11-09T21:18:27.297Z (about 1 year ago)
- Topics: reflection, reflection-api
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeMatcher
**TypeMatcher** is a robust PHP class for dynamic type validation using the Reflection API. It helps you determine whether a given variable conforms to a specified reflected type, supporting named types, union types, and intersection types. The class is designed for PHP 8 and later, taking advantage of modern language features like match expressions and strict type comparisons.
## Features
- **Named Type Matching**: Checks if a variable is an instance of a specified class or a built-in type.
- **Union Type Matching**: Validates the variable against multiple possible types, ensuring it matches at least one.
- **Intersection Type Matching**: Ensures the variable conforms to every type in a given intersection.
- **Strict Comparison Option**: Use strict checks for numeric and string types or allow flexible comparisons when needed.
- **Lightweight & Standalone**: Easily integrate this class into your projects without the overhead of a larger framework.
# Install
```bash
composer require bermudaphp/reflection-type-matcher
```
# Usage
```php
$reflector = new ReflectionFunction(static fn(int $a, int $b) => $a + $b);
$param = $reflector->getParameters()[0];
TypeMatcher::match($param->getType(), '22'); // true
TypeMatcher::match($param->getType(), 22); // true
TypeMatcher::match($param->getType(), '22', true); // false
$reflector = new ReflectionFunction(static fn(A&B $arg) => $arg);
$param = $reflector->getParameters()[0];
TypeMatcher::match($param->getType(), new class implements A, B {}) // true
TypeMatcher::match($param->getType(), new StdClass) // false
```