Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c722ed

Browse files
init
0 parents commit 5c722ed

File tree

2 files changed

+301
-0
lines changed

2 files changed

+301
-0
lines changed

‎README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# PHP Enum generator
2+
3+
Converts list (text with values separated by new lines) to [https://wiki.php.net/rfc/enumerations](PHP enum). You can choose between basic or int/string backed enumerations.
4+
5+
Just reminder - PHP support's native enums from version 8.1!
6+
7+
## Why?
8+
9+
I had to do a lot of enums. For something with couple of values it's not big of a problem, but imagine for example list of countries or currencies 😱 Of course typing this by hand would be too boring. So I decided to write simple Regex for this task, and one thing to another - ended up with this tiny simple app instead. Maybe it will help someone else too, cheers! 🥂
10+
11+
## Usage and contribute
12+
13+
It's written using Vue, but there is no bundlers, depedencies etc. Just single `index.html` file to keep things simple.
14+
So open this file in browser and you are ready to go, or edit it if you need to (PRs are welcomed).
15+
16+
17+
18+
License: MIT

‎index.html‎

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>PHP Enum Generator</title>
7+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
8+
<script src="https://unpkg.com/vue@3.2.26"></script>
9+
</head>
10+
<body>
11+
<div id="app">
12+
<div class="container">
13+
<div class="row my-5">
14+
<div class="col">
15+
<label class="fw-bold">
16+
Enum names (one per line):
17+
</label>
18+
<textarea
19+
class="form-control"
20+
v-model="input"
21+
rows="10"></textarea>
22+
<span class="text-muted small">
23+
Separated by lines.
24+
</span>
25+
</div>
26+
<div class="col">
27+
<label class="fw-bold">
28+
PHP file template:
29+
</label>
30+
<textarea
31+
class="form-control"
32+
v-model="template"
33+
rows="10"></textarea>
34+
<span class="text-muted small">
35+
Can be empty to generate just cases.
36+
</span>
37+
</div>
38+
<div class="col">
39+
<label class="fw-bold">
40+
PHP file result:
41+
</label>
42+
<textarea
43+
:value="output"
44+
class="form-control"
45+
rows="10"></textarea>
46+
</div>
47+
</div>
48+
<div class="row my-5">
49+
<div class="col">
50+
<label class="mb-2 fw-bold">
51+
Options:
52+
</label>
53+
<div class="row">
54+
<div class="col-auto">
55+
<div class="form-check">
56+
<label class="form-check-label">
57+
<input
58+
class="form-check-input"
59+
type="radio"
60+
v-model="enumType"
61+
value="basic"
62+
>
63+
Basic enums
64+
<span class="text-muted small">
65+
case NAME;
66+
</span>
67+
</label>
68+
</div>
69+
<div class="form-check">
70+
<label class="form-check-label" >
71+
<input
72+
class="form-check-input"
73+
type="radio"
74+
v-model="enumType"
75+
value="string"
76+
>
77+
Backed string enums
78+
<span class="text-muted small">
79+
case NAME = 'VALUE';
80+
</span>
81+
</label>
82+
</div>
83+
<div class="form-check">
84+
<label class="form-check-label" >
85+
<input
86+
class="form-check-input"
87+
type="radio"
88+
v-model="enumType"
89+
value="int"
90+
>
91+
Backed integer enums
92+
<span class="text-muted small">
93+
case NAME = 10;
94+
</span>
95+
</label>
96+
</div>
97+
</div>
98+
<div class="col-auto">
99+
<div class="form-check">
100+
<input
101+
class="form-check-input"
102+
type="radio"
103+
v-model="namesTransform"
104+
:value="null"
105+
>
106+
<label class="form-check-label">
107+
Do not transform names
108+
</label>
109+
</div>
110+
<div class="form-check">
111+
<input
112+
class="form-check-input"
113+
type="radio"
114+
v-model="namesTransform"
115+
value="uppercase"
116+
>
117+
<label class="form-check-label">
118+
Uppercase names
119+
</label>
120+
</div>
121+
<div class="form-check">
122+
<input
123+
class="form-check-input"
124+
type="radio"
125+
v-model="namesTransform"
126+
value="lowercase"
127+
>
128+
<label class="form-check-label">
129+
Lowercase names
130+
</label>
131+
</div>
132+
</div>
133+
<div
134+
v-if="enumType === 'string'"
135+
class="col-auto"
136+
>
137+
<div class="form-check">
138+
<input
139+
class="form-check-input"
140+
type="radio"
141+
v-model="valuesTransform"
142+
:value="null"
143+
>
144+
<label class="form-check-label">
145+
Do not transform values
146+
</label>
147+
</div>
148+
<div class="form-check">
149+
<input
150+
class="form-check-input"
151+
type="radio"
152+
v-model="valuesTransform"
153+
value="uppercase"
154+
>
155+
<label class="form-check-label">
156+
Uppercase values
157+
</label>
158+
</div>
159+
<div class="form-check">
160+
<input
161+
class="form-check-input"
162+
type="radio"
163+
v-model="valuesTransform"
164+
value="lowercase"
165+
>
166+
<label class="form-check-label">
167+
Lowercase values
168+
</label>
169+
</div>
170+
</div>
171+
<div
172+
v-if="enumType === 'int'"
173+
class="col-auto"
174+
>
175+
<label>Value starts from:</label>
176+
<input
177+
type="number"
178+
v-model="backedStart"
179+
class="form-control"
180+
>
181+
</div>
182+
</div>
183+
</div>
184+
</div>
185+
<div class="row my-5">
186+
<div class="col">
187+
Source:
188+
<a href="https://github.com/chojnicki/php-enum-generator">
189+
https://github.com/chojnicki/php-enum-generator
190+
</a>
191+
</div>
192+
</div>
193+
</div>
194+
</div>
195+
<script>
196+
const { ref, createApp, computed } = Vue
197+
createApp({
198+
setup() {
199+
const count = ref(0)
200+
const input = ref('ABC\ndef\nGhi')
201+
const template = ref(`<?php
202+
namespace App\\Enums;
203+
204+
enum EnumExample
205+
{
206+
[CASES]
207+
}`)
208+
209+
const enumType = ref("string")
210+
const namesTransform = ref('uppercase')
211+
const valuesTransform = ref('lowercase')
212+
const backedStart = ref(0)
213+
214+
const output = computed(() => {
215+
const cases = input.value
216+
.trim()
217+
.split('\n')
218+
.map(generateCase)
219+
let result = template.value || '[CASES]'
220+
if (enumType.value !== 'basic') {
221+
result = result.replace(/EnumExample$/m, `$&: ${enumType.value}`)
222+
}
223+
result = result.replace(`${tabulation.value}[CASES]`, cases.join('\n'))
224+
return result
225+
})
226+
227+
const generateCase = (line, index) => {
228+
let name = line.trim()
229+
switch (namesTransform.value) {
230+
case 'uppercase':
231+
name = name.toUpperCase()
232+
break;
233+
case 'lowercase':
234+
name = name.toLowerCase();
235+
break;
236+
}
237+
238+
let value = line.trim()
239+
switch (valuesTransform.value) {
240+
case 'uppercase':
241+
value = value.toUpperCase()
242+
break;
243+
case 'lowercase':
244+
value = value.toLowerCase();
245+
break;
246+
}
247+
248+
switch (enumType.value) {
249+
case 'basic':
250+
return `${tabulation.value}case ${name};`
251+
break;
252+
case 'string':
253+
return `${tabulation.value}case ${name} = '${value}';`
254+
break;
255+
case 'int':
256+
return `${tabulation.value}case ${name} = ${index+backedStart.value};`
257+
break;
258+
}
259+
260+
}
261+
262+
const tabulation = computed(() => {
263+
const casesLine = template.value
264+
.split('\n')
265+
.find(line => line.includes('[CASES]'))
266+
return casesLine ? casesLine.split('[CASES]').shift() : ''
267+
})
268+
269+
return {
270+
input,
271+
output,
272+
template,
273+
count,
274+
enumType,
275+
namesTransform,
276+
valuesTransform,
277+
backedStart
278+
}
279+
}
280+
}).mount('#app')
281+
</script>
282+
</body>
283+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /