| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
abstract class CJTServicesEntityModel { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
protected $_propPrefix; |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @param mixed $data |
| 22 |
* @return CJTServicesItemModel |
| 23 |
*/ |
| 24 |
public function __construct($data = null) |
| 25 |
{ |
| 26 |
|
| 27 |
# Make sure data is array |
| 28 |
if(!$data) |
| 29 |
{ |
| 30 |
$data = array(); |
| 31 |
} |
| 32 |
|
| 33 |
# Fill with data |
| 34 |
$this->exchangeArray($data); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* put your comment there... |
| 39 |
* |
| 40 |
* @param mixed $data |
| 41 |
*/ |
| 42 |
public function exchangeArray($data) |
| 43 |
{ |
| 44 |
|
| 45 |
# Fetch properties for all model members |
| 46 |
foreach (get_object_vars($this) as $name => $value) |
| 47 |
{ |
| 48 |
|
| 49 |
# Don't process Private properties |
| 50 |
if (!$this->isPrivateProp($name)) |
| 51 |
{ |
| 52 |
$propName = $this->getPropertyName($name); |
| 53 |
|
| 54 |
$this->$name = isset($data[$propName]) ? $data[$propName] : null; |
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
# Chain |
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* put your comment there... |
| 65 |
* |
| 66 |
*/ |
| 67 |
public function getArray() |
| 68 |
{ |
| 69 |
|
| 70 |
# Gett all vars |
| 71 |
$vars = get_object_vars($this); |
| 72 |
$array = array(); |
| 73 |
|
| 74 |
# Exclude NULL values |
| 75 |
foreach ($vars as $name => $value) |
| 76 |
{ |
| 77 |
# Don't copy private prop or NULL fields |
| 78 |
if (!$this->isPrivateProp($name) && ($value !== null)) |
| 79 |
{ |
| 80 |
|
| 81 |
$propName = $this->getPropertyName($name); |
| 82 |
|
| 83 |
$array[$propName] = $value; |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
return $array; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* put your comment there... |
| 93 |
* |
| 94 |
* @param mixed $name |
| 95 |
*/ |
| 96 |
protected function getPropertyName($name) |
| 97 |
{ |
| 98 |
return "{$this->_propPrefix}{$name}"; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* put your comment there... |
| 103 |
* |
| 104 |
*/ |
| 105 |
public function getVarsArray() |
| 106 |
{ |
| 107 |
|
| 108 |
# Gett all vars |
| 109 |
$vars = get_object_vars($this); |
| 110 |
|
| 111 |
# Exclude NULL values |
| 112 |
foreach ($vars as $name => $value) |
| 113 |
{ |
| 114 |
if ($this->isPrivateProp($name) || ($value === null)) |
| 115 |
{ |
| 116 |
unset($vars[$name]); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $vars; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* put your comment there... |
| 125 |
* |
| 126 |
* @param CJTServicesEntityModel $model |
| 127 |
* @return CJTServicesEntityModel |
| 128 |
*/ |
| 129 |
public function isEqual(CJTServicesEntityModel & $model) |
| 130 |
{ |
| 131 |
return md5(print_r(get_object_vars($this), true)) == md5(print_r(get_object_vars($model), true)); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* put your comment there... |
| 136 |
* |
| 137 |
* @param mixed $name |
| 138 |
*/ |
| 139 |
protected function isPrivateProp($name) |
| 140 |
{ |
| 141 |
return (strpos($name, '_') === 0); |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|