Skip to content

Commit 43463f6

Browse files
committed
Add transformer (auto) to model generation
1 parent 25de67d commit 43463f6

File tree

3 files changed

+204
-167
lines changed

3 files changed

+204
-167
lines changed
Lines changed: 119 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Ubiquity\orm\creator;
43

54
use Ubiquity\annotations\IdAnnotation;
@@ -10,92 +9,149 @@
109
use Ubiquity\annotations\JoinColumnAnnotation;
1110
use Ubiquity\annotations\ColumnAnnotation;
1211
use Ubiquity\contents\validation\ValidationModelGenerator;
12+
use Ubiquity\annotations\TransformerAnnotation;
1313

1414
/**
15+
* Represents a data member in a model class.
1516
* Ubiquity\orm\creator$Member
1617
* This class is part of Ubiquity
18+
*
1719
* @author jcheron <[email protected]>
18-
* @version 1.0.0
20+
* @version 1.0.1
1921
* @package ubiquity.dev
2022
*
2123
*/
2224
class Member {
25+
2326
private $name;
27+
2428
private $primary;
29+
2530
private $manyToOne;
31+
2632
private $annotations;
2733

2834
public function __construct($name) {
29-
$this->name=$name;
30-
$this->annotations=array ();
31-
$this->primary=false;
32-
$this->manyToOne=false;
35+
$this->name = $name;
36+
$this->annotations = [];
37+
$this->primary = false;
38+
$this->manyToOne = false;
3339
}
3440

3541
public function __toString() {
36-
$annotationsStr="";
42+
$annotationsStr = "";
3743
if (sizeof($this->annotations) > 0) {
38-
$annotationsStr="\n\t/**";
39-
$annotations=$this->annotations;
44+
$annotationsStr = "\n\t/**";
45+
$annotations = $this->annotations;
4046
\array_walk($annotations, function ($item) {
4147
return $item . "";
4248
});
4349
if (\sizeof($annotations) > 1) {
44-
$annotationsStr.="\n\t * " . implode("\n\t * ", $annotations);
50+
$annotationsStr .= "\n\t * " . implode("\n\t * ", $annotations);
4551
} else {
46-
$annotationsStr.="\n\t * " . \end($annotations);
52+
$annotationsStr .= "\n\t * " . \end($annotations);
4753
}
48-
$annotationsStr.="\n\t**/";
54+
$annotationsStr .= "\n\t**/";
4955
}
5056
return $annotationsStr . "\n\tprivate $" . $this->name . ";\n";
5157
}
5258

5359
public function setPrimary() {
5460
if ($this->primary === false) {
55-
$this->annotations[]=new IdAnnotation();
56-
$this->primary=true;
61+
$this->annotations[] = new IdAnnotation();
62+
$this->primary = true;
5763
}
5864
}
5965

60-
public function setDbType($infos){
61-
$annot=new ColumnAnnotation();
62-
$annot->name=$this->name;
63-
$annot->dbType=$infos["Type"];
64-
$annot->nullable=(\strtolower($infos["Nullable"])==="yes");
65-
$this->annotations["column"]=$annot;
66+
public function setDbType($infos) {
67+
$annot = new ColumnAnnotation();
68+
$annot->name = $this->name;
69+
$annot->dbType = $infos["Type"];
70+
$annot->nullable = (\strtolower($infos["Nullable"]) === "yes");
71+
$this->annotations["column"] = $annot;
6672
}
6773

68-
public function addManyToOne($name, $className, $nullable=false) {
69-
$this->annotations[]=new ManyToOneAnnotation();
70-
$joinColumn=new JoinColumnAnnotation();
71-
$joinColumn->name=$name;
72-
$joinColumn->className=$className;
73-
$joinColumn->nullable=$nullable;
74-
$this->annotations[]=$joinColumn;
75-
$this->manyToOne=true;
74+
public function addManyToOne($name, $className, $nullable = false) {
75+
$this->annotations[] = new ManyToOneAnnotation();
76+
$joinColumn = new JoinColumnAnnotation();
77+
$joinColumn->name = $name;
78+
$joinColumn->className = $className;
79+
$joinColumn->nullable = $nullable;
80+
$this->annotations[] = $joinColumn;
81+
$this->manyToOne = true;
7682
}
7783

7884
public function addOneToMany($mappedBy, $className) {
79-
$oneToMany=new OneToManyAnnotation();
80-
$oneToMany->mappedBy=$mappedBy;
81-
$oneToMany->className=$className;
82-
$this->annotations[]=$oneToMany;
83-
}
84-
85-
public function addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns=[], $inverseJoinColumns=[]) {
86-
$manyToMany=new ManyToManyAnnotation();
87-
$manyToMany->targetEntity=$targetEntity;
88-
$manyToMany->inversedBy=$inversedBy;
89-
$jt=new JoinTableAnnotation();
90-
$jt->name=$joinTable;
85+
$oneToMany = new OneToManyAnnotation();
86+
$oneToMany->mappedBy = $mappedBy;
87+
$oneToMany->className = $className;
88+
$this->annotations[] = $oneToMany;
89+
}
90+
91+
private function addTransformer($name) {
92+
$transformer = new TransformerAnnotation();
93+
$transformer->name = $name;
94+
$this->annotations[] = $transformer;
95+
}
96+
97+
/**
98+
* Try to set a transformer to the member.
99+
*/
100+
public function setTransformer() {
101+
if ($this->isPassword()) {
102+
$this->addTransformer('password');
103+
} else {
104+
$dbType = $this->getDbType();
105+
if ($dbType == 'datetime') {
106+
$this->addTransformer('datetime');
107+
}
108+
}
109+
}
110+
111+
public function isPassword() {
112+
// Array of multiple translations of the word "password" which could be taken as name of the table field in database
113+
$pwArray = array(
114+
'password',
115+
'senha',
116+
'lozinka',
117+
'heslotajne',
118+
'helslo_tajne',
119+
'wachtwoord',
120+
'contrasena',
121+
'salasana',
122+
'motdepasse',
123+
'mot_de_passe',
124+
'passwort',
125+
'passord',
126+
'haslo',
127+
'senha',
128+
'parola',
129+
'naponb',
130+
'contrasena',
131+
'loesenord',
132+
'losenord',
133+
'sifre',
134+
'naponb',
135+
'matkhau',
136+
'mat_khau'
137+
);
138+
return \in_array($this->name, $pwArray);
139+
}
140+
141+
public function addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns = [], $inverseJoinColumns = []) {
142+
$manyToMany = new ManyToManyAnnotation();
143+
$manyToMany->targetEntity = $targetEntity;
144+
$manyToMany->inversedBy = $inversedBy;
145+
$jt = new JoinTableAnnotation();
146+
$jt->name = $joinTable;
91147
if (\sizeof($joinColumns) == 2) {
92-
$jt->joinColumns=$joinColumns;
148+
$jt->joinColumns = $joinColumns;
93149
}
94150
if (\sizeof($inverseJoinColumns) == 2) {
95-
$jt->inverseJoinColumns=$inverseJoinColumns;
151+
$jt->inverseJoinColumns = $inverseJoinColumns;
96152
}
97-
$this->annotations[]=$manyToMany;
98-
$this->annotations[]=$jt;
153+
$this->annotations[] = $manyToMany;
154+
$this->annotations[] = $jt;
99155
}
100156

101157
public function getName() {
@@ -107,7 +163,7 @@ public function isManyToOne() {
107163
}
108164

109165
public function getManyToOne() {
110-
foreach ( $this->annotations as $annotation ) {
166+
foreach ($this->annotations as $annotation) {
111167
if ($annotation instanceof JoinColumnAnnotation) {
112168
return $annotation;
113169
}
@@ -120,40 +176,40 @@ public function isPrimary() {
120176
}
121177

122178
public function getGetter() {
123-
$result="\n\t public function get" . \ucfirst($this->name) . "(){\n";
124-
$result.="\t\t" . 'return $this->' . $this->name . ";\n";
125-
$result.="\t}\n";
179+
$result = "\n\t public function get" . \ucfirst($this->name) . "(){\n";
180+
$result .= "\t\t" . 'return $this->' . $this->name . ";\n";
181+
$result .= "\t}\n";
126182
return $result;
127183
}
128184

129185
public function getSetter() {
130-
$result="\n\t public function set" . \ucfirst($this->name) . '($' . $this->name . "){\n";
131-
$result.="\t\t" . '$this->' . $this->name . '=$' . $this->name . ";\n";
132-
$result.="\t}\n";
186+
$result = "\n\t public function set" . \ucfirst($this->name) . '($' . $this->name . "){\n";
187+
$result .= "\t\t" . '$this->' . $this->name . '=$' . $this->name . ";\n";
188+
$result .= "\t}\n";
133189
return $result;
134190
}
135191

136-
public function hasAnnotations(){
137-
return \count($this->annotations)>1;
192+
public function hasAnnotations() {
193+
return \count($this->annotations) > 1;
138194
}
139195

140-
public function isNullable(){
141-
if(isset($this->annotations["column"]))
196+
public function isNullable() {
197+
if (isset($this->annotations["column"]))
142198
return $this->annotations["column"]->nullable;
143199
return false;
144200
}
145201

146-
public function getDbType(){
147-
if(isset($this->annotations["column"]))
202+
public function getDbType() {
203+
if (isset($this->annotations["column"]))
148204
return $this->annotations["column"]->dbType;
149205
return "mixed";
150206
}
151-
152-
public function addValidators(){
153-
$parser=new ValidationModelGenerator($this->getDbType(), $this->name, !$this->isNullable(), $this->primary);
154-
$validators=$parser->parse();
155-
if(sizeof($validators)){
156-
$this->annotations=array_merge($this->annotations,$validators);
207+
208+
public function addValidators() {
209+
$parser = new ValidationModelGenerator($this->getDbType(), $this->name, ! $this->isNullable(), $this->primary);
210+
$validators = $parser->parse();
211+
if (sizeof($validators)) {
212+
$this->annotations = array_merge($this->annotations, $validators);
157213
}
158214
}
159215
}

src/Ubiquity/orm/creator/Model.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* This class is part of Ubiquity
88
*
99
* @author jcheron <[email protected]>
10-
* @version 1.0.1
10+
* @version 1.0.2
1111
* @package ubiquity.dev
1212
*
1313
*/
@@ -145,36 +145,11 @@ public function getManyToOneMembers() {
145145

146146
private function getToStringField() {
147147
$result = null;
148-
// Array of multiple translations of the word "password" which could be taken as name of the table field in database
149-
$pwArray = array(
150-
'password',
151-
'senha',
152-
'lozinka',
153-
'heslotajne',
154-
'helslo_tajne',
155-
'wachtwoord',
156-
'contrasena',
157-
'salasana',
158-
'motdepasse',
159-
'mot_de_passe',
160-
'passwort',
161-
'passord',
162-
'haslo',
163-
'senha',
164-
'parola',
165-
'naponb',
166-
'contrasena',
167-
'loesenord',
168-
'losenord',
169-
'sifre',
170-
'naponb',
171-
'matkhau',
172-
'mat_khau'
173-
);
148+
174149
foreach ($this->members as $member) {
175150
if ($member->getDbType() !== 'mixed' && $member->isNullable() !== true && ! $member->isPrimary()) {
176151
$memberName = $member->getName();
177-
if (! \in_array($memberName, $pwArray)) {
152+
if (! $member->isPassword()) {
178153
$result = $memberName;
179154
}
180155
}

0 commit comments

Comments
 (0)