spatie / better-types
对处理联合类型和命名类型的改进抽象。
0.2.0
2024-03-08 11:36 UTC
Requires
- php: ^8.0
- illuminate/collections: ^8.58|^9.0|^10|^11
- symfony/polyfill-php81: ^1.23
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^0.12.99
- phpunit/phpunit: ^9.5
- spatie/ray: ^1.28
- vimeo/psalm: ^4.10
README
检查反射类型或方法是否接受给定输入
支持我们
我们投入大量资源来创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。
我们非常感谢您从家乡寄来明信片,提及您正在使用我们哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将所有收到的明信片发布在 我们的虚拟明信片墙上。
安装
您可以通过composer安装此包
composer require spatie/better-types
用法
直接使用 Type
类
function (FooInterface $foo) {} $reflectionType = … $type = new Type($reflectionType); $type->accepts(new Foo()); // true $type->accepts('invalid string'); // false
使用 Method
类
function (?FooInterface $foo, ?BarInterface $bar) {} $reflectionMethod = … $method = new Method($reflectionMethod); $method->accepts(new Foo(), new Bar()); // true $method->accepts(bar: new Bar() foo: new Foo()); // true $method->accepts(null, new Bar()); // true $method->accepts(null, null); // true $method->accepts('string', 1); // false $method->accepts(new Foo()); // false, you can't omit values
使用 Handlers
确定哪些方法接受给定的输入集
class Foo { public function acceptsString(string $a) {} public function acceptsStringToo(string $a) {} public function acceptsInt(int $a) {} } $reflectionClass = … $handlers = new Handlers($reflectionClass); $handlers->accepts('string')->all(); // ['acceptsString', 'acceptsStringToo'] $handlers->accepts(1)->first(); // 'acceptsInt'
使用 Attributes
类通过流畅的API查找和实例化属性
Attributes::new(AttributesTestClass::class) ->instanceOf(AttributesTestAttribute::class) ->first();
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅 我们的安全策略。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可证文件。