-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
50 lines (44 loc) · 1.2 KB
/
functions.php
File metadata and controls
50 lines (44 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/*
* Copyright 2025 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace CloudCreativity\Modules\Toolkit;
use BackedEnum;
use UnitEnum;
if (!function_exists(__NAMESPACE__ . '\enum_value')) {
/**
* Return a scalar value for an enum.
*
* @param UnitEnum|string|int $value
* @return string|int
*/
function enum_value(UnitEnum|string|int $value): string|int
{
return match (true) {
$value instanceof BackedEnum => $value->value,
$value instanceof UnitEnum => $value->name,
default => $value,
};
}
}
if (!function_exists(__NAMESPACE__ . '\enum_string')) {
/**
* Return a string value for an enum.
*
* @param UnitEnum|string $value
* @return string
*/
function enum_string(UnitEnum|string $value): string
{
return match (true) {
$value instanceof BackedEnum && is_string($value->value) => $value->value,
$value instanceof UnitEnum => $value->name,
default => $value,
};
}
}