1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Classes\AdapterMakerFile\Phalcon; |
4
|
|
|
|
5
|
|
|
use Classes\AdapterConfig\Phalcon; |
6
|
|
|
use Classes\AdapterMakerFile\AbstractAdapter; |
7
|
|
|
use Classes\Maker\AbstractMaker; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Pedro Alarcao <[email protected]> |
11
|
|
|
* @link https://github.com/pedro151/orm-generator |
12
|
|
|
*/ |
13
|
|
|
class Entity extends AbstractAdapter |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var void |
18
|
|
|
*/ |
19
|
|
|
public $pastName = "entity"; |
20
|
|
|
protected $fileTpl = "entity.php"; |
21
|
|
|
protected $overwrite = true; |
22
|
|
|
|
23
|
|
|
protected $validFunc = array (); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param \Classes\MakerFile $makerFile |
27
|
|
|
* @param \Classes\Db\DbTable $dbTable |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function parseRelation ( \Classes\MakerFile $makerFile , \Classes\Db\DbTable $dbTable ) |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
return array ( |
35
|
|
|
'mapParents' => $this->listParents ( $makerFile , $dbTable ) , |
36
|
|
|
'mapDependents' => $this->listDependence ( $makerFile , $dbTable ) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param \Classes\MakerFile $makerFile |
42
|
|
|
* @param \Classes\Db\DbTable $dbTable |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
private function listParents ( \Classes\MakerFile $makerFile , \Classes\Db\DbTable $dbTable ) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$mapParents = ''; |
49
|
|
|
$references = array (); |
50
|
|
|
foreach ( $dbTable->getForeingkeys () as $objColumn ) |
51
|
|
|
{ |
52
|
|
|
$constrant = $objColumn->getFks (); |
53
|
|
|
$references[] = sprintf ( |
54
|
|
|
"\$this->hasMany('%s', '%s', '%s')" , |
55
|
|
|
$objColumn->getName () , |
56
|
|
|
$makerFile->getConfig ()->createClassNamespace ( $constrant ) |
57
|
|
|
. Phalcon::SEPARETOR |
58
|
|
|
. AbstractMaker::getClassName ( $constrant->getTable () ) , |
59
|
|
|
$constrant->getColumn () |
60
|
|
|
|
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ( sizeof ( $references ) > 0 ) |
65
|
|
|
{ |
66
|
|
|
$mapParents = join ( ";\n\t\t" , $references ) . ";\n"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
return $mapParents; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \Classes\MakerFile $makerFile |
75
|
|
|
* @param \Classes\Db\DbTable $dbTable |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
private function listDependence ( \Classes\MakerFile $makerFile , \Classes\Db\DbTable $dbTable ) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$mapDependence = ''; |
82
|
|
|
$references = array (); |
83
|
|
|
foreach ( $dbTable->getDependences () as $objColumn ) |
84
|
|
|
{ |
85
|
|
|
foreach ( $objColumn->getDependences () as $dependence ) |
86
|
|
|
{ |
87
|
|
|
$references[] = sprintf ( |
88
|
|
|
"\$this->belongsTo('%s', '%s', '%s')" , |
89
|
|
|
$objColumn->getName () , |
90
|
|
|
$makerFile->getConfig ()->createClassNamespace ( $dependence ) |
91
|
|
|
. Phalcon::SEPARETOR |
92
|
|
|
. AbstractMaker::getClassName ( $dependence->getTable () ) , |
93
|
|
|
$dependence->getColumn () |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ( sizeof ( $references ) > 0 ) |
100
|
|
|
{ |
101
|
|
|
$mapDependence = join ( ";\n\t\t" , $references ) . ";"; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $mapDependence; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.