4
4
5
5
use ByJG \Authenticate \Model \UserModel ;
6
6
use ByJG \MicroOrm \Mapper ;
7
+ use ByJG \Serializer \BinderObject ;
7
8
8
9
/**
9
10
* Structure to represent the users
10
11
*/
11
12
class UserDefinition
12
13
{
13
-
14
- protected $ table = 'users ' ;
15
- protected $ userid = 'userid ' ;
16
- protected $ name = 'name ' ;
17
- protected $ email = 'email ' ;
18
- protected $ username = 'username ' ;
19
- protected $ password = 'password ' ;
20
- protected $ created = 'created ' ;
21
- protected $ admin = 'admin ' ;
14
+ protected $ __table = 'users ' ;
15
+ protected $ __closures = ["select " => [], "update " => [] ];
16
+ protected $ __loginField ;
17
+ protected $ __model ;
18
+ protected $ __properties = [];
22
19
23
20
const UPDATE ="update " ;
24
21
const SELECT ="select " ;
25
22
26
- protected $ closures = [ "select " => [], "update " => [] ];
27
-
28
- protected $ loginField ;
29
-
30
- protected $ model ;
31
23
32
24
const LOGIN_IS_EMAIL ="email " ;
33
25
const LOGIN_IS_USERNAME ="username " ;
@@ -36,109 +28,102 @@ class UserDefinition
36
28
* Define the name of fields and table to store and retrieve info from database
37
29
*
38
30
* @param string $table
31
+ * @param string $model
39
32
* @param string $loginField
40
33
* @param array $fieldDef
34
+ * @throws \Exception
41
35
*/
42
36
public function __construct (
43
37
$ table = 'users ' ,
38
+ $ model = UserModel::class,
44
39
$ loginField = self ::LOGIN_IS_USERNAME ,
45
40
$ fieldDef = []
46
41
) {
47
- $ this ->table = $ table ;
42
+ $ this ->__table = $ table ;
43
+ $ this ->__model = $ model ;
44
+
45
+ // Set Default User Definition
46
+ $ modelInstance = $ this ->modelInstance ();
47
+ $ modelProperties = BinderObject::toArrayFrom ($ modelInstance );
48
+ foreach (array_keys ($ modelProperties ) as $ property ) {
49
+ $ this ->__properties [$ property ] = $ property ;
50
+ }
48
51
52
+ // Set custom Properties
49
53
foreach ($ fieldDef as $ property => $ value ) {
50
54
$ this ->checkProperty ($ property );
51
- $ this ->{ $ property} = $ value ;
55
+ $ this ->__properties [ $ property] = $ value ;
52
56
}
53
57
54
- $ this ->model = UserModel::class;
55
-
56
58
$ this ->defineClosureForUpdate ('password ' , function ($ value ) {
57
- return strtoupper (sha1 ($ value ));
59
+ // Already have a SHA1 password
60
+ if (strlen ($ value ) === 40 ) {
61
+ return $ value ;
62
+ }
63
+
64
+ // Leave null
65
+ if (empty ($ value )) {
66
+ return null ;
67
+ }
68
+
69
+ // Return the hash password
70
+ return strtolower (sha1 ($ value ));
58
71
});
59
72
60
73
if ($ loginField !== self ::LOGIN_IS_USERNAME && $ loginField !== self ::LOGIN_IS_EMAIL ) {
61
74
throw new \InvalidArgumentException ('Login field is invalid. ' );
62
75
}
63
- $ this ->loginField = $ loginField ;
64
- }
76
+ $ this ->__loginField = $ loginField ;
65
77
66
- /**
67
- * @return string
68
- */
69
- public function table ()
70
- {
71
- return $ this ->table ;
72
- }
73
-
74
- /**
75
- * @return string
76
- */
77
- public function getUserid ()
78
- {
79
- return $ this ->userid ;
78
+ $ this ->beforeInsert = function ($ instance ) {
79
+ return $ instance ;
80
+ };
81
+ $ this ->beforeUpdate = function ($ instance ) {
82
+ return $ instance ;
83
+ };
80
84
}
81
85
82
86
/**
83
87
* @return string
84
88
*/
85
- public function getName ()
89
+ public function table ()
86
90
{
87
- return $ this ->name ;
91
+ return $ this ->__table ;
88
92
}
89
93
90
- /**
91
- * @return string
92
- */
93
- public function getEmail ()
94
- {
95
- return $ this ->email ;
96
- }
97
94
98
- /**
99
- * @return string
100
- */
101
- public function getUsername ()
95
+ public function __get ($ name )
102
96
{
103
- return $ this ->username ;
97
+ $ this ->checkProperty ($ name );
98
+ return $ this ->__properties [$ name ];
104
99
}
105
100
106
- /**
107
- * @return string
108
- */
109
- public function getPassword ()
101
+ public function __call ($ name , $ arguments )
110
102
{
111
- return $ this ->password ;
112
- }
113
-
114
- /**
115
- * @return string
116
- */
117
- public function getCreated ()
118
- {
119
- return $ this ->created ;
103
+ if (strpos ($ name , 'get ' ) === 0 ) {
104
+ $ name = strtolower (substr ($ name , 3 ));
105
+ return $ this ->{$ name };
106
+ }
107
+ throw new \InvalidArgumentException ("Method ' $ name' does not exists' " );
120
108
}
121
109
122
- /**
123
- * @return string
124
- */
125
- public function getAdmin ()
110
+ public function toArray ()
126
111
{
127
- return $ this ->admin ;
112
+ return $ this ->__properties ;
128
113
}
129
114
130
115
/**
131
116
* @return string
132
117
*/
133
118
public function loginField ()
134
119
{
135
- return $ this ->{" get " . $ this ->loginField }() ;
120
+ return $ this ->{$ this ->__loginField } ;
136
121
}
137
122
138
123
private function checkProperty ($ property )
139
124
{
140
- if (!isset ($ this ->{ $ property} )) {
141
- throw new \InvalidArgumentException (' Invalid property ' );
125
+ if (!isset ($ this ->__properties [ $ property] )) {
126
+ throw new \InvalidArgumentException (" Property ' $ property' does not exists' " );
142
127
}
143
128
}
144
129
@@ -150,24 +135,33 @@ private function checkProperty($property)
150
135
private function updateClosureDef ($ event , $ property , $ closure )
151
136
{
152
137
$ this ->checkProperty ($ property );
153
- $ this ->closures [$ event ][$ property ] = $ closure ;
138
+ $ this ->__closures [$ event ][$ property ] = $ closure ;
154
139
}
155
140
156
141
private function getClosureDef ($ event , $ property )
157
142
{
158
143
$ this ->checkProperty ($ property );
159
144
160
- // If does no exists event returns the default closure
161
- if (!isset ($ this ->closures [$ event ])) {
145
+ if (!$ this ->existsClosure ($ event , $ property )) {
162
146
return Mapper::defaultClosure ();
163
147
}
164
148
165
- // If event exists but does no exists the property returns the default closure
166
- if (!array_key_exists ($ property , $ this ->closures [$ event ])) {
167
- return Mapper::defaultClosure ();
149
+ return $ this ->__closures [$ event ][$ property ];
150
+ }
151
+
152
+ public function existsClosure ($ event , $ property )
153
+ {
154
+ // Event not set
155
+ if (!isset ($ this ->__closures [$ event ])) {
156
+ return false ;
168
157
}
169
158
170
- return $ this ->closures [$ event ][$ property ];
159
+ // Event is set but there is no property
160
+ if (!array_key_exists ($ property , $ this ->__closures [$ event ])) {
161
+ return false ;
162
+ }
163
+
164
+ return true ;
171
165
}
172
166
173
167
public function markPropertyAsReadOnly ($ property )
@@ -201,12 +195,48 @@ public function getClosureForSelect($property)
201
195
202
196
public function model ()
203
197
{
204
- return $ this ->model ;
198
+ return $ this ->__model ;
205
199
}
206
200
207
201
public function modelInstance ()
208
202
{
209
- $ model = $ this ->model ;
203
+ $ model = $ this ->__model ;
210
204
return new $ model ();
211
205
}
206
+
207
+ protected $ beforeInsert ;
208
+
209
+ /**
210
+ * @return mixed
211
+ */
212
+ public function getBeforeInsert ()
213
+ {
214
+ return $ this ->beforeInsert ;
215
+ }
216
+
217
+ /**
218
+ * @param mixed $beforeInsert
219
+ */
220
+ public function setBeforeInsert ($ beforeInsert )
221
+ {
222
+ $ this ->beforeInsert = $ beforeInsert ;
223
+ }
224
+
225
+ protected $ beforeUpdate ;
226
+
227
+ /**
228
+ * @return mixed
229
+ */
230
+ public function getBeforeUpdate ()
231
+ {
232
+ return $ this ->beforeUpdate ;
233
+ }
234
+
235
+ /**
236
+ * @param mixed $beforeUpdate
237
+ */
238
+ public function setBeforeUpdate ($ beforeUpdate )
239
+ {
240
+ $ this ->beforeUpdate = $ beforeUpdate ;
241
+ }
212
242
}
0 commit comments