You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+121-1Lines changed: 121 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,7 @@ A collection of enum helpers for PHP.
7
7
-[`Values`](#values)
8
8
-[`Options`](#options)
9
9
-[`From`](#from)
10
+
-[`Metadata`](#metadata)
10
11
11
12
You can read more about the idea on [Twitter](https://twitter.com/archtechx/status/1495158228757270528). I originally wanted to include the `InvokableCases` helper in [`archtechx/helpers`](https://github.com/archtechx/helpers), but it makes more sense to make it a separate dependency and use it *inside* the other package.
Each meta property (= attribute used on a case) needs to exist as a class.
291
+
```php
292
+
#[Attribute]
293
+
class Color extends MetaProperty {}
294
+
295
+
#[Attribute]
296
+
class Description extends MetaProperty {}
297
+
```
298
+
299
+
Inside the class, you can customize a few things. For instance, you may want to use a different method name than the one derived from the class name (`Description` becomes `description()` by default). To do that, override the `method()` method on the meta property:
300
+
```php
301
+
#[Attribute]
302
+
class Description extends MetaProperty
303
+
{
304
+
public static function method(): string
305
+
{
306
+
return 'note';
307
+
}
308
+
}
309
+
```
310
+
311
+
With the code above, the description of a case will be accessible as `TaskStatus::INCOMPLETE->note()`.
312
+
313
+
Another thing you can customize is the passed value. For instance, to wrap a color name like `text-{$color}-500`, you'd add the following `transform()` method:
314
+
```php
315
+
#[Attribute]
316
+
class Color extends MetaProperty
317
+
{
318
+
protected function transform(mixed $value): mixed
319
+
{
320
+
return "text-{$color}-500";
321
+
}
322
+
}
323
+
```
324
+
325
+
And now the returned color will be correctly transformed:
0 commit comments