laravel
🚀
note2self
🚀
Enums and getting name from value
Still trying to learn how to use these well.
<?php
namespace App\InvoiceSystems;
enum Status : string
{
case Open = "1";
case PastDue = "2";
case Paid = "6";
case Archived = "3";
public static function getName(string $value) : string {
$class = new \ReflectionClass(__CLASS__);
$constants = $class->getConstants();
$name = collect($constants)->first(function($item) use ($value) {
return $item->value === $value;
});
if($name === false) {
throw new \InvalidArgumentException("No enum constant with value '$value'");
}
return str($name->name)->headline();
}
}