1
1
<?php
2
-
3
2
namespace Ubiquity \orm \creator ;
4
3
5
4
use Ubiquity \annotations \IdAnnotation ;
10
9
use Ubiquity \annotations \JoinColumnAnnotation ;
11
10
use Ubiquity \annotations \ColumnAnnotation ;
12
11
use Ubiquity \contents \validation \ValidationModelGenerator ;
12
+ use Ubiquity \annotations \TransformerAnnotation ;
13
13
14
14
/**
15
+ * Represents a data member in a model class.
15
16
* Ubiquity\orm\creator$Member
16
17
* This class is part of Ubiquity
18
+ *
17
19
* @author jcheron <[email protected] >
18
- * @version 1.0.0
20
+ * @version 1.0.1
19
21
* @package ubiquity.dev
20
22
*
21
23
*/
22
24
class Member {
25
+
23
26
private $ name ;
27
+
24
28
private $ primary ;
29
+
25
30
private $ manyToOne ;
31
+
26
32
private $ annotations ;
27
33
28
34
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 ;
33
39
}
34
40
35
41
public function __toString () {
36
- $ annotationsStr= "" ;
42
+ $ annotationsStr = "" ;
37
43
if (sizeof ($ this ->annotations ) > 0 ) {
38
- $ annotationsStr= "\n\t/** " ;
39
- $ annotations= $ this ->annotations ;
44
+ $ annotationsStr = "\n\t/** " ;
45
+ $ annotations = $ this ->annotations ;
40
46
\array_walk ($ annotations , function ($ item ) {
41
47
return $ item . "" ;
42
48
});
43
49
if (\sizeof ($ annotations ) > 1 ) {
44
- $ annotationsStr.= "\n\t * " . implode ("\n\t * " , $ annotations );
50
+ $ annotationsStr .= "\n\t * " . implode ("\n\t * " , $ annotations );
45
51
} else {
46
- $ annotationsStr.= "\n\t * " . \end ($ annotations );
52
+ $ annotationsStr .= "\n\t * " . \end ($ annotations );
47
53
}
48
- $ annotationsStr.= "\n\t**/ " ;
54
+ $ annotationsStr .= "\n\t**/ " ;
49
55
}
50
56
return $ annotationsStr . "\n\tprivate $ " . $ this ->name . "; \n" ;
51
57
}
52
58
53
59
public function setPrimary () {
54
60
if ($ this ->primary === false ) {
55
- $ this ->annotations []= new IdAnnotation ();
56
- $ this ->primary = true ;
61
+ $ this ->annotations [] = new IdAnnotation ();
62
+ $ this ->primary = true ;
57
63
}
58
64
}
59
65
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 ;
66
72
}
67
73
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 ;
76
82
}
77
83
78
84
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 ;
91
147
if (\sizeof ($ joinColumns ) == 2 ) {
92
- $ jt ->joinColumns = $ joinColumns ;
148
+ $ jt ->joinColumns = $ joinColumns ;
93
149
}
94
150
if (\sizeof ($ inverseJoinColumns ) == 2 ) {
95
- $ jt ->inverseJoinColumns = $ inverseJoinColumns ;
151
+ $ jt ->inverseJoinColumns = $ inverseJoinColumns ;
96
152
}
97
- $ this ->annotations []= $ manyToMany ;
98
- $ this ->annotations []= $ jt ;
153
+ $ this ->annotations [] = $ manyToMany ;
154
+ $ this ->annotations [] = $ jt ;
99
155
}
100
156
101
157
public function getName () {
@@ -107,7 +163,7 @@ public function isManyToOne() {
107
163
}
108
164
109
165
public function getManyToOne () {
110
- foreach ( $ this ->annotations as $ annotation ) {
166
+ foreach ($ this ->annotations as $ annotation ) {
111
167
if ($ annotation instanceof JoinColumnAnnotation) {
112
168
return $ annotation ;
113
169
}
@@ -120,40 +176,40 @@ public function isPrimary() {
120
176
}
121
177
122
178
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" ;
126
182
return $ result ;
127
183
}
128
184
129
185
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" ;
133
189
return $ result ;
134
190
}
135
191
136
- public function hasAnnotations (){
137
- return \count ($ this ->annotations )> 1 ;
192
+ public function hasAnnotations () {
193
+ return \count ($ this ->annotations ) > 1 ;
138
194
}
139
195
140
- public function isNullable (){
141
- if (isset ($ this ->annotations ["column " ]))
196
+ public function isNullable () {
197
+ if (isset ($ this ->annotations ["column " ]))
142
198
return $ this ->annotations ["column " ]->nullable ;
143
199
return false ;
144
200
}
145
201
146
- public function getDbType (){
147
- if (isset ($ this ->annotations ["column " ]))
202
+ public function getDbType () {
203
+ if (isset ($ this ->annotations ["column " ]))
148
204
return $ this ->annotations ["column " ]->dbType ;
149
205
return "mixed " ;
150
206
}
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 );
157
213
}
158
214
}
159
215
}
0 commit comments