Skip to content

Commit 4b7cc45

Browse files
authored
Merge pull request #5 from adrigar94/develop
new release 0.2.0
2 parents e2e4203 + e123008 commit 4b7cc45

File tree

1 file changed

+83
-4
lines changed

1 file changed

+83
-4
lines changed

src/wizardPhpSkeletons.ts

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ async function wizardFileType(): Promise<string> {
4343
const acceptedTypes = [
4444
"class",
4545
"interface",
46-
// "trait",
47-
// "enum",
48-
// "value object (immutable class)"
46+
"enum",
47+
"trait"
4948
];
5049
const type = await vscode.window.showQuickPick(
5150
acceptedTypes,
@@ -78,7 +77,13 @@ async function generatePhpSkeleton(type: string, fileName: string, namespace: st
7877
if (type === "interface") {
7978
return await generatePhpInterfaceSkeleton(fileName, namespace);
8079
}
81-
return "## TODO";
80+
if (type === "enum") {
81+
return await generatePhpEnumSkeleton(fileName, namespace);
82+
}
83+
if (type === "trait") {
84+
return await generatePhpTraitSkeleton(fileName, namespace);
85+
}
86+
return "## not avaible";
8287
}
8388

8489
async function generatePhpClassSkeleton(className: string, namespace: string): Promise<string> {
@@ -90,6 +95,7 @@ async function generatePhpClassSkeleton(className: string, namespace: string): P
9095
}
9196

9297
let gettersAndSetters = "";
98+
let equalsCondition = [];
9399
for (const property of properties) {
94100
const capitalizedName = capitalize(property.name);
95101
gettersAndSetters += `
@@ -101,7 +107,16 @@ async function generatePhpClassSkeleton(className: string, namespace: string): P
101107
{
102108
$this->${property.name} = $${property.name};
103109
}`;
110+
equalsCondition.push(`$this->get${capitalizedName}() == $toCompare->get${capitalizedName}()`);
111+
}
112+
113+
114+
const equalsMethod = equalsCondition.length ? `
115+
public function equals(self $toCompare): boolean
116+
{
117+
return ${equalsCondition.join('\n AND ')};
104118
}
119+
` : '';
105120

106121
const skeleton = `<?php
107122
@@ -116,6 +131,8 @@ class ${className}
116131
}
117132
118133
${gettersAndSetters}
134+
135+
${equalsMethod}
119136
}`;
120137

121138
return skeleton;
@@ -226,4 +243,66 @@ async function wizardInterfaceMethods(): Promise<Array<{ name: string, returnTyp
226243
}
227244

228245
return methods;
246+
}
247+
248+
async function generatePhpEnumSkeleton(enumName: string, namespace: string): Promise<string> {
249+
const cases = await wizardCasesEnum();
250+
251+
let declareCases: Array<string> = [];
252+
for (const caseDeclaration of cases) {
253+
declareCases.push(` case ${caseDeclaration.toUpperCase().replace(' ', '_')};`);
254+
}
255+
256+
return `<?php
257+
258+
declare(strict_types=1);
259+
260+
namespace ${namespace};
261+
262+
enum ${enumName}
263+
{
264+
${declareCases.join('\n')}
265+
}`;
266+
}
267+
268+
async function wizardCasesEnum(): Promise<Array<string>> {
269+
let cases = [];
270+
let caseName = await vscode.window.showInputBox({
271+
prompt: "Enter a case name (press 'Cancel' or leave empty to finish)"
272+
});
273+
while (caseName) {
274+
cases.push(caseName);
275+
caseName = await vscode.window.showInputBox({
276+
prompt: "Enter another case name (press 'Cancel' or leave empty to finish)"
277+
});
278+
}
279+
return cases;
280+
}
281+
282+
async function generatePhpTraitSkeleton(traitName: string, namespace: string): Promise<string> {
283+
const methods = await wizardInterfaceMethods();
284+
285+
let declareMethods = [];
286+
for (const method of methods) {
287+
let paramsMethod: Array<string> = [];
288+
for (const param of method.params) {
289+
paramsMethod.push(`${param.type} $${param.name}`);
290+
}
291+
declareMethods.push(` public function ${method.name}(${paramsMethod.join(', ')}): ${method.returnType}
292+
{
293+
## TODO
294+
}`);
295+
}
296+
297+
return `<?php
298+
299+
declare(strict_types=1);
300+
301+
namespace ${namespace};
302+
303+
interface ${traitName}
304+
{
305+
${declareMethods.join("\n\n")}
306+
}
307+
`;
229308
}

0 commit comments

Comments
 (0)