1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\generator; |
3
|
|
|
|
4
|
|
|
use Propel\Generator\Model\Table; |
5
|
|
|
use keeko\tools\utils\NameUtils; |
6
|
|
|
|
7
|
|
|
class AbstractModelJsonResponseGenerator extends AbstractJsonResponseGenerator { |
8
|
|
|
|
9
|
|
|
protected function getRelationshipIncludes(Table $model, $root = '', $processed = []) { |
10
|
|
|
if (in_array($model->getOriginCommonName(), $processed)) { |
11
|
|
|
return []; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
$relationships = $this->modelService->getRelationships($model); |
15
|
|
|
$includes = []; |
|
|
|
|
16
|
|
|
|
17
|
|
|
foreach ($relationships['all'] as $rel) { |
18
|
|
|
$fk = $rel['fk']; |
|
|
|
|
19
|
|
|
$foreignModel = $fk->getForeignTable(); |
20
|
|
|
$processed[] = $foreignModel->getOriginCommonName(); |
|
|
|
|
21
|
|
|
|
22
|
|
|
$typeName = NameUtils::dasherize($fk->getForeignTable()->getOriginCommonName()); |
23
|
|
|
if ($rel['type'] == 'many') { |
24
|
|
|
$typeName = NameUtils::pluralize($typeName); |
25
|
|
|
} |
26
|
|
|
$includes[] = (!empty($root) ? $root . '.' : '') . $typeName; |
27
|
|
|
$includes = array_merge($includes, $this->getRelationshipIncludes($foreignModel, $typeName, $processed)); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $includes; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function getModelFields(Table $model, $root = '', $processed = []) { |
34
|
|
|
if (in_array($model->getOriginCommonName(), $processed)) { |
35
|
|
|
return []; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$typeName = NameUtils::dasherize($model->getOriginCommonName()); |
|
|
|
|
39
|
|
|
$relationships = $this->modelService->getRelationships($model); |
40
|
|
|
$fields = [$typeName => $model]; |
|
|
|
|
41
|
|
|
|
42
|
|
|
foreach ($relationships['all'] as $rel) { |
43
|
|
|
$fk = $rel['fk']; |
|
|
|
|
44
|
|
|
$foreignModel = $fk->getForeignTable(); |
45
|
|
|
$processed[] = $foreignModel->getOriginCommonName(); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$typeName = NameUtils::dasherize($fk->getForeignTable()->getOriginCommonName()); |
48
|
|
|
if ($rel['type'] == 'many') { |
49
|
|
|
$typeName = NameUtils::pluralize($typeName); |
50
|
|
|
} |
51
|
|
|
$name = (!empty($root) ? $root . '.' : '') . $typeName; |
52
|
|
|
|
53
|
|
|
$fields[$name] = $foreignModel; |
54
|
|
|
$fields = array_merge($fields, $this->getModelFields($foreignModel, $name, $processed)); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $fields; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
protected function getFieldsCode(array $fields) { |
|
|
|
|
61
|
|
|
$code = ''; |
62
|
|
|
foreach ($fields as $typeName => $field) { |
63
|
|
|
$code .= sprintf("\t'%s' => %s::getSerializer()->getFields(),\n", $typeName, $field->getPhpName()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (strlen($code) > 0) { |
67
|
|
|
$code = substr($code, 0, -2); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return sprintf("[\n%s\n]", $code); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.