7
7
*
8
8
* @author Ibrahim
9
9
*
10
- * @version 1.0
10
+ * @version 1.0.1
11
11
*/
12
12
class EntityMapper {
13
13
/**
@@ -57,6 +57,14 @@ class EntityMapper {
57
57
* @since 1.0
58
58
*/
59
59
private $ table ;
60
+ /**
61
+ * An array that holds extra attributes which can be added to the entity.
62
+ *
63
+ * @var array
64
+ *
65
+ * @since 1.0
66
+ */
67
+ private $ extraAttrs ;
60
68
/**
61
69
* Creates new instance of the class.
62
70
*
@@ -97,6 +105,51 @@ public function __construct($tableObj, $className, $path = __DIR__, $namespace =
97
105
}
98
106
99
107
$ this ->setUseJsonI (false );
108
+ $ this ->extraAttrs = [];
109
+ }
110
+ /**
111
+ * Adds extra class attribute to the entity that will be created.
112
+ *
113
+ * @param string $attrName The name of the attribute. A valid attribute name
114
+ * must follow following conditions:
115
+ * <ul>
116
+ * <li>Must be non-empty string.</li>
117
+ * <li>First letter must be non-number.</li>
118
+ * <li>It must not contain $.</li>
119
+ * </ul>
120
+ *
121
+ * @return boolean If the attribute is added, the method will return
122
+ * true. Other than that, the method will return false.
123
+ *
124
+ * @since 1.0.1
125
+ */
126
+ public function addAttribute ($ attrName ) {
127
+ $ trimmed = trim ($ attrName );
128
+
129
+ if (strlen ($ trimmed ) == 0 ) {
130
+ return false ;
131
+ }
132
+ if ($ trimmed [0 ] <= '9 ' && $ trimmed [0 ] >= '0 ' ) {
133
+ return false ;
134
+ }
135
+ if (strpos (' ' , $ trimmed ) === false || strpos ('$ ' , $ trimmed ) === false ) {
136
+ if (!in_array ($ trimmed , $ this ->extraAttrs )) {
137
+ $ this ->extraAttrs [] = $ trimmed ;
138
+ return true ;
139
+ }
140
+ }
141
+ return false ;
142
+ }
143
+ /**
144
+ * Returns an array that holds the names of the extra attributes which are
145
+ * defined by the user.
146
+ *
147
+ * @return array an indexed array of attributes names,
148
+ *
149
+ * @since 1.0.1
150
+ */
151
+ public function getAttributes () {
152
+ return $ this ->extraAttrs ;
100
153
}
101
154
/**
102
155
* Creates the class that the table records will be mapped to.
@@ -177,6 +230,11 @@ public function getAttribitesNames() {
177
230
foreach ($ keys as $ keyName ) {
178
231
$ retVal [$ keyName ] = $ this ->_colKeyToAttr ($ keyName );
179
232
}
233
+
234
+ foreach ($ this ->getAttributes () as $ attrName ) {
235
+ //The @ only used to show user defined attributes.
236
+ $ retVal [$ attrName .'@ ' ] = $ attrName ;
237
+ }
180
238
ksort ($ retVal , SORT_STRING );
181
239
182
240
return $ retVal ;
@@ -209,6 +267,12 @@ public function getEntityMethods() {
209
267
$ retVal ['getters ' ][] = $ this ->_colKeyToSetterOrGetter ($ keyName , 'g ' );
210
268
$ retVal ['setters ' ][] = $ this ->_colKeyToSetterOrGetter ($ keyName , 's ' );
211
269
}
270
+ foreach ($ this ->getAttributes () as $ attrName ) {
271
+ $ firstLetter = $ attrName [0 ];
272
+ $ xattr = substr ($ attrName , 1 );
273
+ $ retVal ['getters ' ][] = 'get ' . strtoupper ($ firstLetter ).$ xattr ;
274
+ $ retVal ['setters ' ][] = 'set ' . strtoupper ($ firstLetter ).$ xattr ;;
275
+ }
212
276
sort ($ retVal ['getters ' ], SORT_STRING );
213
277
sort ($ retVal ['setters ' ], SORT_STRING );
214
278
@@ -409,30 +473,39 @@ private function _appendGetterMethod($attrName, $colName, $phpType, $getterName)
409
473
$ this ->classStr .= ""
410
474
." /** \n"
411
475
." * Returns the value of the attribute ' " .$ attrName ."'. \n"
412
- ." * \n"
413
- ." * The value of the attribute is mapped to the column which has \n"
414
- ." * the name ' " .$ colName ."'. \n"
415
- ." * \n"
416
- ." * @return " .$ phpType ." The value of the attribute. \n"
417
- ." **/ \n"
418
- .' public function ' .$ getterName ."() { \n"
419
- .' return $this-> ' .$ attrName ."; \n"
420
- ." } \n" ;
476
+ ." * \n" ;
477
+ if ($ colName === null ) {
478
+ $ this ->classStr .= " * @return " .$ phpType ." The value of the attribute. \n"
479
+ ." **/ \n" ;
480
+ } else {
481
+ $ this ->classStr .= " * The value of the attribute is mapped to the column which has \n"
482
+ ." * the name ' " .$ colName ."'. \n"
483
+ ." * \n"
484
+ ." * @return " .$ phpType ." The value of the attribute. \n"
485
+ ." **/ \n" ;
486
+ }
487
+ $ this ->classStr .= ' public function ' .$ getterName ."() { \n"
488
+ .' return $this-> ' .$ attrName ."; \n"
489
+ ." } \n" ;
421
490
}
422
491
private function _appendSetter ($ attrName , $ colName , $ phpType , $ setterName , $ colDatatype ) {
423
492
$ this ->classStr .= ""
424
493
." /** \n"
425
494
." * Sets the value of the attribute ' " .$ attrName ."'. \n"
426
- ." * \n"
427
- ." * The value of the attribute is mapped to the column which has \n"
495
+ ." * \n" ;
496
+ if ($ colName !== null ) {
497
+ $ this ->classStr .=
498
+ " * The value of the attribute is mapped to the column which has \n"
428
499
." * the name ' " .$ colName ."'. \n"
429
- ." * \n"
500
+ . " * \n" ;
501
+ }
502
+ $ this ->classStr .= ""
430
503
." * @param \$$ attrName " .$ phpType ." The new value of the attribute. \n"
431
504
." **/ \n"
432
505
.' public function ' .$ setterName .'($ ' .$ attrName .") { \n" ;
433
506
434
507
if ($ colDatatype == 'boolean ' || $ colDatatype == 'bool ' ) {
435
- $ this ->classStr .= ' $this-> ' .$ attrName .' = $ ' .$ attrName ." === true || $ " .$ attrName ." == 'Y'; \n" ;
508
+ $ this ->classStr .= ' $this-> ' .$ attrName .' = $ ' .$ attrName ." === true || $ " .$ attrName ." == 'Y' || $ " . $ attrName . " == 1 ; \n" ;
436
509
} else {
437
510
$ this ->classStr .= ' $this-> ' .$ attrName .' = $ ' .$ attrName ."; \n" ;
438
511
}
@@ -484,14 +557,26 @@ private function _createEntityMethods() {
484
557
485
558
foreach ($ entityAttrs as $ colKey => $ attrName ) {
486
559
$ colObj = $ this ->getTable ()->getColByKey ($ colKey );
487
- $ getterName = $ this ->_colKeyToSetterOrGetter ($ colKey , 'g ' );
488
- $ this ->_appendGetterMethod ($ attrName , $ colObj ->getNormalName (), $ colObj ->getPHPType (), $ getterName );
560
+ if ($ colObj !== null ) {
561
+ $ getterName = $ this ->_colKeyToSetterOrGetter ($ colKey , 'g ' );
562
+ $ this ->_appendGetterMethod ($ attrName , $ colObj ->getNormalName (), $ colObj ->getPHPType (), $ getterName );
563
+ } else {
564
+ $ firstLetter = $ attrName [0 ];
565
+ $ xattrName = substr ($ attrName , 1 );
566
+ $ this ->_appendGetterMethod ($ attrName , null , 'mixed ' , 'get ' . strtoupper ($ firstLetter ).$ xattrName );
567
+ }
489
568
}
490
569
491
570
foreach ($ entityAttrs as $ colKey => $ attrName ) {
492
- $ setterName = $ this ->_colKeyToSetterOrGetter ($ colKey , 's ' );
493
571
$ colObj = $ this ->getTable ()->getColByKey ($ colKey );
494
- $ this ->_appendSetter ($ attrName , $ colObj ->getNormalName (), $ colObj ->getPHPType (), $ setterName , $ colObj ->getDatatype ());
572
+ if ($ colObj !== null ) {
573
+ $ setterName = $ this ->_colKeyToSetterOrGetter ($ colKey , 's ' );
574
+ $ this ->_appendSetter ($ attrName , $ colObj ->getNormalName (), $ colObj ->getPHPType (), $ setterName , $ colObj ->getDatatype ());
575
+ } else {
576
+ $ firstLetter = $ attrName [0 ];
577
+ $ xattrName = substr ($ attrName , 1 );
578
+ $ this ->_appendSetter ($ attrName , null , 'mixed ' , 'set ' . strtoupper ($ firstLetter ).$ xattrName , null );
579
+ }
495
580
}
496
581
$ this ->_createMapFunction ();
497
582
}
@@ -501,13 +586,23 @@ private function _createEntityVariables() {
501
586
502
587
foreach ($ entityAttrs as $ colKey => $ attrName ) {
503
588
$ colObj = $ this ->getTable ()->getColByKey ($ colKey );
504
- $ this ->classStr .= ""
505
- ." /** \n"
506
- ." * The attribute which is mapped to the column ' " .$ colObj ->getNormalName ()."'. \n"
507
- ." * \n"
508
- ." * @var " .$ colObj ->getPHPType ()."\n"
509
- ." **/ \n"
510
- ." private $ " .$ attrName ."; \n" ;
589
+ if ($ colObj !== null ) {
590
+ $ this ->classStr .= ""
591
+ ." /** \n"
592
+ ." * The attribute which is mapped to the column ' " .$ colObj ->getNormalName ()."'. \n"
593
+ ." * \n"
594
+ ." * @var " .$ colObj ->getPHPType ()."\n"
595
+ ." **/ \n"
596
+ ." private $ " .$ attrName ."; \n" ;
597
+ } else {
598
+ $ this ->classStr .= ""
599
+ ." /** \n"
600
+ ." * A custom attribute. \n"
601
+ ." * \n"
602
+ ." * @var mixed \n"
603
+ ." **/ \n"
604
+ ." private $ " .$ attrName ."; \n" ;
605
+ }
511
606
$ index ++;
512
607
}
513
608
}
0 commit comments