spatie/better-types

对处理联合类型和命名类型的改进抽象。

资助包维护!
spatie

0.2.0 2024-03-08 11:36 UTC

This package is auto-updated.

Last update: 2024-09-08 12:38:56 UTC


README

检查反射类型或方法是否接受给定输入

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

支持我们

我们投入大量资源来创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从家乡寄来明信片,提及您正在使用我们哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将所有收到的明信片发布在 我们的虚拟明信片墙上

安装

您可以通过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)。有关更多信息,请参阅 许可证文件