Skip to content

Commit ff605b8

Browse files
committed
Update EntityMapper.php
1 parent 6e6d0ff commit ff605b8

File tree

1 file changed

+120
-25
lines changed

1 file changed

+120
-25
lines changed

webfiori/database/EntityMapper.php

+120-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @author Ibrahim
99
*
10-
* @version 1.0
10+
* @version 1.0.1
1111
*/
1212
class EntityMapper {
1313
/**
@@ -57,6 +57,14 @@ class EntityMapper {
5757
* @since 1.0
5858
*/
5959
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;
6068
/**
6169
* Creates new instance of the class.
6270
*
@@ -97,6 +105,51 @@ public function __construct($tableObj, $className, $path = __DIR__, $namespace =
97105
}
98106

99107
$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;
100153
}
101154
/**
102155
* Creates the class that the table records will be mapped to.
@@ -177,6 +230,11 @@ public function getAttribitesNames() {
177230
foreach ($keys as $keyName) {
178231
$retVal[$keyName] = $this->_colKeyToAttr($keyName);
179232
}
233+
234+
foreach ($this->getAttributes() as $attrName) {
235+
//The @ only used to show user defined attributes.
236+
$retVal[$attrName.'@'] = $attrName;
237+
}
180238
ksort($retVal, SORT_STRING);
181239

182240
return $retVal;
@@ -209,6 +267,12 @@ public function getEntityMethods() {
209267
$retVal['getters'][] = $this->_colKeyToSetterOrGetter($keyName, 'g');
210268
$retVal['setters'][] = $this->_colKeyToSetterOrGetter($keyName, 's');
211269
}
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+
}
212276
sort($retVal['getters'], SORT_STRING);
213277
sort($retVal['setters'], SORT_STRING);
214278

@@ -409,30 +473,39 @@ private function _appendGetterMethod($attrName, $colName, $phpType, $getterName)
409473
$this->classStr .= ""
410474
." /**\n"
411475
." * 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";
421490
}
422491
private function _appendSetter($attrName, $colName, $phpType, $setterName, $colDatatype) {
423492
$this->classStr .= ""
424493
." /**\n"
425494
." * 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"
428499
." * the name '".$colName."'.\n"
429-
." * \n"
500+
. " * \n";
501+
}
502+
$this->classStr .= ""
430503
." * @param \$$attrName ".$phpType." The new value of the attribute.\n"
431504
." **/\n"
432505
.' public function '.$setterName.'($'.$attrName.") {\n";
433506

434507
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";
436509
} else {
437510
$this->classStr .= ' $this->'.$attrName.' = $'.$attrName.";\n";
438511
}
@@ -484,14 +557,26 @@ private function _createEntityMethods() {
484557

485558
foreach ($entityAttrs as $colKey => $attrName) {
486559
$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+
}
489568
}
490569

491570
foreach ($entityAttrs as $colKey => $attrName) {
492-
$setterName = $this->_colKeyToSetterOrGetter($colKey, 's');
493571
$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+
}
495580
}
496581
$this->_createMapFunction();
497582
}
@@ -501,13 +586,23 @@ private function _createEntityVariables() {
501586

502587
foreach ($entityAttrs as $colKey => $attrName) {
503588
$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+
}
511606
$index++;
512607
}
513608
}

0 commit comments

Comments
 (0)