reedware/is-attribute 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

reedware/is-attribute

最新稳定版本:v1.0.0

Composer 安装命令:

composer require reedware/is-attribute

包简介

Adds truth test helper for checking if class is an attribute.

README 文档

README

Automated Tests Coding Standards Code Coverage Static Analysis Latest Stable Version

This package adds a truth test helper for checking if class is an attribute.

Introduction

PHP Attributes were introduced in PHP 8.0. From the documentation, you can see that Attributes are defined similar to classes:

#[Attribute]
class SetUp
{
    //
}

However, if you're handed the name of a class, there's no good way to know if that class is a PHP Attribute or not. This is where the is_attribute() method, as defined by this package, comes in.

Installation

Install this package using Composer:

composer require reedware/is-attribute

Usage

function is_attribute(string|object|null $class, ?int $target = null, int $match = TARGET_MATCH_EQUALS): bool

Class Argument

Let's start with the basics. For more use-cases, you'll only need to pass one parameter to is_attribute(), being the class itself.

Here's an example of that:

echo is_attribute(SetUp::class); // true
echo is_attribute(CopyFile::class); // false

Target Argument

If you care what targets the attributes support, this is where an optional second parameter comes in.

For example, you can define an attribute like so:

#[Attribute(Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY)]
class Serialize
{
    //
}

This defines a Serialize attribute that can applied to a class or property.

To check if an attribute can be applied to a class or property, you can pass in targets as the second parameter to is_attribute:

echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY); // true

Note that checking one or the other will return false:

echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT); // false
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY); // false

When the second argument is not specified (e.g. null), the specified class must simply be an Attribute, regardless of target. This is different from passing in Attribute::TARGET_ALL as the second argument, which will require the attribute to specify all targets (which is the default).

echo is_attribute(Serialize::class, Attribute::TARGET_ALL); // false

This is because the second parameter must be an exact match to the attribute.

Match Argument

If you don't want to do an exact match, you can use the optional third parameter.

TARGET_MATCH_EQUALS

This is the default value of the third argument, and exhibits the behavior already described above.

TARGET_MATCH_INCLUDES

This match setting requires ALL of the provided targets to be included (e.g. and/conjunction).

echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT, TARGET_MATCH_INCLUDES); // true
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY, TARGET_MATCH_INCLUDES); // true
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER, TARGET_MATCH_INCLUDES); // false

TARGET_MATCH_ANY

This match setting requires ANY of the provided targets to be included (e.g. or/disjunction).

echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT, TARGET_MATCH_INCLUDES); // true
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY, TARGET_MATCH_INCLUDES); // true
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER, TARGET_MATCH_INCLUDES); // true

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固