princejohnsantillan/reflect 问题修复 & 功能扩展

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

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

princejohnsantillan/reflect

最新稳定版本:1.1.0

Composer 安装命令:

composer require princejohnsantillan/reflect

包简介

Clearer API for PHP Attributes and Reflection.

README 文档

README

Reflect

Requirement

PHP 8.1 or higher

Installation

composer require princejohnsantillan/reflect

Example

BEFORE:

enum Plan: string{
    case FREE = 'free';
    case HOBBY = 'hobby';
    case PRO = 'professional';
    case TEAM = 'team';
    case ENTERPRISE = 'enterprise';
    
    public function price(): int {
        return match($this){
            static::FREE => 0,
            static::HOBBY => 10,
            static::PRO => 20,
            static::TEAM => 50,
            static::ENTERPRISE = 200
        };
    }
    
    public function color(): string {
        return match($this){
            static::FREE => 'yellow',
            static::HOBBY => 'orange',
            static::PRO => 'blue',
            static::TEAM => 'silver',
            static::ENTERPRISE = 'gold'
        };
    }
}

AFTER:

use PrinceJohn\Reflect\Traits\HasEnumTarget;

#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
class Price{
    use HasEnumTarget;
    
    public function __construct(public int $price) {}
}
use PrinceJohn\Reflect\Traits\HasEnumTarget;

#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
class Color{
    use HasEnumTarget;
    
    public function __construct(public string $color) {}
}
use PrinceJohn\Reflect\Enum\Reflect;

enum Plan: string{
    #[Price(0)]
    #[Color('yellow')]
    case FREE = 'free';
    
    #[Price(10)]
    #[Color('orange')]
    case HOBBY = 'hobby';
    
    #[Price(20)]
    #[Color('blue')]
    case PRO = 'professional';
    
    #[Price(50)]
    #[Color('silver')]
    case TEAM = 'team';
    
    #[Price(200)]
    #[Color('gold')]
    case ENTERPRISE = 'enterprise';
    
    public function price(): int {
        // Demonstrating usage via the Reflect class        
        return Reflect::on($this)
            ->getAttributeInstance(Price::class)
            ->price;            
    }
    
    public function color(): string {
        // Demonstrating usage via the HasEnumTarget trait
        return Color::onEnum($this)->color;
    }
}

Note

Alternatively, just return the attribute class, that way you can have more functionality at your disposal.

统计信息

  • 总下载量: 10.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-23