@@ -5,3 +5,108 @@ Fully tested, phpstan level 9 compliant.
55### Installation:
66
77> composer require zrnik/php-attribtue-reflection
8+
9+ ### Usage & Reason why this library exists.
10+
11+ I use attributes on enum cases a lot. It's a good way to put
12+ metadata on the cases. Let's look at this example:
13+
14+ ``` php
15+ <?php
16+
17+ namespace Zrnik\Example;
18+
19+ use ReflectionClass;
20+ use RuntimeException;
21+
22+ enum CaseToSolve
23+ {
24+ #[AttributeToFind('AnyParameter')]
25+ case FirstCase;
26+
27+ #[AttributeToFind('DifferentParameter')]
28+ #[AnotherAttribute('WhateverIsHere')]
29+ case SecondCase;
30+
31+ case ThirdCase;
32+
33+ public function getParameter(): string
34+ {
35+ $reflection = new ReflectionClass(self::class);
36+ $caseReflection = $reflection->getReflectionConstant($this->name);
37+
38+ if($caseReflection === false) {
39+ throw new RuntimeException('case not found');
40+ }
41+
42+ foreach ($caseReflection->getAttributes() as $reflectionAttribute) {
43+ if ($reflectionAttribute->getName() === AttributeToFind::class) {
44+ /** @var AttributeToFind $attributeToFindInstance */
45+ $attributeToFindInstance = $reflectionAttribute->newInstance();
46+ return $attributeToFindInstance->customValue;
47+ }
48+ }
49+
50+ throw new RuntimeException(
51+ sprintf(
52+ 'attribute "%s" not found on "%s"!',
53+ AttributeToFind::class,
54+ $this->name
55+ )
56+ );
57+ }
58+ }
59+ ```
60+
61+ You probably know what its meant to do:
62+
63+ ``` php
64+ CaseToSolve::FirstCase->getParameter(); // 'AnyParameter'
65+ CaseToSolve::SecondCase->getParameter(); // 'DifferentParameter'
66+ CaseToSolve::ThirdCase->getParameter(); // RuntimeException
67+ ```
68+
69+ This is what this library does, it just returns the attribute value.
70+ Now let's see how this code would work with this library:
71+
72+ ``` php
73+ <?php
74+
75+ namespace Zrnik\Example;
76+
77+ use Zrnik\AttributeReflection\AttributeReflection;
78+ use Zrnik\AttributeReflection\AttributeReflectionException;
79+
80+ enum SolvedCase
81+ {
82+ #[AttributeToFind('AnyParameter')]
83+ case FirstCase;
84+
85+ #[AttributeToFind('DifferentParameter')]
86+ #[AnotherAttribute('WhateverIsHere')]
87+ case SecondCase;
88+
89+ case ThirdCase;
90+
91+ /**
92+ * @return string
93+ * @throws AttributeReflectionException
94+ */
95+ public function getParameter(): string
96+ {
97+ return AttributeReflection::getClassConstantAttribute(
98+ AttributeToFind::class,
99+ self::class,
100+ $this->name
101+ )->customValue;
102+ }
103+ }
104+ ```
105+
106+ The ` getParameter ` method is much better, isn't it? Works the same:
107+
108+ ``` php
109+ SolvedCase::FirstCase->getParameter(); // 'AnyParameter'
110+ SolvedCase::SecondCase->getParameter(); // 'DifferentParameter'
111+ SolvedCase::ThirdCase->getParameter(); // \Zrnik\AttributeReflection\AttributeReflectionException
112+ ```
0 commit comments