welcomattic/has-attributes
最新稳定版本:v1.1.0
Composer 安装命令:
composer require welcomattic/has-attributes
包简介
Check if a class, a method or a property has an attribute
关键字:
README 文档
README
For years, PHP has made it easy to check whether an object is an instance of a given class. However, it's not as simple to check that an object's class has Attribute.
This package provides some functions to make it easier to check whether an object's class has a given Attribute.
Installation
$ composer require welcomattic/has-attribute
Usage
use Welcomattic\HasAttribute\HasAttributeMode; #[\Attribute(\Attribute::TARGET_CLASS)] class ClassAttribute {} #[\Attribute(\Attribute::TARGET_CLASS)] class SecondClassAttribute {} #[\Attribute(\Attribute::TARGET_CLASS)] class ThirdClassAttribute {} #[ClassAttribute] #[SecondClassAttribute] class Foo {} class_has_attribute(Foo::class, ClassAttribute::class); // true class_has_attribute(Foo::class, ThirdClassAttribute::class); // false class_has_attribute(Foo::class, [ClassAttribute::class, SecondClassAttribute::class]); // true class_has_attribute(Foo::class, [ClassAttribute::class, SecondClassAttribute::class], HasAttributeMode::ATTRIBUTES_ALL_OF); // true (default mode) class_has_attribute(Foo::class, [ClassAttribute::class, SecondClassAttribute::class], HasAttributeMode::ATTRIBUTES_ANY_OF); // true class_has_attribute(Foo::class, [ClassAttribute::class, SecondClassAttribute::class], HasAttributeMode::ATTRIBUTES_ONE_OF); // false
You can also check on object methods or properties:
#[\Attribute(\Attribute::TARGET_METHOD)] class MethodAttribute {} #[\Attribute(\Attribute::TARGET_PROPERTY)] class PropertyAttribute {} class Foo { #[MethodAttribute] public function bar() {} #[PropertyAttribute] public $baz; } method_has_attribute(Foo::class, MethodAttribute::class, 'bar'); // true property_has_attribute(Foo::class, PropertyAttribute::class, 'baz'); // true
It also supports Attribute inheritance:
#[\Attribute(\Attribute::TARGET_CLASS)] class ClassAttributeInterface {} #[\Attribute(\Attribute::TARGET_CLASS)] class ChildClassAttribute extends ClassAttributeInterface {} #[ChildClassAttribute] class Foo {} class_has_attribute(Baz::class, [ClassAttributeInterface::class]); // true
Credits
This package is based on an idea from lyrixx, and was implemented by welcomattic and Korbeil.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-20