-
Notifications
You must be signed in to change notification settings - Fork 3
/
EntityTemplate.php
62 lines (53 loc) · 1.76 KB
/
EntityTemplate.php
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
<?php
/**
* This template expects the input `$tplData`, which should be the name of the JSON data-file.
* The file specifies:
*
* - namespace: string
* - class: string
* - fields: array (string $fieldName => string $type)
*/
namespace CCL\Tests\Examples;
$json = json_decode(\CCL::cat($tplData));
$funcName = function ($action, $fieldName) {
return $action . ucfirst($fieldName);
};
echo "<" . "?php\n";
printf("namespace %s;\n\n", $json->namespace);
printf("class %s\n{\n", $json->class);
//list ($firstField) = array_shift(array_keys($json->fields));
$first = true;
foreach ($json->fields as $fieldName => $fieldType) {
if ($first) {
$first = false;
} else {
echo "\n";
}
printf(" /**\n");
printf(" * @var %s\n", $fieldType);
printf(" */\n");
printf(" protected \$%s;\n", $fieldName);
}
foreach ($json->fields as $fieldName => $fieldType) {
$literalType = strpos($fieldType, '[]') === false ? $fieldType : 'array';
printf("\n");
printf(" /**\n");
printf(" * @return %s\n", $fieldType);
printf(" */\n");
printf(" public function %s(): %s\n", $funcName('get', $fieldName), $literalType);
printf(" {\n");
printf(" return \$this->%s;\n", $fieldName);
printf(" }\n");
}
foreach ($json->fields as $fieldName => $fieldType) {
$literalType = strpos($fieldType, '[]') === false ? $fieldType : 'array';
printf("\n");
printf(" /**\n");
printf(" * @return %s\n", $fieldType);
printf(" */\n");
printf(" public function %s(%s \$%s)\n", $funcName('set', $fieldName), $literalType, $fieldName);
printf(" {\n");
printf(" \$this->%s = \$%s;\n", $fieldName, $fieldName);
printf(" }\n");
}
printf("}\n", $json->class);