zrnik / php-attribute-reflection
封装反射,便于轻松获取属性实例的辅助方法。
v0.0.1
2024-03-09 17:23 UTC
Requires
- php: ^8
Requires (Dev)
- phpstan/phpstan: ^1
- phpunit/phpunit: ^9
- roave/security-advisories: dev-latest
- zrnik/phpunit-exceptions: ^0.1
This package is auto-updated.
Last update: 2024-09-09 18:59:06 UTC
README
完全测试,符合phpstan级别9。
安装
composer require zrnik/php-attribtue-reflection
用法及该库存在的理由。
我经常在枚举实例上使用属性。这是一种在实例上放置元数据的好方法。让我们看看这个例子
<?php namespace Zrnik\Example; use ReflectionClass; use RuntimeException; enum CaseToSolve { #[AttributeToFind('AnyParameter')] case FirstCase; #[AttributeToFind('DifferentParameter')] #[AnotherAttribute('WhateverIsHere')] case SecondCase; case ThirdCase; public function getParameter(): string { $reflection = new ReflectionClass(self::class); $caseReflection = $reflection->getReflectionConstant($this->name); if($caseReflection === false) { throw new RuntimeException('case not found'); } foreach ($caseReflection->getAttributes() as $reflectionAttribute) { if ($reflectionAttribute->getName() === AttributeToFind::class) { /** @var AttributeToFind $attributeToFindInstance */ $attributeToFindInstance = $reflectionAttribute->newInstance(); return $attributeToFindInstance->customValue; } } throw new RuntimeException( sprintf( 'attribute "%s" not found on "%s"!', AttributeToFind::class, $this->name ) ); } }
你可能知道它打算做什么
CaseToSolve::FirstCase->getParameter(); // 'AnyParameter' CaseToSolve::SecondCase->getParameter(); // 'DifferentParameter' CaseToSolve::ThirdCase->getParameter(); // RuntimeException
这就是这个库的作用,它只返回属性值。现在让我们看看使用这个库的代码将如何工作
<?php namespace Zrnik\Example; use Zrnik\AttributeReflection\AttributeReflection; use Zrnik\AttributeReflection\AttributeReflectionException; enum SolvedCase { #[AttributeToFind('AnyParameter')] case FirstCase; #[AttributeToFind('DifferentParameter')] #[AnotherAttribute('WhateverIsHere')] case SecondCase; case ThirdCase; /** * @return string * @throws AttributeReflectionException */ public function getParameter(): string { return AttributeReflection::getClassConstantAttribute( AttributeToFind::class, self::class, $this->name )->customValue; } }
getParameter
方法要好得多,不是吗?工作方式相同
SolvedCase::FirstCase->getParameter(); // 'AnyParameter' SolvedCase::SecondCase->getParameter(); // 'DifferentParameter' SolvedCase::ThirdCase->getParameter(); // \Zrnik\AttributeReflection\AttributeReflectionException