承接 welcomattic/has-attributes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-20