-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-pre-8.1-enum.php
More file actions
136 lines (116 loc) · 3.06 KB
/
php-pre-8.1-enum.php
File metadata and controls
136 lines (116 loc) · 3.06 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
declare(strict_types=1);
/**
* Pre–PHP 8.1 compatible enum-like value object for plan types.
*/
final class PlanType
{
public const FREE = 1;
public const PRO = 2;
/**
* The legacy integer code for this plan type.
*/
private int $value;
/**
* Private constructor to force use of named constructors.
*/
private function __construct(int $value)
{
$this->value = $value;
}
/**
* Create a FREE plan type instance.
*/
public static function free(): self
{
return new self(self::FREE);
}
/**
* Create a PRO plan type instance.
*/
public static function pro(): self
{
return new self(self::PRO);
}
/**
* Determine whether this plan type represents a free plan.
*
* @return bool True if the plan type is FREE.
*/
public function isFree(): bool
{
return $this->value === self::FREE;
}
/**
* Determine whether this plan type represents a pro plan.
*
* @return bool True if the plan type is PRO.
*/
public function isPro(): bool
{
return $this->value === self::PRO;
}
/**
* Create a plan type instance from a string.
*
* The comparison is case-insensitive.
*
* @param string $planType The string plan type (e.g. "FREE", "PRO").
*
* @throws \InvalidArgumentException If the string does not map to a valid plan type.
*/
public static function fromString(string $planType): self
{
$planType = strtoupper($planType);
switch ($planType) {
case 'FREE':
return new self(self::FREE);
case 'PRO':
return new self(self::PRO);
default:
throw new \InvalidArgumentException("Invalid plan type: $planType");
}
}
/**
* Create a plan type instance from the integer-based plan type code.
*
* @param int $planTypeCode The legacy integer plan type code.
*
* @throws \InvalidArgumentException If the code does not map to a valid plan type.
*/
public static function fromCode(int $planTypeCode): self
{
switch ($planTypeCode) {
case self::FREE:
return new self(self::FREE);
case self::PRO:
return new self(self::PRO);
default:
throw new \InvalidArgumentException("Invalid plan type code: $planTypeCode");
}
}
/**
* Return the integer-based plan type code for this instance.
*/
public function toCode(): int
{
return $this->value;
}
/**
* Return the string name for this plan type (e.g. "FREE").
*
*
* @throws \LogicException
*/
public function name(): string
{
switch ($this->value) {
case self::FREE:
return 'FREE';
case self::PRO:
return 'PRO';
default:
throw new \LogicException("Unknown plan type value: {$this->value}");
}
}
}